Class: Quandl::Operation::Collapse
- Inherits:
-
Object
- Object
- Quandl::Operation::Collapse
- Defined in:
- lib/quandl/operation/collapse.rb,
lib/quandl/operation/collapse/guess.rb
Defined Under Namespace
Classes: Guess
Class Method Summary collapse
- .assert_valid_arguments!(data, type) ⇒ Object
- .collapse(data, frequency) ⇒ Object
- .collapses_greater_than(freq) ⇒ Object
- .collapses_greater_than_or_equal_to(freq) ⇒ Object
- .find_each_index(array, find) ⇒ Object
- .find_end_of_range(date, frequency) ⇒ Object
- .frequency?(data) ⇒ Boolean
- .inside_range?(date, range) ⇒ Boolean
- .merge_row_values(top_row, bottom_row) ⇒ Object
- .perform(data, type) ⇒ Object
- .to_table(data) ⇒ Object
- .valid?(type) ⇒ Boolean
- .valid_collapse?(type) ⇒ Boolean
- .valid_collapses ⇒ Object
Class Method Details
.assert_valid_arguments!(data, type) ⇒ Object
27 28 29 30 |
# File 'lib/quandl/operation/collapse.rb', line 27 def assert_valid_arguments!(data, type) raise ArgumentError, "data must be an Array. Received: #{data.class}" unless data.is_a?(Array) raise ArgumentError, "frequency must be one of #{valid_collapses}. Received: #{type}" unless valid?(type) end |
.collapse(data, frequency) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/quandl/operation/collapse.rb', line 44 def collapse(data, frequency) return data unless valid_collapse?( frequency ) # store the new collapsed data collapsed_data = {} range = find_end_of_range( data[0][0], frequency ) # iterate over the data data.each do |row| # grab date and values date, value = row[0], row[1..-1] value = value.first if value.count == 1 # bump to the next range if it exceeds the current one range = find_end_of_range(date, frequency) unless inside_range?(date, range) # consider the value for the next range if inside_range?(date, range) && value.present? # merge this and previous row if nils are present value = merge_row_values( value, collapsed_data[range] ) unless collapsed_data[range].nil? # assign value collapsed_data[range] = value end end to_table(collapsed_data) end |
.collapses_greater_than(freq) ⇒ Object
88 89 90 91 92 |
# File 'lib/quandl/operation/collapse.rb', line 88 def collapses_greater_than(freq) return [] unless freq.respond_to?(:to_sym) index = valid_collapses.index(freq.to_sym) index.present? ? valid_collapses.slice( index + 1, valid_collapses.count ) : [] end |
.collapses_greater_than_or_equal_to(freq) ⇒ Object
94 95 96 97 |
# File 'lib/quandl/operation/collapse.rb', line 94 def collapses_greater_than_or_equal_to(freq) return [] unless freq.respond_to?(:to_sym) valid_collapses.slice( valid_collapses.index(freq.to_sym), valid_collapses.count ) end |
.find_each_index(array, find) ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/quandl/operation/collapse.rb', line 111 def find_each_index(array, find) found, index, q = -1, -1, [] while found found = array[index+1..-1].index(find) if found index = index + found + 1 q << index end end q end |
.find_end_of_range(date, frequency) ⇒ Object
107 108 109 |
# File 'lib/quandl/operation/collapse.rb', line 107 def find_end_of_range(date, frequency) date.end_of_frequency(frequency) end |
.frequency?(data) ⇒ Boolean
99 100 101 |
# File 'lib/quandl/operation/collapse.rb', line 99 def frequency?(data) Guess.frequency(data) end |
.inside_range?(date, range) ⇒ Boolean
103 104 105 |
# File 'lib/quandl/operation/collapse.rb', line 103 def inside_range?(date, range) date <= range end |
.merge_row_values(top_row, bottom_row) ⇒ Object
77 78 79 80 81 82 83 84 85 86 |
# File 'lib/quandl/operation/collapse.rb', line 77 def merge_row_values(top_row, bottom_row) # merge previous values when nils are present if top_row.is_a?(Array) && top_row.include?(nil) # find nil indexes indexes = find_each_index(top_row, nil) # merge nils with previous values indexes.each{|index| top_row[index] = bottom_row[index] } end top_row end |
.perform(data, type) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/quandl/operation/collapse.rb', line 11 def perform(data, type) assert_valid_arguments!(data, type) # nothing to do with an empty array return data unless data.compact.present? # source order order = Sort.order?(data) # operations expect data in ascending order data = Sort.asc(data) # collapse data = collapse(data, type) # return to original order data = Sort.desc(data) if order == :desc # onwards data end |
.to_table(data) ⇒ Object
67 68 69 70 71 72 73 74 75 |
# File 'lib/quandl/operation/collapse.rb', line 67 def to_table(data) data.collect do |date, values| if values.is_a?(Array) values.unshift(date) else [date, values] end end end |
.valid?(type) ⇒ Boolean
36 37 38 |
# File 'lib/quandl/operation/collapse.rb', line 36 def valid?(type) valid_collapses.include?( type.try(:to_sym) ) end |
.valid_collapse?(type) ⇒ Boolean
32 33 34 |
# File 'lib/quandl/operation/collapse.rb', line 32 def valid_collapse?(type) valid?(type) end |
.valid_collapses ⇒ Object
40 41 42 |
# File 'lib/quandl/operation/collapse.rb', line 40 def valid_collapses [ :daily, :weekly, :monthly, :quarterly, :annual ] end |