Module: SBF::Client::Entities::Cacheable::ClassMethods

Defined in:
lib/stbaldricks/entities/lib/cacheable.rb

Instance Method Summary collapse

Instance Method Details

#cache_id(proc) ⇒ Object



17
18
19
20
21
# File 'lib/stbaldricks/entities/lib/cacheable.rb', line 17

def cache_id(proc)
  define_method(:cache_id) do
    instance_exec(&proc)
  end
end

#cache_id_from_hash(proc_or_hash) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/stbaldricks/entities/lib/cacheable.rb', line 23

def cache_id_from_hash(proc_or_hash)
  return cache_id_from_hash_func(proc_or_hash) if proc_or_hash.is_a?(Proc)
  raise CacheableNotImplementedError unless @cache_id_proc || superclass.respond_to?(:cache_id_from_hash)

  id = @cache_id_proc.call(proc_or_hash || {}) if @cache_id_proc
  id ||= superclass.cache_id_from_hash(proc_or_hash || {}) if superclass.respond_to?(:cache_id_from_hash)
  raise CacheableInvalidDataError, "Data missing id. Data: #{proc_or_hash.to_json}" unless id

  id
end