Class: Currency::Exchange::Rate::Source::Historical

Inherits:
Base
  • Object
show all
Defined in:
lib/currency/exchange/rate/source/historical.rb

Overview

Gets historical rates from database using Active::Record. Rates are retrieved using Currency::Exchange::Rate::Source::Historical::Rate as a database record proxy.

See Currency::Exchange::Rate::Source::Historical::Writer for a rate archiver.

Defined Under Namespace

Classes: RateLoader, Writer

Instance Attribute Summary collapse

Attributes inherited from Base

#pivot_currency, #time_quantitizer, #verbose

Instance Method Summary collapse

Methods inherited from Base

#__subclass_responsibility, #clear_rate, #convert, #currencies, #get_rate_base, #new_rate, #normalize_time, #rate, #rates, #to_s

Constructor Details

#initialize(*opt) ⇒ Historical

Returns a new instance of Historical.



18
19
20
21
# File 'lib/currency/exchange/rate/source/historical.rb', line 18

def initialize
  @source = nil # any
  super
end

Instance Attribute Details

#sourceObject

Select specific rate source. Defaults to nil



16
17
18
# File 'lib/currency/exchange/rate/source/historical.rb', line 16

def source
  @source
end

Instance Method Details

#clear_ratesObject



42
43
44
45
46
# File 'lib/currency/exchange/rate/source/historical.rb', line 42

def clear_rates
  @rates_cache.clear
  @raw_rates_cache.clear
  super
end

#get_rate(c1, c2, time) ⇒ Object

Returns a Rate.



50
51
52
53
54
55
# File 'lib/currency/exchange/rate/source/historical.rb', line 50

def get_rate(c1, c2, time)
  # rate = 
  get_rates(time).select{ | r | r.c1 == c1 && r.c2 == c2 }[0]
  # $stderr.puts "#{self}.get_rate(#{c1}, #{c2}, #{time.inspect}) => #{rate.inspect}"
  # rate
end

#get_rates(time = nil) ⇒ Object

Return a list of base Rates.



59
60
61
62
63
64
# File 'lib/currency/exchange/rate/source/historical.rb', line 59

def get_rates(time = nil)
  @rates_cache["#{source_key}:#{time}"] ||= 
    get_raw_rates(time).collect do | rr |
      rr.to_rate
    end
end

#get_raw_rates(time = nil) ⇒ Object

Return a list of raw rates.



68
69
70
71
72
# File 'lib/currency/exchange/rate/source/historical.rb', line 68

def get_raw_rates(time = nil)
  @raw_rates_cache["#{source_key}:#{time}"] ||= 
    ::Currency::Exchange::Rate::Source::Historical::Rate.new(:c1 => nil, :c2 => nil, :date => time, :source => source).
      find_matching_this(:all)
end

#nameObject

This Exchange’s name is the same as its #uri.



30
31
32
# File 'lib/currency/exchange/rate/source/historical.rb', line 30

def name
  "historical #{source_key}"
end

#source_keyObject



24
25
26
# File 'lib/currency/exchange/rate/source/historical.rb', line 24

def source_key
  @source ? @source.join(',') : ''
end