Class: MassiveRecord::ORM::Relations::Proxy::EmbedsMany
- Inherits:
-
MassiveRecord::ORM::Relations::ProxyCollection
- Object
- MassiveRecord::ORM::Relations::Proxy
- MassiveRecord::ORM::Relations::ProxyCollection
- MassiveRecord::ORM::Relations::Proxy::EmbedsMany
- Defined in:
- lib/massive_record/orm/relations/proxy/embeds_many.rb
Instance Attribute Summary
Attributes inherited from MassiveRecord::ORM::Relations::Proxy
#metadata, #proxy_owner, #proxy_target
Instance Method Summary collapse
-
#<<(*records) ⇒ Object
(also: #push, #concat)
Adding record(s) to the collection.
- #changed? ⇒ Boolean
- #changes ⇒ Object
- #find(id) ⇒ Object
-
#include?(record) ⇒ Boolean
Checks if record is included in collection.
- #length ⇒ Object (also: #count, #size)
- #limit(limit) ⇒ Object
-
#parent_has_been_saved! ⇒ Object
Hook to call when save is done through parent.
-
#parent_will_be_saved! ⇒ Object
Hook which are called just before save.
-
#proxy_targets_raw ⇒ Object
Returns the raw hash of attributes for embedded objects It filters away database_ids (keys in a column family) which it does not recognize.
- #proxy_targets_update_hash ⇒ Object
Methods inherited from MassiveRecord::ORM::Relations::ProxyCollection
#delete, #delete_all, #destroy, #destroy_all, #empty?, #first, #load_proxy_target, #replace, #reset
Methods inherited from MassiveRecord::ORM::Relations::Proxy
#blank?, #initialize, #inspect, #is_a?, #load_proxy_target, #loaded!, #loaded?, #method_missing, #reload, #replace, #reset, #respond_to?, #to_param
Constructor Details
This class inherits a constructor from MassiveRecord::ORM::Relations::Proxy
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class MassiveRecord::ORM::Relations::Proxy
Instance Method Details
#<<(*records) ⇒ Object Also known as: push, concat
Adding record(s) to the collection.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/massive_record/orm/relations/proxy/embeds_many.rb', line 24 def <<(*records) records.flatten.each do |record| unless include? record raise_if_type_mismatch(record) proxy_target << record record.send(.inverse_of).replace(proxy_owner, false) end end if proxy_owner.persisted? proxy_owner.save else proxy_target.sort_by! &:id end self end |
#changed? ⇒ Boolean
129 130 131 132 133 |
# File 'lib/massive_record/orm/relations/proxy/embeds_many.rb', line 129 def changed? to_be_destroyed.any? || proxy_target.any? do |record| record.new_record? || record.destroyed? || record.changed? end end |
#changes ⇒ Object
135 136 137 138 139 140 141 |
# File 'lib/massive_record/orm/relations/proxy/embeds_many.rb', line 135 def changes Hash[proxy_target.collect do |record| if record.changed? [record.id, record.changes] end end.compact] end |
#find(id) ⇒ Object
6 7 8 9 10 11 12 13 14 |
# File 'lib/massive_record/orm/relations/proxy/embeds_many.rb', line 6 def find(id) record = if loaded? || proxy_owner.new_record? proxy_target.detect { |record| record.id == id } else (id) end record or raise RecordNotFound.new("Could not find #{proxy_target_class.model_name} with id=#{id}") end |
#include?(record) ⇒ Boolean
Checks if record is included in collection
48 49 50 |
# File 'lib/massive_record/orm/relations/proxy/embeds_many.rb', line 48 def include?(record) load_proxy_target.include? record end |
#length ⇒ Object Also known as: count, size
52 53 54 |
# File 'lib/massive_record/orm/relations/proxy/embeds_many.rb', line 52 def length load_proxy_target.length end |
#limit(limit) ⇒ Object
16 17 18 |
# File 'lib/massive_record/orm/relations/proxy/embeds_many.rb', line 16 def limit(limit) load_proxy_target.slice(0, limit) end |
#parent_has_been_saved! ⇒ Object
Hook to call when save is done through parent
113 114 115 116 |
# File 'lib/massive_record/orm/relations/proxy/embeds_many.rb', line 113 def parent_has_been_saved! reload_raw_data proxy_targets_update_hash.clear end |
#parent_will_be_saved! ⇒ Object
Hook which are called just before save. It iterates over new or changed records, asking them to “save” themself. This will result in created_at / updated_at and persistence state being set. It will also build the proxy_targets_update_hash with these changes, which will be used at the proxy owner’s save for actually updating these records.
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/massive_record/orm/relations/proxy/embeds_many.rb', line 88 def parent_will_be_saved! # :nodoc: proxy_targets_update_hash.clear MassiveRecord::ORM::Persistence::Operations.suppress do proxy_target.each do |record| if record.destroyed? proxy_targets_update_hash[record.database_id] = nil elsif record.new_record? || record.changed? record.save(:validate => false) unless record.in_the_middle_of_saving? proxy_targets_update_hash[record.database_id] = Base.coder.dump(record.attributes_db_raw_data_hash) end end to_be_destroyed.each do |record| targets_current_owner = record.send(.inverse_of).proxy_target if targets_current_owner.nil? || targets_current_owner == proxy_owner record.destroy proxy_targets_update_hash[record.database_id] = nil end end to_be_destroyed.clear end end |
#proxy_targets_raw ⇒ Object
Returns the raw hash of attributes for embedded objects It filters away database_ids (keys in a column family) which it does not recognize.
67 68 69 70 71 72 73 74 75 |
# File 'lib/massive_record/orm/relations/proxy/embeds_many.rb', line 67 def proxy_targets_raw # :nodoc: Hash[proxy_owner.raw_data[.store_in].collect do |database_id, value| begin base_class, id = Embedded.parse_database_id(database_id) [id, value] if base_class == proxy_target_class.base_class.to_s.underscore rescue InvalidEmbeddedDatabaseId end end.compact] end |
#proxy_targets_update_hash ⇒ Object
118 119 120 |
# File 'lib/massive_record/orm/relations/proxy/embeds_many.rb', line 118 def proxy_targets_update_hash @proxy_targets_update_hash ||= {} end |