Sha256: c95a46b9fca10542f48ebc2009e2da2fb214d1987cd710b3aaca72a286233f2e

Contents?: true

Size: 654 Bytes

Versions: 1

Compression:

Stored size: 654 Bytes

Contents

require 'request_store'
require 'active_model_cachers/cache_service'

module ActiveModelCachers
  class CacheServiceFactory
    class << self
      def create(reflect, cache_key, &query)
        klass = Class.new(CacheService)

        class << klass
          def instance(id)
            hash = (RequestStore.store[self] ||= {})
            return hash[id] ||= new(id)
          end

          def [](id)
            instance(id)
          end
        end

        klass.send(:define_method, :cache_key){ "#{cache_key}_#{@id}" }
        klass.send(:define_method, :get_without_cache){ query.call(@id) }
        return klass
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
active_model_cachers-1.0.0 lib/active_model_cachers/cache_service_factory.rb