Class: Currency::Currency

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

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code, symbol = nil, scale = 100) ⇒ Currency

Create a new currency. This should only be called from Currency::Currency::Factory.



47
48
49
50
51
52
53
54
55
# File 'lib/currency/currency.rb', line 47

def initialize(code, symbol = nil, scale = 100)
  self.code = code
  self.symbol = symbol
  self.scale = scale

  @formatter =
    @parser = 
    nil
end

Instance Attribute Details

#codeObject

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_leftObject (readonly)

Used by Formatter.



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

def format_left
  @format_left
end

#format_rightObject (readonly)

Used by Formatter.



25
26
27
# File 'lib/currency/currency.rb', line 25

def format_right
  @format_right
end

#formatterObject

The default Formatter.



39
40
41
# File 'lib/currency/currency.rb', line 39

def formatter
  @formatter
end

#parserObject

The default parser.



42
43
44
# File 'lib/currency/currency.rb', line 42

def parser
  @parser
end

#scaleObject

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_expObject (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

#symbolObject

The Currency’s symbol. e.g: USD symbol is ‘$’



32
33
34
# File 'lib/currency/currency.rb', line 32

def symbol
  @symbol
end

#symbol_htmlObject

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.



70
71
72
73
74
75
# File 'lib/currency/currency.rb', line 70

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

.defaultObject

Returns the default Factory’s currency.



148
149
150
# File 'lib/currency/currency.rb', line 148

def self.default
  Factory.default.currency
end

.default=(x) ⇒ Object

Sets the default Factory’s currency.



154
155
156
157
# File 'lib/currency/currency.rb', line 154

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.



60
61
62
63
64
65
# File 'lib/currency/currency.rb', line 60

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>


165
166
167
168
169
170
171
# File 'lib/currency/currency.rb', line 165

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.



91
92
93
# File 'lib/currency/currency.rb', line 91

def ==(x)
  self.class == x.class && @code == x.code
end

#eql?(x) ⇒ Boolean

Returns true if the Currency’s are equal.

Returns:

  • (Boolean)


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

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.



131
132
133
# File 'lib/currency/currency.rb', line 131

def format(m, *opt)
   formatter_or_default.format(m, *opt)
end

#formatter_or_defaultObject



136
137
138
# File 'lib/currency/currency.rb', line 136

def formatter_or_default
  (@formatter || ::Currency::Formatter.default)
end

#hashObject

Returns the hash of the Currency’s code.



79
80
81
# File 'lib/currency/currency.rb', line 79

def hash
  @code.hash
end

#parse(str, *opt) ⇒ Object

Parse a Money string in this Currency.

See Currency::Parser#parse.



119
120
121
# File 'lib/currency/currency.rb', line 119

def parse(str, *opt)
  parser_or_default.parse(str, *opt)
end

#parser_or_defaultObject



124
125
126
# File 'lib/currency/currency.rb', line 124

def parser_or_default
  (@parser || ::Currency::Parser.default)
end

#to_sObject

Returns the Currency code as a String.



142
143
144
# File 'lib/currency/currency.rb', line 142

def to_s
  @code.to_s
end