Class: RelaxDB::ReferencesManyProxy

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/relaxdb/references_many_proxy.rb

Instance Method Summary collapse

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

#clearObject



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

Returns:

  • (Boolean)


61
62
63
# File 'lib/relaxdb/references_many_proxy.rb', line 61

def empty?
  peer_ids.empty?
end

#inspectObject



77
78
79
# File 'lib/relaxdb/references_many_proxy.rb', line 77

def inspect
  @client.instance_variable_get("@#{@relationship}".to_sym).inspect
end

#peer_idsObject 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

#sizeObject



65
66
67
# File 'lib/relaxdb/references_many_proxy.rb', line 65

def size
  peer_ids.size
end