Class: RelaxDB::ReferencesManyProxy
- Inherits:
-
Object
- Object
- RelaxDB::ReferencesManyProxy
- Includes:
- Enumerable
- Defined in:
- lib/relaxdb/references_many_proxy.rb
Instance Method Summary collapse
- #<<(obj, reciprocal_invocation = false) ⇒ Object
- #[](*args) ⇒ Object
- #clear ⇒ Object
- #delete(obj) ⇒ Object
- #delete_from_self(obj) ⇒ Object
- #each(&blk) ⇒ Object
- #empty? ⇒ Boolean
-
#initialize(client, relationship, opts) ⇒ ReferencesManyProxy
constructor
A new instance of ReferencesManyProxy.
- #inspect ⇒ Object
- #peer_ids ⇒ Object (also: #to_id_a)
- #size ⇒ Object
Constructor Details
#initialize(client, relationship, opts) ⇒ ReferencesManyProxy
Returns a new instance of ReferencesManyProxy.
7 8 9 10 11 12 13 14 15 |
# File 'lib/relaxdb/references_many_proxy.rb', line 7 def initialize(client, relationship, opts) @client = client @relationship = relationship @target_class = opts[:class] @relationship_as_viewed_by_target = opts[:known_as].to_s @peers = resolve end |
Instance Method Details
#<<(obj, reciprocal_invocation = false) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/relaxdb/references_many_proxy.rb', line 17 def <<(obj, reciprocal_invocation=false) return false if peer_ids.include? obj._id @peers << obj if @peers peer_ids << obj._id unless reciprocal_invocation # Set the other side of the relationship, ensuring this method isn't called again obj.send(@relationship_as_viewed_by_target).send(:<<, @client, true) # Bulk save to ensure relationship is persisted on both sides RelaxDB.bulk_save! @client, obj end self end |
#[](*args) ⇒ Object
69 70 71 |
# File 'lib/relaxdb/references_many_proxy.rb', line 69 def [](*args) @peers[*args] end |
#clear ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/relaxdb/references_many_proxy.rb', line 34 def clear @peers.each do |peer| peer.send(@relationship_as_viewed_by_target).send(:delete_from_self, @client) end # Important to resolve in the database before in memory, although an examination of the # contents of the bulk_save will look wrong as this object will still list all its peers RelaxDB.bulk_save(@client, *@peers) peer_ids.clear @peers.clear end |
#delete(obj) ⇒ Object
47 48 49 50 51 52 53 54 |
# File 'lib/relaxdb/references_many_proxy.rb', line 47 def delete(obj) deleted = obj.send(@relationship_as_viewed_by_target).send(:delete_from_self, @client) if deleted delete_from_self(obj) RelaxDB.bulk_save(@client, obj) end deleted end |
#delete_from_self(obj) ⇒ Object
56 57 58 59 |
# File 'lib/relaxdb/references_many_proxy.rb', line 56 def delete_from_self(obj) @peers.delete(obj) peer_ids.delete(obj._id) end |
#each(&blk) ⇒ Object
73 74 75 |
# File 'lib/relaxdb/references_many_proxy.rb', line 73 def each(&blk) @peers.each(&blk) end |
#empty? ⇒ Boolean
61 62 63 |
# File 'lib/relaxdb/references_many_proxy.rb', line 61 def empty? peer_ids.empty? end |
#inspect ⇒ Object
77 78 79 |
# File 'lib/relaxdb/references_many_proxy.rb', line 77 def inspect @client.instance_variable_get("@#{@relationship}".to_sym).inspect end |
#peer_ids ⇒ Object Also known as: to_id_a
81 82 83 |
# File 'lib/relaxdb/references_many_proxy.rb', line 81 def peer_ids @client.instance_variable_get("@#{@relationship}".to_sym) end |
#size ⇒ Object
65 66 67 |
# File 'lib/relaxdb/references_many_proxy.rb', line 65 def size peer_ids.size end |