Module: CachedRecord::ORM::DataMapper::InstanceMethods
- Defined in:
- lib/cached_record/orm/data_mapper.rb
Instance Method Summary collapse
- #cache_attributes ⇒ Object
- #cache_foreign_key(name, relationship, value) ⇒ Object
- #cache_foreign_keys ⇒ Object
Instance Method Details
#cache_attributes ⇒ Object
64 65 66 |
# File 'lib/cached_record/orm/data_mapper.rb', line 64 def cache_attributes ([:only] ? as_json.slice(*[:only]) : as_json).symbolize_keys!.merge cache_foreign_keys end |
#cache_foreign_key(name, relationship, value) ⇒ Object
73 74 75 76 77 78 79 80 81 82 |
# File 'lib/cached_record/orm/data_mapper.rb', line 73 def cache_foreign_key(name, relationship, value) case relationship when ::DataMapper::Associations::ManyToOne::Relationship {:"#{relationship.child_key.first.name}" => value.try(:id)} when ::DataMapper::Associations::OneToOne::Relationship {:"_#{name.to_s.singularize}_id" => value.try(:id)} when ::DataMapper::Associations::OneToMany::Relationship, ::DataMapper::Associations::ManyToMany::Relationship {:"_#{name.to_s.singularize}_ids" => value.collect(&:id)} end end |
#cache_foreign_keys ⇒ Object
67 68 69 70 71 72 |
# File 'lib/cached_record/orm/data_mapper.rb', line 67 def cache_foreign_keys ([:include] || {}).inject({}) do |json, name| relationship = relationships[name] json.merge cache_foreign_key(name, relationship, send(name)) end end |