module Quandl::Data::Operations extend ActiveSupport::Concern include Quandl::Operation module ClassMethods def forwardable_methods @forwardable_methods ||= (operations + operations.collect{|o| "#{o}!" if method_defined?("#{o}!") }).compact end def operations [ :to_csv, :to_jd, :to_date, :sort_order, :sort_ascending, :sort_descending, :transform, :collapse, :frequency, :parse ] end end def to_h data_array.inject({}) do |memo, row| memo[row[0]] = row[1..-1] memo end end 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 def to_jd! @data_array = Parse.to_jd( data_array ); self end def to_date! @data_array = Parse.to_date( data_array ); self end def to_date_str! @data_array = to_date!.collect{|r| r = r.dup; r[0] = r[0].to_s; r }; self end 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 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 def limit!(amount) @data_array = data_array[0..( amount.to_i - 1 )]; self end def sort_order(dir) dir == :asc ? sort_ascending! : sort_descending! end def sort_ascending! @data_array = Parse.sort_asc( data_array ); self end def sort_descending! @data_array = Parse.sort_desc( data_array ); self end def transform(*args) return @transform unless args.first.present? self.transform = args.first self end def transform=(value) return false unless Transform.valid?(value) @transform = value @data_array = Transform.perform( data_array, value ) end def collapse(*args) return @collapse unless args.first.present? self.collapse = args.first self end def collapse=(collapse) return false unless Collapse.valid?(collapse) @collapse = collapse @frequency = collapse @data_array = Collapse.perform( data_array, collapse ) end def frequency @frequency ||= Collapse.frequency?( data_array ) end def frequency=(value) @frequency = value.to_sym if value.present? end def clone Quandl::Data.new( data_array.dup ) end def to_jd clone.to_jd! end def to_date clone.to_date! end def to_date_str clone.to_date_str! end def trim_start(date) clone.trim_start!(date) end def trim_end(date) clone.trim_end!(date) end def limit(amount) clone.limit!(amount) end def sort_ascending clone.sort_ascending! end def sort_descending clone.sort_descending! end end