class RuboCop::Cop::HashAlignmentStyles::TableAlignment

Handles calculation of deltas when the enforced style is ‘table’.

Attributes

max_key_width[RW]

Public Class Methods

new() click to toggle source
# File lib/rubocop/cop/mixin/hash_alignment_styles.rb, line 84
def initialize
  self.max_key_width = 0
end

Public Instance Methods

deltas_for_first_pair(first_pair, node) click to toggle source
# File lib/rubocop/cop/mixin/hash_alignment_styles.rb, line 88
def deltas_for_first_pair(first_pair, node)
  self.max_key_width = node.keys.map { |key| key.source.length }.max

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

Private Instance Methods

hash_rocket_delta(first_pair, current_pair) click to toggle source
# File lib/rubocop/cop/mixin/hash_alignment_styles.rb, line 106
def hash_rocket_delta(first_pair, current_pair)
  first_pair.loc.column + max_key_width + 1 - current_pair.loc.operator.column
end
key_delta(first_pair, current_pair) click to toggle source
# File lib/rubocop/cop/mixin/hash_alignment_styles.rb, line 102
def key_delta(first_pair, current_pair)
  first_pair.key_delta(current_pair)
end
value_delta(first_pair, current_pair) click to toggle source
# File lib/rubocop/cop/mixin/hash_alignment_styles.rb, line 110
def value_delta(first_pair, current_pair)
  correct_value_column = first_pair.key.loc.column +
                         current_pair.delimiter(true).length +
                         max_key_width

  current_pair.value_omission? ? 0 : correct_value_column - current_pair.value.loc.column
end