Class: Currency::Currency::Factory

Inherits:
Object
  • Object
show all
Defined in:
lib/currency/currency/factory.rb

Overview

Responsible for creating Currency::Currency objects on-demand.

Constant Summary collapse

@@default =
nil

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*opts) ⇒ Factory

Returns a new instance of Factory.



19
20
21
22
23
# File 'lib/currency/currency/factory.rb', line 19

def initialize(*opts)
  @currency_by_code = { }
  @currency_by_symbol = { }
  @currency = nil
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sel, *args, &blk) ⇒ Object

If selector is [A-Z][A-Z], load the currency.

factory.USD
=> #<Currency::Currency:0xb7d0917c @formatter=nil, @scale_exp=2, @scale=100, @symbol="$", @format_left=-3, @code=:USD, @parser=nil, @format_right=-2>


110
111
112
113
114
115
116
# File 'lib/currency/currency/factory.rb', line 110

def method_missing(sel, *args, &blk)
  if args.size == 0 && (! block_given?) && /^[A-Z][A-Z][A-Z]$/.match(sel.to_s)
    self.get_by_code(sel)
  else
    super
  end
end

Class Method Details

.defaultObject

Returns the default Currency::Factory.



10
11
12
# File 'lib/currency/currency/factory.rb', line 10

def self.default
  @@default ||= self.new
end

.default=(x) ⇒ Object

Sets the default Currency::Factory.



14
15
16
# File 'lib/currency/currency/factory.rb', line 14

def self.default=(x)
  @@default = x
end

Instance Method Details

#currencyObject

Returns the default Currency. Defaults to self.get_by_code(:USD).



94
95
96
# File 'lib/currency/currency/factory.rb', line 94

def currency
  @currency ||= self.get_by_code(:USD)
end

#currency=(x) ⇒ Object

Sets the default Currency.



100
101
102
# File 'lib/currency/currency/factory.rb', line 100

def currency=(x)
  @currency = x
end

#get_by_code(x) ⇒ Object

Lookup Currency by code.



27
28
29
30
31
# File 'lib/currency/currency/factory.rb', line 27

def get_by_code(x)
  x = ::Currency::Currency.cast_code(x)
  # $stderr.puts "get_by_code(#{x})"
  @currency_by_code[x] ||= install(load(::Currency::Currency.new(x)))
end

#get_by_symbol(symbol) ⇒ Object

Lookup Currency by symbol.



35
36
37
# File 'lib/currency/currency/factory.rb', line 35

def get_by_symbol(symbol)
  @currency_by_symbol[symbol] ||= install(load(::Currency::Currency.new(nil, symbol)))
end

#install(currency) ⇒ Object

Installs a new Currency for #get_by_symbol and #get_by_code.



85
86
87
88
89
# File 'lib/currency/currency/factory.rb', line 85

def install(currency)
  raise ::Currency::Exception::UnknownCurrency.new() unless currency
  @currency_by_symbol[currency.symbol] ||= currency unless currency.symbol.nil?
  @currency_by_code[currency.code] = currency
end

#load(currency) ⇒ Object

This method initializes a Currency object as requested from #get_by_code or #get_by_symbol.

This method must initialize:

currency.code
currency.scale

Optionally:

currency.symbol
currency.symbol_html

Subclasses that provide Currency metadata should override this method. For example, loading Currency metadata from a database or YAML file.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/currency/currency/factory.rb', line 55

def load(currency)
  # $stderr.puts "BEFORE: load(#{currency.code})"

  # Basic
  if currency.code == :USD || currency.symbol == '$'
	# $stderr.puts "load('USD')"
    currency.code = :USD
    currency.symbol = '$'
    currency.scale = 100
  elsif currency.code == :CAD
    # $stderr.puts "load('CAD')"
    currency.symbol = '$'
    currency.scale = 100
  elsif currency.code == :EUR
    # $stderr.puts "load('CAD')"
    currency.symbol = nil
    currency.symbol_html = '&#8364;'
    currency.scale = 100
  else
    currency.symbol = nil
    currency.scale = 100
  end
  
  # $stderr.puts "AFTER: load(#{currency.inspect})"

  currency
end