lib/minuteman.rb in minuteman-1.0.2 vs lib/minuteman.rb in minuteman-1.0.3

- old
+ new

@@ -1,25 +1,10 @@ require "redis" require "time" require "forwardable" require "minuteman/time_events" -# Until redis gem gets updated -class Redis - def bitop(operation, destkey, *keys) - synchronize do |client| - client.call([:bitop, operation, destkey] + keys) - end - end - - def bitcount(key, start = 0, stop = -1) - synchronize do |client| - client.call([:bitcount, key, start, stop]) - end - end -end - # Public: Minuteman core classs # class Minuteman extend Forwardable @@ -46,25 +31,13 @@ def initialize(options = {}) redis_options = options.delete(:redis) || {} self.options = default_options.merge!(options) self.redis = define_connection(redis_options) - end - # Public: Generates the methods to fech data - # - # event_name - The event name to be searched for - # date - A Time object used to do the search - # - %w[year month week day hour minute].each do |method_name| - define_method(method_name) do |*args| - event_name, date = *args - date ||= Time.now.utc - - constructor = self.class.const_get(method_name.capitalize) - constructor.new(event_name, date) - end + spans = self.options.fetch(:time_spans, %w[year month week day hour minute]) + @time_spans = generate_spans(spans) end # Public: Marks an id to a given event on a given time # # event_name - The event name to be searched for @@ -76,11 +49,11 @@ # analytics.track("login", 1) # analytics.track("login", [2, 3, 4]) # def track(event_name, ids, time = Time.now.utc) event_time = time.kind_of?(Time) ? time : Time.parse(time.to_s) - time_events = TimeEvents.start(event_name, event_time) + time_events = TimeEvents.start(@time_spans, event_name, event_time) track_events(time_events, Array(ids)) end # Public: List all the events given the minuteman namespace @@ -110,17 +83,33 @@ safe { redis.del(keys) } if keys.any? end private + # Public: Generates the methods to fech data + # + # spans: An array of timespans corresponding to a TimeSpan class + # + def generate_spans(spans) + spans.map do |method_name| + constructor = self.class.const_get(method_name.capitalize) + + define_singleton_method(method_name) do |*args| + event_name, date = *args + date ||= Time.now.utc + + constructor.new(event_name, date) + end + + constructor + end + end + # Private: Default configuration options # def default_options - { - cache: true, - silent: false - } + { cache: true, silent: false } end # Private: Determines to use or instance a Redis connection # # object: Can be the options to instance a Redis connection or a connection @@ -155,10 +144,8 @@ end # Private: The prefix key of all the operations # def operations_cache_key_prefix - [ - PREFIX, Minuteman::KeysMethods::BIT_OPERATION_PREFIX - ].join("_") + [ PREFIX, Minuteman::KeysMethods::BIT_OPERATION_PREFIX ].join("_") end end