class RuboCop::Cop::Style::AlignHash::AlignmentOfValues

Common functionality for the styles where not only keys, but also values are aligned.

Public Instance Methods

checkable_layout(node) click to toggle source
# File lib/rubocop/cop/style/align_hash.rb, line 34
def checkable_layout(node)
  !any_pairs_on_the_same_line?(node) && all_have_same_separator?(node)
end
deltas(first_pair, current_pair) click to toggle source
# File lib/rubocop/cop/style/align_hash.rb, line 38
def deltas(first_pair, current_pair)
  key_delta = key_delta(first_pair, current_pair)
  current_separator = current_pair.loc.operator
  separator_delta = separator_delta(first_pair, current_separator,
                                    key_delta)
  value_delta = value_delta(first_pair, current_pair) -
                key_delta - separator_delta

  { key: key_delta, separator: separator_delta, value: value_delta }
end

Private Instance Methods

all_have_same_separator?(node) click to toggle source
# File lib/rubocop/cop/style/align_hash.rb, line 59
def all_have_same_separator?(node)
  first_separator = node.children.first.loc.operator.source
  node.children.butfirst.all? do |pair|
    pair.loc.operator.is?(first_separator)
  end
end
separator_delta(first_pair, current_separator, key_delta) click to toggle source
# File lib/rubocop/cop/style/align_hash.rb, line 51
def separator_delta(first_pair, current_separator, key_delta)
  if current_separator.is?(':')
    0 # Colon follows directly after key
  else
    hash_rocket_delta(first_pair, current_separator) - key_delta
  end
end