Class: Currency::Currency::Factory
- Defined in:
- lib/currency/currency/factory.rb
Overview
Responsible for creating Currency::Currency objects on-demand.
Constant Summary collapse
- CURRENCY_SYMBOLS =
{ :AFN => {:symbol => '؋', :symbol_html => '؋'}, :ALL => {:symbol => 'Lek', :symbol_html => 'Lek'}, :ANG => {:symbol => 'ƒ', :symbol_html => 'ƒ'}, :ARS => {:symbol => '$', :symbol_html => '$'}, :AUD => {:symbol => '$', :symbol_html => '$'}, :AWG => {:symbol => 'ƒ', :symbol_html => 'ƒ'}, :AZN => {:symbol => 'ман', :symbol_html => 'ман'}, :BAM => {:symbol => 'KM', :symbol_html => 'KM'}, :BBD => {:symbol => '$', :symbol_html => '$'}, :BGN => {:symbol => 'лв', :symbol_html => 'лв'}, :BMD => {:symbol => '$', :symbol_html => '$'}, :BND => {:symbol => '$', :symbol_html => '$'}, :BOB => {:symbol => '$b', :symbol_html => '$b'}, :BRL => {:symbol => 'R$', :symbol_html => 'R$'}, :BSD => {:symbol => '$', :symbol_html => '$'}, :BWP => {:symbol => 'P', :symbol_html => 'P'}, :BYR => {:symbol => 'p.', :symbol_html => 'p.'}, :BZD => {:symbol => 'BZ$', :symbol_html => 'BZ$'}, :CAD => {:symbol => '$', :symbol_html => '$'}, :CHF => {:symbol => 'CHF', :symbol_html => 'CHF'}, :CLP => {:symbol => '$', :symbol_html => '$'}, :CNY => {:symbol => '¥', :symbol_html => '¥'}, :COP => {:symbol => '$', :symbol_html => '$'}, :CRC => {:symbol => '₡', :symbol_html => '₡'}, :CUP => {:symbol => '₱', :symbol_html => '₱'}, :CZK => {:symbol => 'Kč', :symbol_html => 'Kč'}, :DKK => {:symbol => 'kr', :symbol_html => 'kr'}, :DOP => {:symbol => 'RD$', :symbol_html => 'RD$'}, :EEK => {:symbol => 'kr', :symbol_html => 'kr'}, :EGP => {:symbol => '£', :symbol_html => '£'}, :EUR => {:symbol => '€', :symbol_html => '€'}, :FJD => {:symbol => '$', :symbol_html => '$'}, :FKP => {:symbol => '£', :symbol_html => '£'}, :GBP => {:symbol => '£', :symbol_html => '£'}, :GGP => {:symbol => '£', :symbol_html => '£'}, :GHC => {:symbol => '¢', :symbol_html => '¢'}, :GIP => {:symbol => '£', :symbol_html => '£'}, :GTQ => {:symbol => 'Q', :symbol_html => 'Q'}, :GYD => {:symbol => '$', :symbol_html => '$'}, :HKD => {:symbol => '$', :symbol_html => '$'}, :HNL => {:symbol => 'L', :symbol_html => 'L'}, :HRK => {:symbol => 'kn', :symbol_html => 'kn'}, :HUF => {:symbol => 'Ft', :symbol_html => 'Ft'}, :IDR => {:symbol => 'Rp', :symbol_html => 'Rp'}, :ILS => {:symbol => '₪', :symbol_html => '₪'}, :IMP => {:symbol => '£', :symbol_html => '£'}, :INR => {:symbol => '₨', :symbol_html => '₨'}, :IRR => {:symbol => '﷼', :symbol_html => '﷼'}, :ISK => {:symbol => 'kr', :symbol_html => 'kr'}, :JEP => {:symbol => '£', :symbol_html => '£'}, :JMD => {:symbol => 'J$', :symbol_html => 'J$'}, :JPY => {:symbol => '¥', :symbol_html => '¥'}, :KGS => {:symbol => 'лв', :symbol_html => 'лв'}, :KHR => {:symbol => '៛', :symbol_html => '៛'}, :KPW => {:symbol => '₩', :symbol_html => '₩'}, :KRW => {:symbol => '₩', :symbol_html => '₩'}, :KYD => {:symbol => '$', :symbol_html => '$'}, :KZT => {:symbol => 'лв', :symbol_html => 'лв'}, :LAK => {:symbol => '₭', :symbol_html => '₭'}, :LBP => {:symbol => '£', :symbol_html => '£'}, :LKR => {:symbol => '₨', :symbol_html => '₨'}, :LRD => {:symbol => '$', :symbol_html => '$'}, :LTL => {:symbol => 'Lt', :symbol_html => 'Lt'}, :LVL => {:symbol => 'Ls', :symbol_html => 'Ls'}, :MKD => {:symbol => 'ден', :symbol_html => 'ден'}, :MNT => {:symbol => '₮', :symbol_html => '₮'}, :MUR => {:symbol => '₨', :symbol_html => '₨'}, :MXN => {:symbol => '$', :symbol_html => '$'}, :MYR => {:symbol => 'RM', :symbol_html => 'RM'}, :MZN => {:symbol => 'MT', :symbol_html => 'MT'}, :NAD => {:symbol => '$', :symbol_html => '$'}, :NGN => {:symbol => '₦', :symbol_html => '₦'}, :NIO => {:symbol => 'C$', :symbol_html => 'C$'}, :NOK => {:symbol => 'kr', :symbol_html => 'kr'}, :NPR => {:symbol => '₨', :symbol_html => '₨'}, :NZD => {:symbol => '$', :symbol_html => '$'}, :OMR => {:symbol => '﷼', :symbol_html => '﷼'}, :PAB => {:symbol => 'B/.', :symbol_html => 'B/.'}, :PEN => {:symbol => 'S/.', :symbol_html => 'S/.'}, :PHP => {:symbol => 'Php', :symbol_html => 'Php'}, :PKR => {:symbol => '₨', :symbol_html => '₨'}, :PLN => {:symbol => 'zł', :symbol_html => 'zł'}, :PYG => {:symbol => 'Gs', :symbol_html => 'Gs'}, :QAR => {:symbol => '﷼', :symbol_html => '﷼'}, :RON => {:symbol => 'lei', :symbol_html => 'lei'}, :RSD => {:symbol => 'Дин.', :symbol_html => 'Дин.'}, :RUB => {:symbol => 'руб', :symbol_html => 'руб'}, :SAR => {:symbol => '﷼', :symbol_html => '﷼'}, :SBD => {:symbol => '$', :symbol_html => '$'}, :SCR => {:symbol => '₨', :symbol_html => '₨'}, :SEK => {:symbol => 'kr', :symbol_html => 'kr'}, :SGD => {:symbol => '$', :symbol_html => '$'}, :SHP => {:symbol => '£', :symbol_html => '£'}, :SOS => {:symbol => 'S', :symbol_html => 'S'}, :SRD => {:symbol => '$', :symbol_html => '$'}, :SVC => {:symbol => '$', :symbol_html => '$'}, :SYP => {:symbol => '£', :symbol_html => '£'}, :THB => {:symbol => '฿', :symbol_html => '฿'}, :TRL => {:symbol => '₤', :symbol_html => '₤'}, :TRY => {:symbol => 'TL', :symbol_html => 'TL'}, :TTD => {:symbol => 'TT$', :symbol_html => 'TT$'}, :TVD => {:symbol => '$', :symbol_html => '$'}, :TWD => {:symbol => 'NT$', :symbol_html => 'NT$'}, :UAH => {:symbol => '₴', :symbol_html => '₴'}, :USD => {:symbol => '$', :symbol_html => '$'}, :UYU => {:symbol => '$U', :symbol_html => '$U'}, :UZS => {:symbol => 'лв', :symbol_html => 'лв'}, :VEF => {:symbol => 'Bs', :symbol_html => 'Bs'}, :VND => {:symbol => '₫', :symbol_html => '₫'}, :XCD => {:symbol => '$', :symbol_html => '$'}, :YER => {:symbol => '﷼', :symbol_html => '﷼'}, :ZAR => {:symbol => 'R', :symbol_html => 'R'}, :ZWD => {:symbol => 'Z$', :symbol_html => 'Z$'}, }
- UNIQUE_SYMBOLS =
CURRENCY_SYMBOLS.collect {|code, symbol| symbol[:symbol]}.uniq
- CODES =
CURRENCY_SYMBOLS.collect {|code, symbol| code}
- @@default =
nil
Class Method Summary collapse
-
.default ⇒ Object
Returns the default Currency::Factory.
-
.default=(x) ⇒ Object
Sets the default Currency::Factory.
- .get_currency_from_symbol(symbol) ⇒ Object
Instance Method Summary collapse
-
#currency ⇒ Object
Returns the default Currency.
-
#currency=(x) ⇒ Object
Sets the default Currency.
-
#get_by_code(x) ⇒ Object
Lookup Currency by code.
-
#get_by_symbol(symbol) ⇒ Object
Lookup Currency by symbol.
-
#initialize(*opts) ⇒ Factory
constructor
A new instance of Factory.
-
#install(currency) ⇒ Object
Installs a new Currency for #get_by_symbol and #get_by_code.
-
#load(currency) ⇒ Object
This method initializes a Currency object as requested from #get_by_code or #get_by_symbol.
-
#method_missing(sel, *args, &blk) ⇒ Object
If selector is [A-Z][A-Z], load the currency.
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>
217 218 219 220 221 222 223 |
# File 'lib/currency/currency/factory.rb', line 217 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
.default ⇒ Object
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 |
.get_currency_from_symbol(symbol) ⇒ Object
62 63 64 65 66 67 68 69 70 |
# File 'lib/currency/currency/factory.rb', line 62 def self.get_currency_from_symbol(symbol) return Currency::Currency.get(:USD) if symbol == "$" return Currency::Currency.get(:GBP) if symbol == "£" return Currency::Currency.get(:EUR) if symbol == "€" CURRENCY_SYMBOLS.sort {|a,b| a.to_s <=> b.to_s}.each do |code, hash| return Currency::Currency.get(code) if symbol == hash[:symbol] end Currency::Currency.default end |
Instance Method Details
#currency ⇒ Object
Returns the default Currency. Defaults to self.get_by_code(:USD).
201 202 203 |
# File 'lib/currency/currency/factory.rb', line 201 def currency @currency ||= self.get_by_code(:USD) end |
#currency=(x) ⇒ Object
Sets the default Currency.
207 208 209 |
# File 'lib/currency/currency/factory.rb', line 207 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.
192 193 194 195 196 |
# File 'lib/currency/currency/factory.rb', line 192 def install(currency) raise ::Currency::Exception::UnknownCurrency 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 |
# File 'lib/currency/currency/factory.rb', line 55 def load(currency) currency.symbol = CURRENCY_SYMBOLS[currency.code] ? CURRENCY_SYMBOLS[currency.code][:symbol] : nil currency.symbol_html = CURRENCY_SYMBOLS[currency.code] ? CURRENCY_SYMBOLS[currency.code][:symbol_html] : nil currency.scale = 100 currency end |