Sha256: 5db9b5475df8b5a2de13e616bc4b80e34d45db339ab85303df1a471e52f60f35

Contents?: true

Size: 1.12 KB

Versions: 1

Compression:

Stored size: 1.12 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
      "#{self.class.name}/#{self.class.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

      # TODO: Make this some kind of private helper
      def cache_options
        options = { expires_in: cache_ttl }
        options.tap(&:compact!)
      end

      def call(*args)
        ret = new(*args)
        cache_store.fetch(ret.cache_key, cache_options) do
          ret.tap(&:call)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
business_flow-0.5.0 lib/business_flow/cacheable.rb