module Mongoid::TaggableWithContext::AggregationStrategy::MapReduce

Protected Instance Methods

changed_tag_arrays() click to toggle source
# File lib/mongoid/taggable_with_context/aggregation_strategy/map_reduce.rb, line 35
def changed_tag_arrays
  self.class.tag_database_fields & changes.keys.map(&:to_sym)
end
map_reduce_all_contexts!() click to toggle source
# File lib/mongoid/taggable_with_context/aggregation_strategy/map_reduce.rb, line 43
def map_reduce_all_contexts!
  self.class.tag_contexts.each do |context|
    map_reduce_context!(context)
  end
end
map_reduce_context!(context) click to toggle source
# File lib/mongoid/taggable_with_context/aggregation_strategy/map_reduce.rb, line 49
    def map_reduce_context!(context)
      db_field = self.class.tag_options_for(context)[:db_field]

      map = <<-END
        function() {
          if (!this.#{db_field})return;
          for (index in this.#{db_field})
            emit(this.#{db_field}[index], 1);
        }
      END

      reduce = <<-END
        function(key, values) {
          var count = 0;
          for (index in values) count += values[index];
          return count;
        }
      END

      self.class.map_reduce(map, reduce).out(replace: aggregation_collection_for(context)).time
    end
tags_changed?() click to toggle source
# File lib/mongoid/taggable_with_context/aggregation_strategy/map_reduce.rb, line 39
def tags_changed?
  !changed_tag_arrays.empty?
end