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
- #size ⇒ Object
Constructor Details
#initialize(client, relationship, opts) ⇒ ReferencesManyProxy
Returns a new instance of ReferencesManyProxy.
7 8 9 10 11 12 13 |
# 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 end |
Instance Method Details
#<<(obj, reciprocal_invocation = false) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/relaxdb/references_many_proxy.rb', line 15 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
68 69 70 71 |
# File 'lib/relaxdb/references_many_proxy.rb', line 68 def [](*args) resolve @peers[*args] end |
#clear ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/relaxdb/references_many_proxy.rb', line 32 def clear resolve @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
46 47 48 49 50 51 52 53 |
# File 'lib/relaxdb/references_many_proxy.rb', line 46 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
55 56 57 58 |
# File 'lib/relaxdb/references_many_proxy.rb', line 55 def delete_from_self(obj) @peers.delete(obj) if @peers peer_ids.delete(obj._id) end |
#each(&blk) ⇒ Object
73 74 75 76 |
# File 'lib/relaxdb/references_many_proxy.rb', line 73 def each(&blk) resolve @peers.each(&blk) end |
#empty? ⇒ Boolean
60 61 62 |
# File 'lib/relaxdb/references_many_proxy.rb', line 60 def empty? peer_ids.empty? end |
#inspect ⇒ Object
78 79 80 |
# File 'lib/relaxdb/references_many_proxy.rb', line 78 def inspect @client.instance_variable_get("@#{@relationship}".to_sym).inspect end |
#size ⇒ Object
64 65 66 |
# File 'lib/relaxdb/references_many_proxy.rb', line 64 def size peer_ids.size end |