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



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

def clone
  self.class.new( data_array.dup, headers: headers, cleaned: cleaned )
end

#collapse(*args) ⇒ Object



114
115
116
117
118
# File 'lib/quandl/data/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
124
# File 'lib/quandl/data/operations.rb', line 119

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

#frequencyObject



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

def frequency
  @frequency ||= Quandl::Operation::Collapse.frequency?( data_array )
end

#frequency=(value) ⇒ Object



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

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

#limit(amount) ⇒ Object



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

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

#limit!(amount) ⇒ Object



75
76
77
# File 'lib/quandl/data/operations.rb', line 75

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

#row(*args) ⇒ Object



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

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

#sort_ascendingObject



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

def sort_ascending
  clone.sort_ascending!
end

#sort_ascending!Object



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

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

#sort_descendingObject



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

def sort_descending
  clone.sort_descending!
end

#sort_descending!Object



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

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

#sort_order(dir) ⇒ Object



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

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



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

def to_date
  clone.to_date!
end

#to_date!Object



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

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

#to_date_strObject



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

def to_date_str
  clone.to_date_str!
end

#to_date_str!Object



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

def to_date_str!
  @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



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

def to_jd
  clone.to_jd!
end

#to_jd!Object



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

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

#to_precision(value) ⇒ Object



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

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

#to_precision!(value) ⇒ Object



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

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

#transform(*args) ⇒ Object



102
103
104
105
106
# File 'lib/quandl/data/operations.rb', line 102

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

#transform=(value) ⇒ Object



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

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

#trim_end(date) ⇒ Object



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

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

#trim_end!(date) ⇒ Object



64
65
66
67
68
69
70
71
72
73
# File 'lib/quandl/data/operations.rb', line 64

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

#trim_start(date) ⇒ Object



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

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

#trim_start!(date) ⇒ Object



53
54
55
56
57
58
59
60
61
62
# File 'lib/quandl/data/operations.rb', line 53

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