Module: Quandl::Data::Table::Operations
- Extended by:
- ActiveSupport::Concern
- Includes:
- Operation
- Included in:
- Quandl::Data::Table
- Defined in:
- lib/quandl/data/table/operations.rb
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
- #collapse(*args) ⇒ Object
- #collapse=(collapse) ⇒ Object
- #frequency ⇒ Object
- #frequency=(value) ⇒ Object
- #limit(amount) ⇒ Object
- #parse(data) ⇒ Object
- #sort_ascending ⇒ Object
- #sort_ascending! ⇒ Object
- #sort_descending ⇒ Object
- #sort_descending! ⇒ Object
- #sort_order(dir) ⇒ Object
- #to_csv ⇒ Object
- #to_date ⇒ Object
- #to_date! ⇒ Object
- #to_h ⇒ Object
- #to_jd ⇒ Object
- #to_jd! ⇒ Object
- #transform(*args) ⇒ Object
- #transform=(value) ⇒ Object
- #trim_end(date) ⇒ Object
- #trim_start(date) ⇒ Object
Instance Method Details
#collapse(*args) ⇒ Object
114 115 116 117 118 |
# File 'lib/quandl/data/table/operations.rb', line 114 def collapse(*args) return @collapse unless args.first.present? self.collapse = args.first self end |
#collapse=(collapse) ⇒ Object
119 120 121 122 123 |
# File 'lib/quandl/data/table/operations.rb', line 119 def collapse=(collapse) @collapse = collapse @frequency = collapse @data_array = Collapse.perform( data_array, collapse ) end |
#frequency ⇒ Object
125 126 127 |
# File 'lib/quandl/data/table/operations.rb', line 125 def frequency @frequency ||= Collapse.frequency?( data_array ) end |
#frequency=(value) ⇒ Object
128 129 130 |
# File 'lib/quandl/data/table/operations.rb', line 128 def frequency=(value) @frequency = value.to_sym end |
#limit(amount) ⇒ Object
79 80 81 82 |
# File 'lib/quandl/data/table/operations.rb', line 79 def limit(amount) @data_array = data_array[0..( amount.to_i - 1 )] if amount.present? self end |
#parse(data) ⇒ Object
132 133 134 135 |
# File 'lib/quandl/data/table/operations.rb', line 132 def parse(data) data = Parse.perform( data ) data end |
#sort_ascending ⇒ Object
88 89 90 |
# File 'lib/quandl/data/table/operations.rb', line 88 def sort_ascending Table.new( Parse.sort( data_array, :asc ), frequency: frequency ) end |
#sort_ascending! ⇒ Object
91 92 93 94 |
# File 'lib/quandl/data/table/operations.rb', line 91 def sort_ascending! @data_array = Parse.sort( data_array, :asc ) self end |
#sort_descending ⇒ Object
96 97 98 |
# File 'lib/quandl/data/table/operations.rb', line 96 def sort_descending Table.new( Parse.sort( data_array, :desc ), frequency: frequency ) end |
#sort_descending! ⇒ Object
99 100 101 102 |
# File 'lib/quandl/data/table/operations.rb', line 99 def sort_descending! @data_array = Parse.sort( data_array, :desc ) self end |
#sort_order(dir) ⇒ Object
84 85 86 |
# File 'lib/quandl/data/table/operations.rb', line 84 def sort_order(dir) dir == :asc ? sort_ascending! : sort_descending! end |
#to_csv ⇒ Object
32 33 34 35 36 37 |
# File 'lib/quandl/data/table/operations.rb', line 32 def to_csv return data_array.collect(&:to_csv).join if data_array? return pristine_data.collect(&:to_csv).join if pristine_data.kind_of?(Array) return pristine_data if pristine_data.kind_of?(String) return '' end |
#to_date ⇒ Object
47 48 49 |
# File 'lib/quandl/data/table/operations.rb', line 47 def to_date Parse.julian_to_date data_array end |
#to_date! ⇒ Object
50 51 52 53 |
# File 'lib/quandl/data/table/operations.rb', line 50 def to_date! @data_array = Parse.julian_to_date data_array self end |
#to_h ⇒ Object
25 26 27 28 29 30 |
# File 'lib/quandl/data/table/operations.rb', line 25 def to_h data_array.inject({}) do |memo, row| memo[row[0]] = row[1..-1] memo end end |
#to_jd ⇒ Object
39 40 41 |
# File 'lib/quandl/data/table/operations.rb', line 39 def to_jd Table.new( Parse.date_to_julian( data_array ), frequency: frequency ) end |
#to_jd! ⇒ Object
42 43 44 45 |
# File 'lib/quandl/data/table/operations.rb', line 42 def to_jd! @data_array = Parse.date_to_julian( data_array ) self end |
#transform(*args) ⇒ Object
104 105 106 107 108 |
# File 'lib/quandl/data/table/operations.rb', line 104 def transform(*args) return @transform unless args.first.present? self.transform = args.first self end |
#transform=(value) ⇒ Object
109 110 111 112 |
# File 'lib/quandl/data/table/operations.rb', line 109 def transform=(value) @transform = value @data_array = Transform.perform( data_array, value ) end |
#trim_end(date) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/quandl/data/table/operations.rb', line 67 def trim_end(date) # date format date = Date.parse(date) if date.is_a?(String) date = date.jd if date.respond_to?(:jd) # find index index = data_array.rindex{|r| r[0] == date } # trim if index is valid @data_array = @data_array[0..index] if index && @data_array[index] # chainable self end |
#trim_start(date) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/quandl/data/table/operations.rb', line 55 def trim_start(date) # date format date = Date.parse(date) if date.is_a?(String) date = date.jd if date.respond_to?(:jd) # find index index = data_array.rindex{|r| r[0] == date } # trim if index is valid @data_array = @data_array[index..-1] if index && @data_array[index] # chainable self end |