Class: MongoidTraffic::Logger

Inherits:
Object
  • Object
show all
Defined in:
lib/mongoid_traffic/logger.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(log_cls, options = {}) ⇒ Logger


12
13
14
15
16
17
18
19
# File 'lib/mongoid_traffic/logger.rb', line 12

def initialize(log_cls, options = {})
  @log_cls = log_cls
  @selector = log_cls.criteria.selector
  @time_scope = options.fetch(:time_scope, TIME_SCOPE_OPTIONS.keys)
  @additonal_counters = options.except(:time_scope, :scope)

  raise "Invalid time scope definition: #{time_scope}" unless time_scope.all? { |ts| TIME_SCOPE_OPTIONS.key?(ts) }
end

Instance Attribute Details

#additonal_countersObject

Returns the value of attribute additonal_counters


6
7
8
# File 'lib/mongoid_traffic/logger.rb', line 6

def additonal_counters
  @additonal_counters
end

#log_clsObject

Returns the value of attribute log_cls


3
4
5
# File 'lib/mongoid_traffic/logger.rb', line 3

def log_cls
  @log_cls
end

#selectorObject

Returns the value of attribute selector


4
5
6
# File 'lib/mongoid_traffic/logger.rb', line 4

def selector
  @selector
end

#time_scopeObject

Returns the value of attribute time_scope


5
6
7
# File 'lib/mongoid_traffic/logger.rb', line 5

def time_scope
  @time_scope
end

Class Method Details

.log(log_cls, *args) ⇒ Object


8
9
10
# File 'lib/mongoid_traffic/logger.rb', line 8

def self.log(log_cls, *args)
  new(log_cls, *args).log
end

Instance Method Details

#logObject


21
22
23
24
25
26
27
# File 'lib/mongoid_traffic/logger.rb', line 21

def log
  time_scope.each do |ts|
    log_cls.collection
           .find(find_query(ts))
           .update_many(upsert_query(ts), upsert: true)
  end
end