Class: MovingAverage
Instance Attribute Summary
Attributes inherited from Transform
#settings, #table
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from Transform
#columns, display_name, #initialize, #setting, transforms
Constructor Details
This class inherits a constructor from Transform
Class Method Details
2
3
4
|
# File 'app/transforms/moving_average.rb', line 2
def self.form_keys
[:columns, :days, :date]
end
|
Instance Method Details
#date_column ⇒ Object
11
12
13
|
# File 'app/transforms/moving_average.rb', line 11
def date_column
setting(:date) || default_date_column || first_date_column
end
|
#num_days ⇒ Object
6
7
8
9
|
# File 'app/transforms/moving_average.rb', line 6
def num_days
val = setting(:days).to_i
val <= 0 ? 7 : val
end
|
#result ⇒ Object
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
# File 'app/transforms/moving_average.rb', line 15
def result
cols = self.columns
datecol = self.date_column
daycount = self.num_days
avg_cols = cols.collect { |col| "#{col}_#{daycount}" }
out = Ruport::Data::Table.new( :column_names => [datecol] + cols + avg_cols)
data = day_data
days = data["days"]
first = data["earliest"]
latest = data["latest"]
current = first
while current <= latest do
row = {}
row[datecol] = current
cols.each do |col|
value = 0
value = days[current][col].to_f if days[current]
sum = 0
count = 0
check = current - daycount + 1
while check <= current
count += 1 unless check < first
sum += days[check][col].to_f if days[check]
check += 1
end
row["#{col}_#{daycount}"] = sum / count
row["#{col}"] = value
end
out << row
current += 1
end
out
end
|