Sha256: 690bdd20c04d4017dad6213e358cb951b343ae8d4e3179b25b2cc1f76eb10ca6

Contents?: true

Size: 954 Bytes

Versions: 6

Compression:

Stored size: 954 Bytes

Contents

module InfoparkComponentCache
  # @author Tomasz Przedmojski <tomasz.przedmojski@infopark.de>
  #
  # Cache entity (component) consists of an obj, a name
  # and some parameters (hash). It should be used to point
  # to some data stored in a particular context
  class Component
    attr_reader :obj, :component, :params

    def initialize(obj, component, params = {})
      @obj = obj
      @component = component
      @params = params
    end

    def name
      component
    end

    def cache_key(meta_prefix = nil)
      if meta_prefix
        meta_prefix + "_" + KeyGenerator.generate_key(identity_hash)
      else
        KeyGenerator.generate_key(identity_hash)
      end
    end

    def identity_hash
      @params
        .except(:obj) # avoid the (ruby) object_id as part of the cache key (to_s), because it will change for each request
        .merge({ obj_name: @obj.name, obj_id: @obj.id, obj_component: @component })
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
infopark_component_cache-5.0.2 lib/infopark_component_cache/component.rb
infopark_component_cache-5.0.1 lib/infopark_component_cache/component.rb
infopark_component_cache-4.2.0 lib/infopark_component_cache/component.rb
infopark_component_cache-4.1.0 lib/infopark_component_cache/component.rb
infopark_component_cache-4.0.1 lib/infopark_component_cache/component.rb
infopark_component_cache-4.0.0 lib/infopark_component_cache/component.rb