Module: Quandl::Data::Operations

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

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#collapse(*args) ⇒ Object



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

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

#collapse=(collapse) ⇒ Object



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

def collapse=(collapse)
  @collapse = collapse
  @frequency = collapse
  @data_array = Collapse.perform( data_array, collapse )
end

#frequencyObject



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

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

#frequency=(value) ⇒ Object



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

def frequency=(value)
  @frequency = value.to_sym
end

#limit(amount) ⇒ Object



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

def limit(amount)
  Quandl::Data.new( data_array[0..( amount.to_i - 1 )] )
end

#limit!(amount) ⇒ Object



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

def limit!(amount)
  @data_array = limit(amount).data_array
  self
end

#parse(data) ⇒ Object



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

def parse(data)
  data = Parse.perform( data )
  data
end

#sort_ascendingObject



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

def sort_ascending
  Quandl::Data.new( Parse.sort( data_array.dup, :asc ), frequency: frequency )
end

#sort_ascending!Object



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

def sort_ascending!
  @data_array = sort_ascending.data_array
  self
end

#sort_descendingObject



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

def sort_descending
  Quandl::Data.new( Parse.sort( data_array.dup, :desc ), frequency: frequency )
end

#sort_descending!Object



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

def sort_descending!
  @data_array = sort_descending.data_array
  self
end

#sort_order(dir) ⇒ Object



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

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

#to_csvObject



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

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



47
48
49
# File 'lib/quandl/data/operations.rb', line 47

def to_date
  Quandl::Data.new( Parse.julian_to_date( data_array ) )
end

#to_date!Object



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

def to_date!
  @data_array = to_date.data_array
  self
end

#to_hObject



21
22
23
24
25
26
# File 'lib/quandl/data/operations.rb', line 21

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

#to_jdObject



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

def to_jd
  Quandl::Data.new( Parse.date_to_julian( data_array ), frequency: frequency )
end

#to_jd!Object



38
39
40
41
# File 'lib/quandl/data/operations.rb', line 38

def to_jd!
  @data_array = Parse.date_to_julian( data_array )
  self
end

#transform(*args) ⇒ Object



115
116
117
118
119
# File 'lib/quandl/data/operations.rb', line 115

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

#transform=(value) ⇒ Object



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

def transform=(value)
  @transform = value
  @data_array = Transform.perform( data_array, value )
end

#trim_end(trim_date) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/quandl/data/operations.rb', line 73

def trim_end(trim_date)
  # date format
  trim_date = Date.parse(trim_date) if trim_date.is_a?(String)
  trim_date = trim_date.jd if trim_date.respond_to?(:jd)
  # find index
  return self unless trim_date.is_a?(Integer)
  # reject rows with dates less than
  data = sort_descending.delete_if do |row|
    row_date = row[0]
    row_date > trim_date
  end
  Quandl::Data.new(data)
end

#trim_end!(trim_date) ⇒ Object



69
70
71
72
# File 'lib/quandl/data/operations.rb', line 69

def trim_end!(trim_date)
  @data_array = trim_end(trim_date).data_array
  self
end

#trim_start(trim_date) ⇒ Object



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

def trim_start(trim_date)
  # date format
  trim_date = Date.parse(trim_date) if trim_date.is_a?(String)
  trim_date = trim_date.jd if trim_date.respond_to?(:jd)
  # find index
  return self unless trim_date.is_a?(Integer)
  # reject rows with dates less than
  data = sort_descending.delete_if do |row|
    row_date = row[0]
    row_date < trim_date
  end
  Quandl::Data.new(data)
end

#trim_start!(trim_date) ⇒ Object



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

def trim_start!(trim_date)
  @data_array = trim_start(trim_date).data_array
  self
end