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'.

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.newTableAlignment

[ GitHub ]

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

def initialize
  self.max_key_width = 0
end

Instance Attribute Details

#max_key_width (rw, private)

[ GitHub ]

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

attr_accessor :max_key_width

Instance Method Details

#deltas_for_first_pair(first_pair, node)

[ GitHub ]

  
# 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

#hash_rocket_delta(first_pair, current_pair) (private)

[ GitHub ]

  
# 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) (private)

[ GitHub ]

  
# 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) (private)

[ GitHub ]

  
# 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