class RuboCop::Cop::Money::ZeroMoney

Constants

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.

@example

# bad
Money.zero

# good when configured with `ReplacementCurrency: CAD`
Money.new(0, 'CAD')

Public Instance Methods

autocorrect(node) click to toggle source
# 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) click to toggle source
# 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

Private Instance Methods

replacement_currency(currency_arg) click to toggle source
# File lib/rubocop/cop/money/zero_money.rb, line 56
def replacement_currency(currency_arg)
  return currency_arg.first.source unless currency_arg.empty?
  return "'#{cop_config['ReplacementCurrency']}'" if cop_config['ReplacementCurrency']

  'Money::NULL_CURRENCY'
end