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

Inherits:
RuboCop::Cop show all
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

#autocorrect(node) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/rubocop/cop/money/zero_money.rb', line 39

def autocorrect(node)
  receiver, _ = *node

  lambda do |corrector|
    money_zero(node) do |currency_arg|
      replacement_currency = replacement_currency(currency_arg)

      corrector.replace(
        node.loc.expression,
        "#{receiver.source}.new(0, #{replacement_currency})"
      )
    end
  end
end

#on_send(node) ⇒ Object



33
34
35
36
37
# File 'lib/rubocop/cop/money/zero_money.rb', line 33

def on_send(node)
  money_zero(node) do |currency_arg|
    add_offense(node, message: format(MSG, currency: replacement_currency(currency_arg)))
  end
end