Sha256: 3cc916648f0b9583181646c21ce780810813beb6081033a815be7b5bee890ed0
Contents?: true
Size: 1.54 KB
Versions: 2
Compression:
Stored size: 1.54 KB
Contents
module BusinessFlow # Extends the DSL to support caching of completed processes module Cacheable def self.included(klass) klass.extend(ClassMethods) end def cache_key klass = self.class "#{klass.name}/#{klass.cache_key.call(self, nil)}" end # DSL Methods module ClassMethods def cache_store(store = nil) if store @cache_store = store else @cache_store ||= ActiveSupport::Cache::MemoryStore.new end end def cache_ttl(ttl = nil) if ttl @cache_ttl = ttl else @cache_ttl end end def cache_key(key = nil) if key @cache_key = Callable.new(key, self) else @cache_key ||= Callable.new(:parameter_object, self) end end def call(*args) flow = new(*args) PrivateHelpers.execute(self, flow) rescue FlowFailedException flow end end # Avoid polluting the namespace of whoever includes us module PrivateHelpers def self.execute(klass, flow) klass.cache_store.fetch(flow.cache_key, cache_options(klass)) do flow.call raise FlowFailedException, flow if flow.errors.any? flow end end def self.cache_options(klass) # compact is not available in Ruby <2.4 or ActiveSupport < 4, so # we can't use it here. options = {} ttl = klass.cache_ttl options[:expires_in] = ttl if ttl options end end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
business_flow-0.6.0 | lib/business_flow/cacheable.rb |
business_flow-0.5.2 | lib/business_flow/cacheable.rb |