Class: RuboCop::Cop::Money::ZeroMoney

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Defined in:
lib/rubocop/cop/money/zero_money.rb

Constant Summary collapse

MSG =

‘Money.zero` and it’s alias ‘empty`, with or without currency argument is removed in favour of the more explicit Money.new syntax. Supplying it with a real currency is preferred for additional currency safety checks.

If no currency was supplied, it defaults to Money::NULL_CURRENCY which was the default setting of Money.default_currency and should effectively be the same. The cop can be configured with a ReplacementCurrency in case that is more appropriate for your application.

Examples:


# bad
Money.zero

# good when configured with `ReplacementCurrency: CAD`
Money.new(0, 'CAD')
'Money.zero is removed, use `Money.new(0, %<currency>s)`.'

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rubocop/cop/money/zero_money.rb', line 35

def on_send(node)
  receiver, _ = *node

  money_zero(node) do |currency_arg|
    add_offense(node, message: format(MSG, currency: replacement_currency(currency_arg))) do |corrector|
      corrector.replace(
        node.loc.expression,
        "#{receiver.source}.new(0, #{replacement_currency(currency_arg)})",
      )
    end
  end
end