Class: JIJI::Dao::RateDao

Inherits:
Object
  • Object
show all
Defined in:
lib/jiji/dao/rate_dao.rb

Overview

レートDAO

Defined Under Namespace

Classes: PairBuffer, RatesAggregator, RawRatesAggregator

Instance Method Summary collapse

Constructor Details

#initialize(data_dir, scales = []) ⇒ RateDao

Returns a new instance of RateDao.



14
15
16
17
18
19
# File 'lib/jiji/dao/rate_dao.rb', line 14

def initialize( data_dir, scales=[] )
  @data_dir = data_dir
  @scales = scales
  @daos = {}
  @mutex = Mutex.new
end

Instance Method Details

#dao(pair) ⇒ Object

ペアに対応するdaoを取得する



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/jiji/dao/rate_dao.rb', line 50

def dao(pair)
  @mutex.synchronize {
    return @daos[pair] if @daos.key? pair

    dir = "#{@data_dir}/#{pair.to_s}"
    FileUtils.mkdir_p dir unless File.exists? dir

    aggregators = @scales.map {|s| RatesAggregator.new(s) }
    aggregators << RawRatesAggregator.new

    @daos[pair] = TimedDataDao.new( dir, aggregators )
    @daos[pair]
  }
end

#each(scale = :raw, pair = :USDJPY, start_date = nil, end_date = nil) ⇒ Object

所定の期間のレートデータを得る。



22
23
24
25
26
# File 'lib/jiji/dao/rate_dao.rb', line 22

def each( scale=:raw, pair=:USDJPY, start_date=nil, end_date=nil )
  dao( pair ).each_data( scale, start_date, end_date ) {|row,time|
     yield row
  }
end

#each_all_pair_rates(scale = :raw, start_date = nil, end_date = nil, &block) ⇒ Object

所定の期間のすべての通貨ペアのレートデータを得る。



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/jiji/dao/rate_dao.rb', line 29

def each_all_pair_rates( scale=:raw, start_date=nil, end_date=nil, &block )
  pairs = list_pairs
  its  = nil
  begin
    its = pairs.map {|p|
      dao(p).each_data( scale,start_date,end_date )
    }
    _each_all_pair_rates( pairs, its, &block )
  ensure
    its.each {|i| i.close } if its
  end
end

#list_pairsObject



65
66
67
68
69
70
71
72
73
# File 'lib/jiji/dao/rate_dao.rb', line 65

def list_pairs
  list = []
  Dir.glob( "#{@data_dir}/*" ) {|d|
   next unless File.directory? d
   next unless d =~ /\/([A-Z]+)$/
   list << $1
  }
  list
end

#next_rates(rates) ⇒ Object

次のレートを追加する。



43
44
45
46
47
# File 'lib/jiji/dao/rate_dao.rb', line 43

def next_rates( rates )
  rates.each_pair {|pair,v|
    dao(pair) << v
  }
end