Browse files

standardize initialization

  • Loading branch information...
1 parent 948e3bb commit d03a4646208edf95703c0e56ddf9f387a08bc079 @seamusabshere committed Nov 17, 2011
Showing with 13 additions and 29 deletions.
  1. +2 −2 lib/cache_method.rb
  2. +7 −12 lib/cache_method/cached_result.rb
  3. +4 −15 lib/cache_method/epoch.rb
View
4 lib/cache_method.rb
@@ -31,7 +31,7 @@ module InstanceMethods
# my_blog.clear_method_cache :get_latest_entries
def clear_method_cache(method_id)
if ::CacheMethod.config.generational?
- ::CacheMethod::Epoch.mark_passing :obj => self, :method_id => method_id
+ ::CacheMethod::Epoch.new(self, method_id).mark_passing
else
raise ::RuntimeError, "[cache_method] clear_method_cache called, but you have disabled generational caching. Check your setting for CacheMethod.config.generational"
end
@@ -61,7 +61,7 @@ def cache_method(method_id, ttl = nil)
original_method_id = "_uncached_#{method_id}"
alias_method original_method_id, method_id
define_method method_id do |*args|
- ::CacheMethod::CachedResult.fetch :obj => self, :method_id => method_id, :original_method_id => original_method_id, :ttl => ttl, :args => args
+ ::CacheMethod::CachedResult.new(self, method_id, original_method_id, ttl, args).fetch
end
end
end
View
19 lib/cache_method/cached_result.rb
@@ -1,17 +1,12 @@
require 'digest/md5'
module CacheMethod
class CachedResult #:nodoc: all
- class << self
- def fetch(options = {})
- cached_result = new options
- cached_result.fetch
- end
- end
-
- def initialize(options = {})
- options.each do |k, v|
- instance_variable_set "@#{k}", v
- end
+ def initialize(obj, method_id, original_method_id, ttl, args)
+ @obj = obj
+ @method_id = method_id
+ @original_method_id = original_method_id
+ @ttl = ttl
+ @args = args
end
attr_reader :obj
@@ -56,7 +51,7 @@ def args_digest
def current_epoch
if Config.instance.generational?
- @current_epoch ||= Epoch.current(:obj => obj, :method_id => method_id)
+ @current_epoch ||= Epoch.new(obj, method_id).current
end
end
end
View
19 lib/cache_method/epoch.rb
@@ -1,25 +1,14 @@
module CacheMethod
class Epoch #:nodoc: all
class << self
- def current(options = {})
- epoch = new options
- epoch.current
- end
-
- def mark_passing(options = {})
- epoch = new options
- epoch.mark_passing
- end
-
def random_name
- rand(1_000_000).to_s
+ rand(100_000_000).to_s
end
end
- def initialize(options = {})
- options.each do |k, v|
- instance_variable_set "@#{k}", v
- end
+ def initialize(obj, method_id)
+ @obj = obj
+ @method_id = method_id
end
attr_reader :obj

0 comments on commit d03a464

Please sign in to comment.