class RuboCop::Cop::Style::NumericPredicate

bar.baz > 0
0 > foo
foo == 0
# good
bar.baz.positive?
foo.negative?
foo.zero?
# bad
@example EnforcedStyle: comparison
bar.baz.positive?
foo.negative?
foo.zero?
# good
bar.baz > 0
0 > foo
foo == 0
# bad
@example EnforcedStyle: predicate (default)
not themselves ‘Integer` polymorphic.
populated with objects which can be compared with integers, but are
The cop ignores comparisons to global variables, since they are often
`!= 0`.
but not `true` and `false`, and thus not always interchangeable with
The cop disregards `#nonzero?` as it its value is truthy or falsey,
The cop can also be configured to do the reverse.
These can be replaced by their respective predicate methods.
`>`, `<`) to test numbers as zero, positive, or negative.
This cop checks for usage of comparison operators (`==`,

def autocorrect(node)

def autocorrect(node)
  _, replacement = check(node)
  lambda do |corrector|
    corrector.replace(node.loc.expression, replacement)
  end
end

def check(node)

def check(node)
  numeric, operator =
    if style == :predicate
      comparison(node) || inverted_comparison(node, &invert)
    else
      predicate(node)
    end
  return unless numeric && operator
  [numeric, replacement(numeric, operator)]
end

def invert

def invert
  lambda do |comparison, numeric|
    comparison = { :> => :<, :< => :> }[comparison] || comparison
    [numeric, comparison]
  end
end

def on_send(node)

def on_send(node)
  return if node.each_ancestor(:send, :block).any? do |ancestor|
    ignored_method?(ancestor.method_name)
  end
  numeric, replacement = check(node)
  return unless numeric
  add_offense(node,
              message: format(MSG,
                              prefer: replacement,
                              current: node.source))
end

def parenthesized_source(node)

def parenthesized_source(node)
  if require_parentheses?(node)
    "(#{node.source})"
  else
    node.source
  end
end

def replacement(numeric, operation)

def replacement(numeric, operation)
  if style == :predicate
    [parenthesized_source(numeric),
     REPLACEMENTS.invert[operation.to_s]].join('.')
  else
    [numeric.source, REPLACEMENTS[operation.to_s], 0].join(' ')
  end
end

def require_parentheses?(node)

def require_parentheses?(node)
  node.send_type? && node.binary_operation? && !node.parenthesized?
end