Class: FacelessObj

Inherits:
Object
  • Object
show all
Defined in:
lib/cache_ninja/faceless_obj.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object) ⇒ FacelessObj

Returns a new instance of FacelessObj.



4
5
6
7
8
9
10
# File 'lib/cache_ninja/faceless_obj.rb', line 4

def initialize(object)
  @attributes = extract_attributes(object)
  @primary_key = extract_primary_key(object)
  @klass = extract_class(object)
  create_getters
  create_association_data_getters
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



2
3
4
# File 'lib/cache_ninja/faceless_obj.rb', line 2

def attributes
  @attributes
end

#klassObject (readonly)

Returns the value of attribute klass.



2
3
4
# File 'lib/cache_ninja/faceless_obj.rb', line 2

def klass
  @klass
end

#primary_keyObject (readonly)

Returns the value of attribute primary_key.



2
3
4
# File 'lib/cache_ninja/faceless_obj.rb', line 2

def primary_key
  @primary_key
end

Instance Method Details

#cacheable_dataObject



30
31
32
33
34
35
36
# File 'lib/cache_ninja/faceless_obj.rb', line 30

def cacheable_data
  {
    attributes: attributes,
    primary_key: primary_key,
    klass: klass
  }
end

#create_association_data_gettersObject



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/cache_ninja/faceless_obj.rb', line 18

def create_association_data_getters
  return unless klass.respond_to?(:cached_associations)

  klass.cached_associations.each do |association|
    define_singleton_method("cached_#{association}") do
      cached_data = klass.new(attributes).send("cached_#{association}")
      return cached_data if cached_data.present?
      klass.new(attributes).send(association)
    end
  end
end

#create_gettersObject



12
13
14
15
16
# File 'lib/cache_ninja/faceless_obj.rb', line 12

def create_getters
  attributes.each do |key, value|
    define_singleton_method(key) { value }
  end
end