Class: TimeSeries

Inherits:
Object
  • Object
show all
Defined in:
lib/asdrubal/time_series.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(from_date, to_date = from_date, step_days = 1) ⇒ TimeSeries

Returns a new instance of TimeSeries.



4
5
6
7
8
9
# File 'lib/asdrubal/time_series.rb', line 4

def initialize(from_date, to_date = from_date, step_days = 1)
  @span = (from_date..to_date)
  @step = step_days
  @dates = Hash.new(0)
  self.smooth_window = 2
end

Instance Attribute Details

#smooth_windowObject

Returns the value of attribute smooth_window.



2
3
4
# File 'lib/asdrubal/time_series.rb', line 2

def smooth_window
  @smooth_window
end

Class Method Details

.from(year, month, day) ⇒ Object



11
12
13
# File 'lib/asdrubal/time_series.rb', line 11

def self.from(year, month, day)
  TimeSeries.new to_date(year, month, day)
end

Instance Method Details

#[](date) ⇒ Object

Raises:

  • (Exception)


39
40
41
42
43
# File 'lib/asdrubal/time_series.rb', line 39

def [](date)
  raise Exception.new('Out of range') unless include?(date)
  refresh_cache
  at(date).value
end

#[]=(date, value) ⇒ Object

Raises:

  • (Exception)


45
46
47
48
49
# File 'lib/asdrubal/time_series.rb', line 45

def []=(date, value)
  raise Exception.new('Out of range') unless include?(date)
  reset_cache
  @dates[date] = value
end

#at(date) ⇒ Object



34
35
36
37
# File 'lib/asdrubal/time_series.rb', line 34

def at(date)
  refresh_cache
  @cache[date]
end

#eachObject



24
25
26
27
28
# File 'lib/asdrubal/time_series.rb', line 24

def each
  @span.step(@step).each do |date|
    yield date, self[date]
  end
end

#include?(date) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/asdrubal/time_series.rb', line 30

def include?(date)
  @span.include?(date)
end

#step(days) ⇒ Object



19
20
21
22
# File 'lib/asdrubal/time_series.rb', line 19

def step(days)
  @step = days
  self
end

#to(year, month, day) ⇒ Object



15
16
17
# File 'lib/asdrubal/time_series.rb', line 15

def to(year, month, day)
  TimeSeries.new @span.first, self.class.to_date(year, month, day)
end