Sha256: 50dc11e72063646528d84553331241eec1b852a5964cded302716d37dfee52a1

Contents?: true

Size: 1.23 KB

Versions: 9

Compression:

Stored size: 1.23 KB

Contents

# encoding: utf-8
module Mongoid  #:nodoc:
  module Tracking
    # This internal class handles all interaction of an aggregation token.
    class TrackerAggregates

      def initialize(owner, token, key_selector, track_field = nil)
        @owner, @token = owner, token
        @key = key_selector.first
        @track_field = track_field

        @accessor = @owner.class.send(:internal_accessor_name, @token)
        @selector = {:ns => @token}
        @selector.merge!(:key => @key) if @key

        @criteria = @owner.send(@accessor).where(@selector)
      end

      # Delegate all missing methods to the underlying Mongoid Criteria
      def method_missing(name, *args, &block)
        @criteria.send(name)
      end

      # Define all readers here. Basically we are delegating to the Track
      # object for every object in the criteria
      Readers.instance_methods.each {|name|
        define_method(name) do |*args|
          return nil unless @track_field
          if @key
            res = @criteria.first
            res.send(@track_field).send(name, *args) if res
          else
            @criteria.collect {|c|
              [c.key, c.send(@track_field).send(name, *args)]
            }
          end
        end
      }

    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
trackoid-0.3.8 lib/trackoid/tracker_aggregates.rb
trackoid-0.3.7 lib/trackoid/tracker_aggregates.rb
trackoid-0.3.6 lib/trackoid/tracker_aggregates.rb
trackoid-0.3.5 lib/trackoid/tracker_aggregates.rb
trackoid-0.3.4 lib/trackoid/tracker_aggregates.rb
trackoid-0.3.3 lib/trackoid/tracker_aggregates.rb
trackoid-0.3.2 lib/trackoid/tracker_aggregates.rb
trackoid-0.3.1 lib/trackoid/tracker_aggregates.rb
trackoid-0.3.0 lib/trackoid/tracker_aggregates.rb