Module: Mongoid::Tracking::Readers

Included in:
Tracker
Defined in:
lib/mongoid/tracking/readers.rb

Overview

Reader methods (previously known as “accessors”)

Instance Method Summary collapse

Instance Method Details

#all_valuesObject



38
39
40
# File 'lib/mongoid/tracking/readers.rb', line 38

def all_values
  on(first_date..last_date) if first_date
end

#date_cleanupObject

We need the cleanup method only for methods who rely on date indexes to be valid (well formed) like first/last_date. This is because Mongo update operations cleans up the last key, which in our case left the array in an inconsistent state.

Example: Before update:

{ :visits_data => {"14803" => {"22" => 1} } }

After updating with: Mongoid::Tracking::Readers.“$unset”=>{“visits_data“$unset”=>{“visits_data.14803“$unset”=>{“visits_data.14803.22”=>1

{ :visits_data => {"14803" => {} } }

We can NOT retrieve the first date with visits_data.keys.min



73
74
75
# File 'lib/mongoid/tracking/readers.rb', line 73

def date_cleanup
  @data.reject! {|k,v| v.count == 0}
end

#first_dateObject

Utility methods



43
44
45
46
47
48
# File 'lib/mongoid/tracking/readers.rb', line 43

def first_date
  date_cleanup
  return nil unless _ts = @data.keys.min
  return nil unless _h = @data[_ts].keys.min
  Time.from_key(_ts, _h)
end

#first_valueObject



16
17
18
# File 'lib/mongoid/tracking/readers.rb', line 16

def first_value
  data_for(first_date)
end

#last_dateObject



50
51
52
53
54
55
# File 'lib/mongoid/tracking/readers.rb', line 50

def last_date
  date_cleanup
  return nil unless _ts = @data.keys.max
  return nil unless _h = @data[_ts].keys.max
  Time.from_key(_ts, _h)
end

#last_days(how_much = 7) ⇒ Object



24
25
26
27
28
# File 'lib/mongoid/tracking/readers.rb', line 24

def last_days(how_much = 7)
  return [today] unless how_much > 0
  now, hmd = Time.now, (how_much - 1)
  on( now.ago(hmd.days)..now )
end

#last_valueObject



20
21
22
# File 'lib/mongoid/tracking/readers.rb', line 20

def last_value
  data_for(last_date)
end

#on(date) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/mongoid/tracking/readers.rb', line 30

def on(date)
  if date.is_a?(Range)
    whole_data_for_range(date)
  else
    whole_data_for(date)
  end
end

#todayObject

Access methods



8
9
10
# File 'lib/mongoid/tracking/readers.rb', line 8

def today
  whole_data_for(Time.now)
end

#yesterdayObject



12
13
14
# File 'lib/mongoid/tracking/readers.rb', line 12

def yesterday
  whole_data_for(Time.now - 1.day)
end