Class: Currency::Currency
Overview
Represents a currency.
Currency objects are created on-demand by Currency::Currency::Factory.
See Currency.get method.
Defined Under Namespace
Classes: Factory
Instance Attribute Summary collapse
-
#code ⇒ Object
Returns the ISO three-letter currency code as a symbol.
-
#format_left ⇒ Object
readonly
Used by Formatter.
-
#format_right ⇒ Object
readonly
Used by Formatter.
-
#formatter ⇒ Object
The default Formatter.
-
#parser ⇒ Object
The default parser.
-
#scale ⇒ Object
The Currency’s scale factor.
-
#scale_exp ⇒ Object
readonly
The Currency’s scale factor.
-
#symbol ⇒ Object
The Currency’s symbol.
-
#symbol_html ⇒ Object
The Currency’s symbol as HTML.
Class Method Summary collapse
-
.cast_code(x) ⇒ Object
Internal method for converting currency codes to internal Symbol format.
-
.default ⇒ Object
Returns the default Factory’s currency.
-
.default=(x) ⇒ Object
Sets the default Factory’s currency.
-
.get(code) ⇒ Object
Returns the Currency object from the default Currency::Currency::Factory by its three-letter uppercase Symbol, such as :USD, or :CAD.
-
.method_missing(sel, *args, &blk) ⇒ Object
If selector is [A-Z][A-Z], load the currency via Factory.default.
Instance Method Summary collapse
-
#==(x) ⇒ Object
Returns true if the Currency’s are equal.
-
#eql?(x) ⇒ Boolean
Returns true if the Currency’s are equal.
-
#format(m, *opt) ⇒ Object
Formats the Money value as a string using the current Formatter.
- #formatter_or_default ⇒ Object
-
#hash ⇒ Object
Returns the hash of the Currency’s code.
-
#initialize(code, symbol = nil, scale = Currency::Config.current.scale) ⇒ Currency
constructor
Create a new currency.
-
#parse(str, *opt) ⇒ Object
Parse a Money string in this Currency.
- #parser_or_default ⇒ Object
-
#to_s ⇒ Object
Returns the Currency code as a String.
Constructor Details
#initialize(code, symbol = nil, scale = Currency::Config.current.scale) ⇒ Currency
Create a new currency. This should only be called from Currency::Currency::Factory. def initialize(code, symbol = nil, scale = 1000000)
48 49 50 51 52 |
# File 'lib/currency/currency.rb', line 48 def initialize(code, symbol = nil, scale = Currency::Config.current.scale) self.code = code self.symbol = symbol self.scale = scale end |
Instance Attribute Details
#code ⇒ Object
Returns the ISO three-letter currency code as a symbol. e.g. :USD, :CAD, etc.
14 15 16 |
# File 'lib/currency/currency.rb', line 14 def code @code end |
#format_left ⇒ Object (readonly)
Used by Formatter.
28 29 30 |
# File 'lib/currency/currency.rb', line 28 def format_left @format_left end |
#format_right ⇒ Object (readonly)
Used by Formatter.
25 26 27 |
# File 'lib/currency/currency.rb', line 25 def format_right @format_right end |
#formatter ⇒ Object
The default Formatter.
39 40 41 |
# File 'lib/currency/currency.rb', line 39 def formatter @formatter end |
#parser ⇒ Object
The default parser.
42 43 44 |
# File 'lib/currency/currency.rb', line 42 def parser @parser end |
#scale ⇒ Object
The Currency’s scale factor.
e.g: the :USD scale factor is 100.
18 19 20 |
# File 'lib/currency/currency.rb', line 18 def scale @scale end |
#scale_exp ⇒ Object (readonly)
The Currency’s scale factor.
e.g: the :USD scale factor is 2, where 10 ^ 2 == 100.
22 23 24 |
# File 'lib/currency/currency.rb', line 22 def scale_exp @scale_exp end |
#symbol ⇒ Object
The Currency’s symbol. e.g: USD symbol is ‘$’
32 33 34 |
# File 'lib/currency/currency.rb', line 32 def symbol @symbol end |
#symbol_html ⇒ Object
The Currency’s symbol as HTML. e.g: EUR symbol is ‘€’ (:html € :) or ‘€’ (:html € :)
36 37 38 |
# File 'lib/currency/currency.rb', line 36 def symbol_html @symbol_html end |
Class Method Details
.cast_code(x) ⇒ Object
Internal method for converting currency codes to internal Symbol format.
67 68 69 70 71 72 |
# File 'lib/currency/currency.rb', line 67 def self.cast_code(x) x = x.upcase.intern if x.kind_of?(String) raise ::Currency::Exception::InvalidCurrencyCode, x unless x.kind_of?(Symbol) raise ::Currency::Exception::InvalidCurrencyCode, x unless x.to_s.length == 3 x end |
.default ⇒ Object
Returns the default Factory’s currency.
145 146 147 |
# File 'lib/currency/currency.rb', line 145 def self.default Factory.default.currency end |
.default=(x) ⇒ Object
Sets the default Factory’s currency.
151 152 153 154 |
# File 'lib/currency/currency.rb', line 151 def self.default=(x) x = self.get(x) unless x.kind_of?(self) Factory.default.currency = x end |
.get(code) ⇒ Object
Returns the Currency object from the default Currency::Currency::Factory by its three-letter uppercase Symbol, such as :USD, or :CAD.
57 58 59 60 61 62 |
# File 'lib/currency/currency.rb', line 57 def self.get(code) # $stderr.puts "#{self}.get(#{code.inspect})" return nil unless code return code if code.kind_of?(::Currency::Currency) Factory.default.get_by_code(code) end |
.method_missing(sel, *args, &blk) ⇒ Object
If selector is [A-Z][A-Z], load the currency via Factory.default.
Currency::Currency.USD
=> #<Currency::Currency:0xb7d0917c @formatter=nil, @scale_exp=2, @scale=100, @symbol="$", @format_left=-3, @code=:USD, @parser=nil, @format_right=-2>
162 163 164 165 166 167 168 |
# File 'lib/currency/currency.rb', line 162 def self.method_missing(sel, *args, &blk) if args.size == 0 && (! block_given?) && /^[A-Z][A-Z][A-Z]$/.match(sel.to_s) Factory.default.get_by_code(sel) else super end end |
Instance Method Details
#==(x) ⇒ Object
Returns true if the Currency’s are equal.
88 89 90 |
# File 'lib/currency/currency.rb', line 88 def ==(x) self.class == x.class && @code == x.code end |
#eql?(x) ⇒ Boolean
Returns true if the Currency’s are equal.
82 83 84 |
# File 'lib/currency/currency.rb', line 82 def eql?(x) self.class == x.class && @code == x.code end |
#format(m, *opt) ⇒ Object
Formats the Money value as a string using the current Formatter. See Currency::Formatter#format.
128 129 130 |
# File 'lib/currency/currency.rb', line 128 def format(m, *opt) formatter_or_default.format(m, *opt) end |
#formatter_or_default ⇒ Object
133 134 135 |
# File 'lib/currency/currency.rb', line 133 def formatter_or_default (@formatter || ::Currency::Formatter.default) end |
#hash ⇒ Object
Returns the hash of the Currency’s code.
76 77 78 |
# File 'lib/currency/currency.rb', line 76 def hash @code.hash end |
#parse(str, *opt) ⇒ Object
Parse a Money string in this Currency.
See Currency::Parser#parse.
116 117 118 |
# File 'lib/currency/currency.rb', line 116 def parse(str, *opt) parser_or_default.parse(str, *opt) end |
#parser_or_default ⇒ Object
121 122 123 |
# File 'lib/currency/currency.rb', line 121 def parser_or_default (@parser || ::Currency::Parser.default) end |
#to_s ⇒ Object
Returns the Currency code as a String.
139 140 141 |
# File 'lib/currency/currency.rb', line 139 def to_s @code.to_s end |