Module: Quandl::Data::Operations

Extended by:
ActiveSupport::Concern
Included in:
Quandl::Data
Defined in:
lib/quandl/data/operations.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#cloneObject



137
138
139
# File 'lib/quandl/data/operations.rb', line 137

def clone
  self.class.new( _attributes: attributes.clone )
end

#collapse(*args) ⇒ Object



118
119
120
121
122
# File 'lib/quandl/data/operations.rb', line 118

def collapse(*args)
  return @collapse unless args.first.present?
  self.collapse = args.first
  self
end

#collapse=(collapse) ⇒ Object



123
124
125
126
127
128
# File 'lib/quandl/data/operations.rb', line 123

def collapse=(collapse)
  return false unless Quandl::Operation::Collapse.valid?(collapse)
  @collapse = collapse
  @frequency = collapse
  self.data_array = Quandl::Operation::Collapse.perform( data_array, collapse )
end

#frequencyObject



130
131
132
# File 'lib/quandl/data/operations.rb', line 130

def frequency
  @frequency ||= Quandl::Babelfish.guess_frequency( data_array ).try(:to_sym)
end

#frequency=(value) ⇒ Object



133
134
135
# File 'lib/quandl/data/operations.rb', line 133

def frequency=(value)
  @frequency = value.to_sym if value.present?
end

#limit(amount) ⇒ Object



159
160
161
# File 'lib/quandl/data/operations.rb', line 159

def limit(amount)
  clone.limit!(amount)
end

#limit!(amount) ⇒ Object



79
80
81
# File 'lib/quandl/data/operations.rb', line 79

def limit!(amount)
  self.data_array = data_array[0..( amount.to_i - 1 )]; self
end

#row(*args) ⇒ Object



99
100
101
102
103
104
# File 'lib/quandl/data/operations.rb', line 99

def row(*args)
  return @row if args[0].nil?
  @row = args[0]
  self.data_array = [data_array[ args[0] ]]
  self
end

#sort_ascendingObject



162
163
164
# File 'lib/quandl/data/operations.rb', line 162

def sort_ascending
  clone.sort_ascending!
end

#sort_ascending!Object



87
88
89
# File 'lib/quandl/data/operations.rb', line 87

def sort_ascending!
  self.data_array = Quandl::Operation::Sort.asc( data_array ); self
end

#sort_descendingObject



165
166
167
# File 'lib/quandl/data/operations.rb', line 165

def sort_descending
  clone.sort_descending!
end

#sort_descending!Object



91
92
93
# File 'lib/quandl/data/operations.rb', line 91

def sort_descending!
  self.data_array = Quandl::Operation::Sort.desc( data_array ); self
end

#sort_order(dir) ⇒ Object



83
84
85
# File 'lib/quandl/data/operations.rb', line 83

def sort_order(dir)
  dir == :asc ? sort_ascending! : sort_descending!
end

#to_csvObject



34
35
36
37
38
39
# File 'lib/quandl/data/operations.rb', line 34

def to_csv
  return data_array.collect(&:to_csv).join if data_array?
  return pristine_data.collect(&:to_csv).join if pristine_data.respond_to?(:collect)
  return pristine_data if pristine_data.kind_of?(String)
  return ''
end

#to_dateObject



147
148
149
# File 'lib/quandl/data/operations.rb', line 147

def to_date
  clone.to_date!
end

#to_date!Object



49
50
51
# File 'lib/quandl/data/operations.rb', line 49

def to_date!
  self.data_array = Quandl::Data::Format.to_date( data_array ); self
end

#to_date_strObject



150
151
152
# File 'lib/quandl/data/operations.rb', line 150

def to_date_str
  clone.to_date_str!
end

#to_date_str!Object



53
54
55
# File 'lib/quandl/data/operations.rb', line 53

def to_date_str!
  self.data_array = to_date!.collect{|r| r = r.dup; r[0] = r[0].to_s; r }; self
end

#to_hObject



27
28
29
30
31
32
# File 'lib/quandl/data/operations.rb', line 27

def to_h
  data_array.inject({}) do |memo, row|
    memo[row[0]] = row[1..-1]
    memo
  end
end

#to_jdObject



144
145
146
# File 'lib/quandl/data/operations.rb', line 144

def to_jd
  clone.to_jd!
end

#to_jd!Object



45
46
47
# File 'lib/quandl/data/operations.rb', line 45

def to_jd!
  self.data_array = Quandl::Data::Format.to_jd( data_array ); self
end

#to_precision(value) ⇒ Object



141
142
143
# File 'lib/quandl/data/operations.rb', line 141

def to_precision(value)
  clone.to_precision!(value)
end

#to_precision!(value) ⇒ Object



95
96
97
# File 'lib/quandl/data/operations.rb', line 95

def to_precision!(value)
  self.data_array = Quandl::Operation::Value.precision(data_array, value); self
end

#to_tableObject



41
42
43
# File 'lib/quandl/data/operations.rb', line 41

def to_table
  self
end

#transform(*args) ⇒ Object



106
107
108
109
110
# File 'lib/quandl/data/operations.rb', line 106

def transform(*args)
  return @transform unless args.first.present?
  self.transform = args.first
  self
end

#transform=(value) ⇒ Object



111
112
113
114
115
116
# File 'lib/quandl/data/operations.rb', line 111

def transform=(value)
  return false unless Quandl::Operation::Transform.valid?(value)
  @transform = value
  self.data_array = Quandl::Operation::Transform.perform( data_array, value )
  self.data_array
end

#trim_end(date) ⇒ Object



156
157
158
# File 'lib/quandl/data/operations.rb', line 156

def trim_end(date)
  clone.trim_end!(date)
end

#trim_end!(date) ⇒ Object



68
69
70
71
72
73
74
75
76
77
# File 'lib/quandl/data/operations.rb', line 68

def trim_end!(date)
  # date format
  date = Quandl::Operation::QDate.parse(date)
  # reject rows with dates less than
  self.data_array = to_date!.sort_descending!.delete_if do |row|
    row_date = row[0]
    row_date > date
  end
  self
end

#trim_start(date) ⇒ Object



153
154
155
# File 'lib/quandl/data/operations.rb', line 153

def trim_start(date)
  clone.trim_start!(date)
end

#trim_start!(date) ⇒ Object



57
58
59
60
61
62
63
64
65
66
# File 'lib/quandl/data/operations.rb', line 57

def trim_start!(date)
  # date format
  date = Quandl::Operation::QDate.parse(date)
  # reject rows with dates less than
  self.data_array = to_date!.sort_descending!.delete_if do |row|
    row_date = row[0]
    row_date < date
  end
  self
end