class RuboCop::Cop::Style::AlignHash::TableAlignment

Handles calculation of deltas when the enforced style is 'table'.

Public Instance Methods

deltas_for_first_pair(first_pair, node) click to toggle source

The table style is the only one where the first key-value pair can be considered to have bad alignment.

# File lib/rubocop/cop/style/align_hash.rb, line 71
def deltas_for_first_pair(first_pair, node)
  key_widths = node.children.map do |pair|
    key, _value = *pair
    key.source.length
  end
  @max_key_width = key_widths.max

  separator_delta = separator_delta(first_pair,
                                    first_pair.loc.operator, 0)
  {
    separator: separator_delta,
    value:     value_delta(first_pair, first_pair) - separator_delta
  }
end

Private Instance Methods

hash_rocket_delta(first_pair, current_separator) click to toggle source
# File lib/rubocop/cop/style/align_hash.rb, line 92
def hash_rocket_delta(first_pair, current_separator)
  first_pair.loc.column + @max_key_width + 1 -
    current_separator.column
end
key_delta(first_pair, current_pair) click to toggle source
# File lib/rubocop/cop/style/align_hash.rb, line 88
def key_delta(first_pair, current_pair)
  first_pair.loc.column - current_pair.loc.column
end
spaced_separator(node) click to toggle source
# File lib/rubocop/cop/style/align_hash.rb, line 106
def spaced_separator(node)
  node.loc.operator.is?('=>') ? ' => ' : ': '
end
value_delta(first_pair, current_pair) click to toggle source
# File lib/rubocop/cop/style/align_hash.rb, line 97
def value_delta(first_pair, current_pair)
  first_key, = *first_pair
  _, current_value = *current_pair
  correct_value_column = first_key.loc.column +
                         spaced_separator(current_pair).length +
                         @max_key_width
  correct_value_column - current_value.loc.column
end