lib/tabs/resolution.rb in tabs-0.9.1 vs lib/tabs/resolution.rb in tabs-1.0.0
- old
+ new
@@ -1,15 +1,20 @@
module Tabs
module Resolution
- extend Resolutionable
+ include Resolutionable
extend self
- def register(resolution, klass)
+ def register(klass)
@@resolution_classes ||= {}
- @@resolution_classes[resolution] = klass
+ @@resolution_classes[klass.name] = klass
end
+ def unregister(resolutions)
+ resolutions = Array[resolutions].flatten
+ resolutions.each{ |res| @@resolution_classes.delete(res) }
+ end
+
def serialize(resolution, timestamp)
resolution_klass(resolution).serialize(timestamp)
end
def deserialize(resolution, str)
@@ -30,20 +35,31 @@
def all
@@resolution_classes.keys
end
+ def expire(resolution, key, timestamp)
+ resolution_klass(resolution).expire(key, timestamp)
+ end
+
+ def reset_default_resolutions
+ @@resolution_classes = {}
+ Tabs::Resolution.register(Tabs::Resolutions::Minute)
+ Tabs::Resolution.register(Tabs::Resolutions::Hour)
+ Tabs::Resolution.register(Tabs::Resolutions::Day)
+ Tabs::Resolution.register(Tabs::Resolutions::Week)
+ Tabs::Resolution.register(Tabs::Resolutions::Month)
+ Tabs::Resolution.register(Tabs::Resolutions::Year)
+ end
+
private
def resolution_klass(resolution)
- @@resolution_classes[resolution]
+ klass = @@resolution_classes[resolution]
+ raise Tabs::ResolutionMissingError.new(resolution) unless klass
+ klass
end
end
end
-Tabs::Resolution.register(:minute, Tabs::Resolutions::Minute)
-Tabs::Resolution.register(:hour, Tabs::Resolutions::Hour)
-Tabs::Resolution.register(:day, Tabs::Resolutions::Day)
-Tabs::Resolution.register(:week, Tabs::Resolutions::Week)
-Tabs::Resolution.register(:month, Tabs::Resolutions::Month)
-Tabs::Resolution.register(:year, Tabs::Resolutions::Year)
+Tabs::Resolution.reset_default_resolutions