Class: Currency::Exchange::Rate::Source::Base
- Defined in:
- lib/currency/exchange/rate/source/base.rb
Overview
Currency::Exchange::Rate::Source::Base
The Currency::Exchange::Rate::Source::Base class is the base class for currency exchange rate providers.
Currency::Exchange::Rate::Source::Base subclasses are Currency::Exchange::Rate factories.
Represents a method of converting between two currencies.
See Currency;:Exchange::Rate::source for more details.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#name ⇒ Object
The name of this Exchange.
-
#pivot_currency ⇒ Object
Currency to use as pivot for deriving rate pairs.
-
#time_quantitizer ⇒ Object
Returns the value of attribute time_quantitizer.
-
#verbose ⇒ Object
If true, this Exchange will log information.
Instance Method Summary collapse
-
#clear_rate(c1, c2, time, recip = true) ⇒ Object
Flush any cached Rate between Currency c1 and c2.
-
#clear_rates ⇒ Object
Flush all cached Rate.
-
#convert(m, c2, time = nil, c1 = nil) ⇒ Object
Converts Money m in Currency c1 to a new Money value in Currency c2.
-
#currencies ⇒ Object
Returns a list of Currencies that the rate source provides.
-
#get_rate(c1, c2, time) ⇒ Object
Determines and creates the Rate between Currency c1 and c2.
-
#get_rate_base(c1, c2, time) ⇒ Object
Returns a base Rate.
-
#get_rates(time = nil) ⇒ Object
Returns a list of all available rates.
-
#initialize(opt = { }) ⇒ Base
constructor
A new instance of Base.
-
#new_rate(c1, c2, c1_to_c2_rate, time = nil, derived = nil) ⇒ Object
Called by implementors to construct new Rate objects.
-
#normalize_time(time) ⇒ Object
Normalizes rate time to a quantitized value.
-
#rate(c1, c2, time) ⇒ Object
Returns the cached Rate between Currency c1 and c2 at a given time.
-
#rates(time = nil) ⇒ Object
Gets all rates available by this source.
-
#to_s ⇒ Object
(also: #inspect)
Returns a simple string rep.
Constructor Details
#initialize(opt = { }) ⇒ Base
Returns a new instance of Base.
33 34 35 36 37 38 39 40 41 |
# File 'lib/currency/exchange/rate/source/base.rb', line 33 def initialize(opt = { }) @name = nil @verbose = nil unless defined? @verbose @pivot_currency ||= :USD @rate = { } @currencies = nil opt.each_pair{|k,v| self.send("#{k}=", v)} end |
Instance Attribute Details
#name ⇒ Object
The name of this Exchange.
21 22 23 |
# File 'lib/currency/exchange/rate/source/base.rb', line 21 def name @name end |
#pivot_currency ⇒ Object
Currency to use as pivot for deriving rate pairs. Defaults to :USD.
25 26 27 |
# File 'lib/currency/exchange/rate/source/base.rb', line 25 def pivot_currency @pivot_currency end |
#time_quantitizer ⇒ Object
Returns the value of attribute time_quantitizer.
30 31 32 |
# File 'lib/currency/exchange/rate/source/base.rb', line 30 def time_quantitizer @time_quantitizer end |
#verbose ⇒ Object
If true, this Exchange will log information.
28 29 30 |
# File 'lib/currency/exchange/rate/source/base.rb', line 28 def verbose @verbose end |
Instance Method Details
#clear_rate(c1, c2, time, recip = true) ⇒ Object
Flush any cached Rate between Currency c1 and c2.
69 70 71 72 73 74 |
# File 'lib/currency/exchange/rate/source/base.rb', line 69 def clear_rate(c1, c2, time, recip = true) time = time && normalize_time(time) @rate["#{c1}:#{c2}:#{time}"] = nil @rate["#{c2}:#{c1}:#{time}"] = nil if recip time end |
#clear_rates ⇒ Object
Flush all cached Rate.
62 63 64 65 |
# File 'lib/currency/exchange/rate/source/base.rb', line 62 def clear_rates @rate.clear @currencies = nil end |
#convert(m, c2, time = nil, c1 = nil) ⇒ Object
Converts Money m in Currency c1 to a new Money value in Currency c2.
46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/currency/exchange/rate/source/base.rb', line 46 def convert(m, c2, time = nil, c1 = nil) c1 = m.currency if c1 == nil time = m.time if time == nil time = normalize_time(time) if c1 == c2 && normalize_time(m.time) == time m else rate = rate(c1, c2, time) # raise ::Currency::Exception::UnknownRate, "#{c1} #{c2} #{time}" unless rate rate && ::Currency::Money(rate.convert(m, c1), c2, time) end end |
#currencies ⇒ Object
Returns a list of Currencies that the rate source provides.
Subclasses can override this method.
100 101 102 |
# File 'lib/currency/exchange/rate/source/base.rb', line 100 def currencies @currencies ||= rates.collect{| r | [ r.c1, r.c2 ]}.flatten.uniq end |
#get_rate(c1, c2, time) ⇒ Object
Determines and creates the Rate between Currency c1 and c2.
May attempt to use a pivot currency to bridge between rates.
110 111 112 |
# File 'lib/currency/exchange/rate/source/base.rb', line 110 def get_rate(c1, c2, time) raise ::Currency::Exception::SubclassResponsibility, "#{self.class}#get_rate" end |
#get_rate_base(c1, c2, time) ⇒ Object
Returns a base Rate.
Subclasses are required to implement this method.
117 118 119 |
# File 'lib/currency/exchange/rate/source/base.rb', line 117 def get_rate_base(c1, c2, time) raise ::Currency::Exception::SubclassResponsibility, "#{self.class}#get_rate_base" end |
#get_rates(time = nil) ⇒ Object
Returns a list of all available rates.
Subclasses must override this method.
125 126 127 |
# File 'lib/currency/exchange/rate/source/base.rb', line 125 def get_rates(time = nil) raise ::Currency::Exception::SubclassResponsibility, "#{self.class}#get_rate" end |
#new_rate(c1, c2, c1_to_c2_rate, time = nil, derived = nil) ⇒ Object
Called by implementors to construct new Rate objects.
131 132 133 134 135 136 137 |
# File 'lib/currency/exchange/rate/source/base.rb', line 131 def new_rate(c1, c2, c1_to_c2_rate, time = nil, derived = nil) c1 = ::Currency::Currency.get(c1) c2 = ::Currency::Currency.get(c2) rate = ::Currency::Exchange::Rate.new(c1, c2, c1_to_c2_rate, name, time, derived) # $stderr.puts "new_rate = #{rate}" rate end |
#normalize_time(time) ⇒ Object
Normalizes rate time to a quantitized value.
Subclasses can override this method.
143 144 145 |
# File 'lib/currency/exchange/rate/source/base.rb', line 143 def normalize_time(time) time && (time_quantitizer || ::Currency::Exchange::TimeQuantitizer.current).quantitize_time(time) end |
#rate(c1, c2, time) ⇒ Object
Returns the cached Rate between Currency c1 and c2 at a given time.
Time is normalized using #normalize_time(time)
Subclasses can override this method to implement rate expiration rules.
84 85 86 87 |
# File 'lib/currency/exchange/rate/source/base.rb', line 84 def rate(c1, c2, time) time = time && normalize_time(time) @rate["#{c1}:#{c2}:#{time}"] ||= get_rate(c1, c2, time) end |
#rates(time = nil) ⇒ Object
Gets all rates available by this source.
92 93 94 |
# File 'lib/currency/exchange/rate/source/base.rb', line 92 def rates(time = nil) raise ::Currency::Exception::SubclassResponsibility, "#{self.class}#rate" end |
#to_s ⇒ Object Also known as: inspect
Returns a simple string rep.
149 150 151 |
# File 'lib/currency/exchange/rate/source/base.rb', line 149 def to_s "#<#{self.class.name} #{self.name && self.name.inspect}>" end |