Module: Formatting::Currency

Includes:
Number
Included in:
Formatting
Defined in:
lib/formatting/currency.rb

Instance Method Summary collapse

Methods included from Number

#format_number

Instance Method Details

#format_currency(record, amount_or_method, opts = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/formatting/currency.rb', line 7

def format_currency(record, amount_or_method, opts = {})
  if record.is_a?(Symbol)
    raise NotARecordError, "Expected an object that could tell us its currency; got #{record.inspect}"
  end

  format_string = opts.fetch(:format, "<amount> <currency>")
  skip_currency = opts.fetch(:skip_currency, false)

  unless skip_currency
    currency = opts.fetch(:currency) {
      record.respond_to?(:currency) ? record.currency: nil
    }
    currency = nil if currency == false
  end

  if amount_or_method.is_a?(Symbol)
    amount = record.public_send(amount_or_method)
  else
    amount = amount_or_method
  end

  return "" if amount.nil?

  amount = format_number(amount, opts)
  apply_format_string(format_string, amount, currency)
end