Sha256: 1fc0666ada99840e5caa5b30be6990daaef12b9bc554ee9357a21e73a2acea7a
Contents?: true
Size: 1.75 KB
Versions: 1
Compression:
Stored size: 1.75 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 key = Digest::SHA256.hexdigest(klass.cache_key.call(self, nil).to_s) "#{klass.name.underscore}/#{key}" end # DSL Methods module ClassMethods def cache_store(store = nil) if store @cache_store = store else @cache_store ||= if defined?(Rails) Rails.cache else ActiveSupport::Cache::MemoryStore.new end 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
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
business_flow-0.7.0 | lib/business_flow/cacheable.rb |