123456789_123456789_123456789_123456789_123456789_

Class: RuboCop::Cop::HashAlignmentStyles::TableAlignment

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Instance Chain:
Inherits: Object
Defined in: lib/rubocop/cop/mixin/hash_alignment_styles.rb

Overview

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

Instance Method Summary

Instance Method Details

#deltas_for_first_pair(first_pair)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/hash_alignment_styles.rb', line 84

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

#hash_rocket_delta(first_pair, current_pair) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/hash_alignment_styles.rb', line 98

def hash_rocket_delta(first_pair, current_pair)
  first_pair.loc.column + max_key_width(first_pair.parent) + 1 -
    current_pair.loc.operator.column
end

#key_delta(first_pair, current_pair) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/hash_alignment_styles.rb', line 94

def key_delta(first_pair, current_pair)
  first_pair.key_delta(current_pair)
end

#max_delimiter_width(hash_node) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/hash_alignment_styles.rb', line 115

def max_delimiter_width(hash_node)
  hash_node.pairs.map { |pair| pair.delimiter(true).length }.max
end

#max_key_width(hash_node) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/hash_alignment_styles.rb', line 111

def max_key_width(hash_node)
  hash_node.keys.map { |key| key.source.length }.max
end

#value_delta(first_pair, current_pair) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/hash_alignment_styles.rb', line 103

def value_delta(first_pair, current_pair)
  correct_value_column = first_pair.key.loc.column +
                         max_key_width(first_pair.parent) +
                         max_delimiter_width(first_pair.parent)

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