class Patterns::Calculation
Attributes
options[R]
subject[R]
Public Class Methods
new(*args)
click to toggle source
# File lib/patterns/calculation.rb, line 7 def initialize(*args) @options = args.extract_options! @subject = args.first end
result(*args, &block)
click to toggle source
# File lib/patterns/calculation.rb, line 12 def self.result(*args, &block) new(*args).cached_result(&block) end
Also aliased as: result_for, calculate
set_cache_expiry_every(period)
click to toggle source
# File lib/patterns/calculation.rb, line 21 def self.set_cache_expiry_every(period) self.cache_expiry_every = period end
Private Class Methods
hash_of(*args)
click to toggle source
# File lib/patterns/calculation.rb, line 47 def self.hash_of(*args) Digest::SHA1.hexdigest(args.map(&:to_s).join(':')) end
Public Instance Methods
cached_result(&block)
click to toggle source
# File lib/patterns/calculation.rb, line 25 def cached_result(&block) if cache_expiry_period.blank? result(&block) else Rails.cache.fetch(cache_key, expires_in: cache_expiry_period) do result(&block) end end end
Private Instance Methods
cache_expiry_period()
click to toggle source
# File lib/patterns/calculation.rb, line 55 def cache_expiry_period self.class.cache_expiry_every end
cache_key()
click to toggle source
# File lib/patterns/calculation.rb, line 43 def cache_key "#{self.class.name}_#{hash_of(subject, options)}" end
hash_of(*args)
click to toggle source
# File lib/patterns/calculation.rb, line 51 def hash_of(*args) self.class.hash_of(*args) end
result()
click to toggle source
# File lib/patterns/calculation.rb, line 39 def result raise NotImplementedError end