Class: IdentityCache::Cached::AttributeByOne

Inherits:
Attribute
  • Object
show all
Defined in:
lib/identity_cache/cached/attribute_by_one.rb

Instance Attribute Summary collapse

Attributes inherited from Attribute

#alias_name, #key_fields, #model, #unique

Instance Method Summary collapse

Methods inherited from Attribute

#attribute, #cache_encode, #cache_key, #expire, #fetch, #fetch_multi, #load_multi_from_db, #load_one_from_db

Constructor Details

#initializeAttributeByOne

Returns a new instance of AttributeByOne.



8
9
10
11
# File 'lib/identity_cache/cached/attribute_by_one.rb', line 8

def initialize(*)
  super
  @key_field = key_fields.first
end

Instance Attribute Details

#key_fieldObject (readonly)

Returns the value of attribute key_field.



6
7
8
# File 'lib/identity_cache/cached/attribute_by_one.rb', line 6

def key_field
  @key_field
end

Instance Method Details

#buildObject



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/identity_cache/cached/attribute_by_one.rb', line 13

def build
  cached_attribute = self

  model.define_singleton_method(:"fetch_#{fetch_method_suffix}") do |key|
    raise_if_scoped
    cached_attribute.fetch(key)
  end

  model.define_singleton_method(:"fetch_multi_#{fetch_method_suffix}") do |keys|
    raise_if_scoped
    cached_attribute.fetch_multi(keys)
  end
end