Class: FacelessObj
- Inherits:
-
Object
- Object
- FacelessObj
- Defined in:
- lib/cache_ninja/faceless_obj.rb
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
-
#primary_key ⇒ Object
readonly
Returns the value of attribute primary_key.
Instance Method Summary collapse
- #cacheable_data ⇒ Object
- #create_association_data_getters ⇒ Object
- #create_getters ⇒ Object
-
#initialize(object) ⇒ FacelessObj
constructor
A new instance of FacelessObj.
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
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
2 3 4 |
# File 'lib/cache_ninja/faceless_obj.rb', line 2 def attributes @attributes end |
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
2 3 4 |
# File 'lib/cache_ninja/faceless_obj.rb', line 2 def klass @klass end |
#primary_key ⇒ Object (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_data ⇒ Object
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_getters ⇒ Object
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_getters ⇒ Object
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 |