Class: Quandl::Cassandra::Models::Column::Read::Offset

Inherits:
Quandl::Cassandra::Models::Column::Read show all
Defined in:
lib/quandl/cassandra/models/column/read/offset.rb

Instance Method Summary collapse

Methods inherited from Quandl::Cassandra::Models::Column::Read

#column_collapses, #column_ids, #column_ids=, #count?, perform

Instance Method Details

#accuracyObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/quandl/cassandra/models/column/read/offset.rb', line 64

def accuracy
  # did the query include an accuracy?
  accuracy = attributes[:accuracy]
  # otherwise guess at the accuracy by collapse
  if accuracy.blank?
    accuracy = case frequency.to_sym
    when :daily     then 0
    when :weekly    then 1
    when :monthly   then 1
    when :quarterly then 1
    when :annual    then 0
    else
      3
    end
  end
  accuracy
end

#accuracy_with_limitObject



58
59
60
61
62
# File 'lib/quandl/cassandra/models/column/read/offset.rb', line 58

def accuracy_with_limit
  awl = accuracy
  awl = awl + limit - 1 if limit && limit > 0
  awl
end

#apply_offsetObject



27
28
29
30
# File 'lib/quandl/cassandra/models/column/read/offset.rb', line 27

def apply_offset
  return unless attributes[:offset].present?
  order == :asc ? apply_offset_asc : apply_offset_desc
end

#apply_offset_ascObject



32
33
34
35
36
37
38
39
# File 'lib/quandl/cassandra/models/column/read/offset.rb', line 32

def apply_offset_asc
  # calculate ranges
  offset_start  = columns_trim_end.occurrences_of_frequency_ahead( offset, collapse_with_frequency ).start_of_frequency(collapse_with_frequency)
  offset_end    = offset_start.occurrences_of_frequency_ahead( accuracy_with_limit, collapse_with_frequency ).end_of_frequency( collapse_with_frequency ) if limit
  # set trims
  self.trim_start = offset_start
  self.trim_end   = offset_end if limit
end

#apply_offset_descObject



41
42
43
44
45
46
47
48
# File 'lib/quandl/cassandra/models/column/read/offset.rb', line 41

def apply_offset_desc
  # calculate ranges
  offset_start  = columns_trim_start.occurrences_of_frequency_ago( offset, collapse_with_frequency ).end_of_frequency(collapse_with_frequency)
  offset_end    = offset_start.occurrences_of_frequency_ago( accuracy_with_limit, collapse_with_frequency ).start_of_frequency( collapse_with_frequency ) if limit
  # set trims
  self.trim_start = offset_end  if limit
  self.trim_end   = offset_start
end

#apply_offset_with_transformObject



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/quandl/cassandra/models/column/read/offset.rb', line 15

def apply_offset_with_transform
  # rdiff_from needs the data from the current to offset
  if attributes[:row].present? && attributes[:transform] == :rdiff_from
    # limit the results by trim 
    self.trim_start = columns_trim_start.occurrences_of_frequency_ago( attributes[:row], collapse_with_frequency ).end_of_frequency(collapse_with_frequency) if order == :desc
    self.trim_end   = columns_trim_end.occurrences_of_frequency_ahead( attributes[:row], collapse_with_frequency ).end_of_frequency(collapse_with_frequency) if order == :asc
    # the query should not limit or offset the data
    attributes[:limit] = nil
    attributes[:offset] = nil
  end
end

#collapse_with_frequencyObject



94
95
96
# File 'lib/quandl/cassandra/models/column/read/offset.rb', line 94

def collapse_with_frequency
  collapse == :source ? frequency : collapse
end

#columns_trim_endObject



54
55
56
# File 'lib/quandl/cassandra/models/column/read/offset.rb', line 54

def columns_trim_end
  @trim_end ||= Quandl::Cassandra::Column.find_max_time_by_ids(column_ids, column_collapses, "ASC")
end

#columns_trim_startObject



50
51
52
# File 'lib/quandl/cassandra/models/column/read/offset.rb', line 50

def columns_trim_start
  @trim_start ||= Quandl::Cassandra::Column.find_max_time_by_ids(column_ids, column_collapses, "DESC")
end

#frequencyObject



98
99
100
# File 'lib/quandl/cassandra/models/column/read/offset.rb', line 98

def frequency
  attributes[:frequency]
end

#limitObject



82
83
84
# File 'lib/quandl/cassandra/models/column/read/offset.rb', line 82

def limit
  @limit ||= attributes[:limit]
end

#offsetObject



86
87
88
# File 'lib/quandl/cassandra/models/column/read/offset.rb', line 86

def offset
  attributes[:offset]
end

#orderObject



90
91
92
# File 'lib/quandl/cassandra/models/column/read/offset.rb', line 90

def order
  @order ||= attributes[:order] == :asc ? :asc : :desc
end

#performObject



3
4
5
6
7
8
9
# File 'lib/quandl/cassandra/models/column/read/offset.rb', line 3

def perform
  # skip strategy unless all required attriutes are present
  return unless required_attributes_present?
  # apply strategy
  apply_offset_with_transform
  apply_offset
end

#required_attributes_present?Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/quandl/cassandra/models/column/read/offset.rb', line 11

def required_attributes_present?
  column_ids.present? && column_collapses.present? && columns_trim_start.is_a?(Date) && columns_trim_end.is_a?(Date)
end