Class: Currency::Exchange::Rate::Source::Base

Inherits:
Object
  • Object
show all
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

Deriver, Historical, Provider, TimedCache

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#nameObject

The name of this Exchange.



21
22
23
# File 'lib/currency/exchange/rate/source/base.rb', line 21

def name
  @name
end

#pivot_currencyObject

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_quantitizerObject

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

#verboseObject

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

#__subclass_responsibility(meth) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/currency/exchange/rate/source/base.rb', line 44

def __subclass_responsibility(meth)
  raise ::Currency::Exception::SubclassResponsibility, 
  [
   "#{self.class}#\#{meth}", 
   :class, self.class, 
   :method, method,
  ]
end

#clear_rate(c1, c2, time, recip = true) ⇒ Object

Flush any cached Rate between Currency c1 and c2.



79
80
81
82
83
84
# File 'lib/currency/exchange/rate/source/base.rb', line 79

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_ratesObject

Flush all cached Rate.



72
73
74
75
# File 'lib/currency/exchange/rate/source/base.rb', line 72

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.



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/currency/exchange/rate/source/base.rb', line 56

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

#currenciesObject

Returns a list of Currencies that the rate source provides.

Subclasses can override this method.



110
111
112
# File 'lib/currency/exchange/rate/source/base.rb', line 110

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.



120
121
122
# File 'lib/currency/exchange/rate/source/base.rb', line 120

def get_rate(c1, c2, time)
  __subclass_responsibility(:get_rate)
end

#get_rate_base(c1, c2, time) ⇒ Object

Returns a base Rate.

Subclasses are required to implement this method.



127
128
129
# File 'lib/currency/exchange/rate/source/base.rb', line 127

def get_rate_base(c1, c2, time)
  __subclass_responsibility(:get_rate_base)
end

#get_rates(time = nil) ⇒ Object

Returns a list of all available rates.

Subclasses must override this method.



135
136
137
# File 'lib/currency/exchange/rate/source/base.rb', line 135

def get_rates(time = nil)
  __subclass_responsibility(:get_rates)
end

#new_rate(c1, c2, c1_to_c2_rate, time = nil, derived = nil) ⇒ Object

Called by implementors to construct new Rate objects.



141
142
143
144
145
146
147
# File 'lib/currency/exchange/rate/source/base.rb', line 141

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.



153
154
155
# File 'lib/currency/exchange/rate/source/base.rb', line 153

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.



94
95
96
97
# File 'lib/currency/exchange/rate/source/base.rb', line 94

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.



102
103
104
# File 'lib/currency/exchange/rate/source/base.rb', line 102

def rates(time = nil)
  __subclass_responsibility(:rates)
end

#to_sObject Also known as: inspect

Returns a simple string rep.



159
160
161
# File 'lib/currency/exchange/rate/source/base.rb', line 159

def to_s
  "#<#{self.class.name} #{self.name && self.name.inspect}>"
end