Sha256: 4dc3989f800a5b99294272c856f362a6d2bb36b617d538929e15ccd55e1402a1

Contents?: true

Size: 1.97 KB

Versions: 2

Compression:

Stored size: 1.97 KB

Contents

# encoding: utf-8
module Mongoid  #:nodoc:
  module Tracking
    # Reader methods (previously known as "accessors")
    module Readers



      # Access methods
      def today
        whole_data_for(Time.now)
      end

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

      def first_value
        data_for(first_date)
      end

      def last_value
        data_for(last_date)
      end

      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

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

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

      def all_values_total
        return all_values.sum.to_i if all_values && !all_values.nil?
        return 0
      end

      # Utility methods
      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

      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

      # 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:  {"$unset"=>{"visits_data.14803.22"=>1}
      #
      #  { :visits_data => {"14803" => {} } }
      #
      # We can NOT retrieve the first date with visits_data.keys.min
      #
      def date_cleanup
        @data.reject! {|k,v| v.count == 0}
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
trackoid_mongoid4-0.1.4 lib/mongoid/tracking/readers.rb
trackoid_mongoid4-0.1.3 lib/mongoid/tracking/readers.rb