Class: Recurly::Money
- Inherits:
-
Object
- Object
- Recurly::Money
- Defined in:
- lib/recurly/money.rb
Overview
Represents a collection of currencies (in cents).
Instance Method Summary collapse
-
#<=>(other) ⇒ -1, ...
Implemented so that solitary currencies can be compared and sorted.
-
#eql?(other) ⇒ true, false
instance.
-
#hash ⇒ Integer
Unique identifier.
-
#initialize(currencies = {}) ⇒ Object
constructor
A money object representing multiple currencies (in cents).
- #inspect ⇒ String (also: #to_s)
- #respond_to?(method_name, include_private = false) ⇒ true, false
-
#to_hash ⇒ Hash
A hash of currency codes to amounts.
Constructor Details
#initialize(currencies = {}) ⇒ Object
Returns A money object representing multiple currencies (in cents).
16 17 18 19 20 21 22 23 24 |
# File 'lib/recurly/money.rb', line 16 def initialize currencies = {} @currencies = Helper.hash_with_indifferent_read_access if currencies.respond_to? :each_pair currencies.each_pair { |key, value| @currencies[key.to_s] = value } else @currencies[Recurly.default_currency] = currencies end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object (private)
91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/recurly/money.rb', line 91 def method_missing name, *args, &block if currencies.respond_to? name return currencies.send name, *args, &block elsif c = currencies[Recurly.default_currency] and c.respond_to? name if currencies.keys.length > 1 raise TypeError, "can't convert multicurrency into Integer" else return c.send name, *args, &block end end super end |
Instance Method Details
#<=>(other) ⇒ -1, ...
Implemented so that solitary currencies can be compared and sorted.
52 53 54 55 56 57 58 59 60 |
# File 'lib/recurly/money.rb', line 52 def <=> other if currencies.keys.length == 1 && other.currencies.length == 1 if currencies.keys == other.currencies.keys return currencies.values.first <=> other.currencies.values.first end end currencies <=> other.currencies end |
#eql?(other) ⇒ true, false
instance.
34 35 36 |
# File 'lib/recurly/money.rb', line 34 def eql? other other.respond_to?(:currencies) && currencies.eql?(other.currencies) end |
#hash ⇒ Integer
Returns Unique identifier.
40 41 42 |
# File 'lib/recurly/money.rb', line 40 def hash currencies.hash end |
#inspect ⇒ String Also known as: to_s
69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/recurly/money.rb', line 69 def inspect string = "#<#{self.class}" if currencies.any? string << " %s" % currencies.keys.sort.map { |code| value = currencies[code].to_s value.gsub!(/^(\d)$/, '0_0\1') value.gsub!(/^(\d{2})$/, '0_\1') value.gsub!(/(\d)(\d{2})$/, '\1_\2') value.gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, '\1_') "#{code}: #{value}" }.join(', ') end string << '>' end |
#respond_to?(method_name, include_private = false) ⇒ true, false
64 65 66 |
# File 'lib/recurly/money.rb', line 64 def respond_to? method_name, include_private = false super || currencies.respond_to?(method_name, include_private) end |
#to_hash ⇒ Hash
Returns A hash of currency codes to amounts.
27 28 29 |
# File 'lib/recurly/money.rb', line 27 def to_hash currencies.dup end |