Class: Money::MoneyFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/money/money_formatter.rb

Overview

Money::MoneyFormatter persues locale-specific formatting of a Money instance.

m = Money::Money.new(100, "EUR")
puts MoneyFormatter.new(m, :locale => "de-DE")

or as a shortcut:

m = Money::Money.new(100, "EUR")
puts m.format(:locale => "de-DE")

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(money, options = {}) ⇒ MoneyFormatter

Returns a new instance of MoneyFormatter.



22
23
24
25
26
27
28
29
# File 'lib/money/money_formatter.rb', line 22

def initialize(money, options = {})
  @money = money
  @locale = options[:locale] || Money.default_locale
  @locale.gsub!(/_/, '-')
  @bank = options[:bank] || Money.default_bank
  @format = FORMATS[@locale]
  @currency_symbol = CURRENCIES[@money.currency][:symbol]
end

Instance Attribute Details

#bankObject (readonly)

Returns the value of attribute bank.



20
21
22
# File 'lib/money/money_formatter.rb', line 20

def bank
  @bank
end

#localeObject (readonly)

Returns the value of attribute locale.



20
21
22
# File 'lib/money/money_formatter.rb', line 20

def locale
  @locale
end

#moneyObject (readonly)

Returns the value of attribute money.



20
21
22
# File 'lib/money/money_formatter.rb', line 20

def money
  @money
end

Instance Method Details

#to_sObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/money/money_formatter.rb', line 31

def to_s
  unless @money.nan?
    pattern = @money.positive? || @money.zero? ? @format[:pos_pattern] : @format[:neg_pattern]
    precision = CURRENCIES[@money.currency][:decimal_digits] # @format[:decimal_digits]
    # divisor = 10 ** precision
    #if @money.amount.round(2) == 1.77
    #  puts ""
    #  puts "Money = #{@money.amount} #{@money.currency}"
    #  puts "  Prec: #{precision}"
    #  puts "  Divi: #{divisor}"
    #end
    number = number_with_precision(
      @money.amount.to_f.abs, # / divisor, 
      precision, 
      @format[:decimal_sep], 
      @format[:group_sep])
    pattern.gsub("n", number).gsub("$", @currency_symbol)
  end
end