Class: RelaxDB::HasManyProxy
- Inherits:
-
Object
- Object
- RelaxDB::HasManyProxy
- Includes:
- Enumerable
- Defined in:
- lib/relaxdb/has_many_proxy.rb
Instance Attribute Summary collapse
-
#children ⇒ Object
Returns the value of attribute children.
Instance Method Summary collapse
- #<<(obj) ⇒ Object
- #[](*args) ⇒ Object
- #break_back_link(obj) ⇒ Object
- #clear ⇒ Object
- #delete(obj) ⇒ Object
- #each(&blk) ⇒ Object
- #empty? ⇒ Boolean
- #first ⇒ Object
-
#initialize(client, relationship, opts) ⇒ HasManyProxy
constructor
A new instance of HasManyProxy.
- #inspect ⇒ Object
- #last ⇒ Object
- #load_children ⇒ Object
- #reload ⇒ Object
- #size ⇒ Object
Constructor Details
#initialize(client, relationship, opts) ⇒ HasManyProxy
Returns a new instance of HasManyProxy.
9 10 11 12 13 14 15 16 17 18 |
# File 'lib/relaxdb/has_many_proxy.rb', line 9 def initialize(client, relationship, opts) @client = client @relationship = relationship @opts = opts @target_class = opts[:class] @relationship_as_viewed_by_target = (opts[:known_as] || client.class.name.snake_case).to_s @children = load_children end |
Instance Attribute Details
#children ⇒ Object
Returns the value of attribute children.
7 8 9 |
# File 'lib/relaxdb/has_many_proxy.rb', line 7 def children @children end |
Instance Method Details
#<<(obj) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/relaxdb/has_many_proxy.rb', line 20 def <<(obj) return false if @children.include?(obj) obj.send("#{@relationship_as_viewed_by_target}=".to_sym, @client) if obj.save @children << obj self else false end end |
#[](*args) ⇒ Object
59 60 61 |
# File 'lib/relaxdb/has_many_proxy.rb', line 59 def [](*args) @children[*args] end |
#break_back_link(obj) ⇒ Object
44 45 46 47 48 49 |
# File 'lib/relaxdb/has_many_proxy.rb', line 44 def break_back_link(obj) if obj obj.send("#{@relationship_as_viewed_by_target}=".to_sym, nil) obj.save end end |
#clear ⇒ Object
32 33 34 35 36 37 |
# File 'lib/relaxdb/has_many_proxy.rb', line 32 def clear @children.each do |c| break_back_link c end @children.clear end |
#delete(obj) ⇒ Object
39 40 41 42 |
# File 'lib/relaxdb/has_many_proxy.rb', line 39 def delete(obj) obj = @children.delete(obj) break_back_link(obj) if obj end |
#each(&blk) ⇒ Object
71 72 73 |
# File 'lib/relaxdb/has_many_proxy.rb', line 71 def each(&blk) @children.each(&blk) end |
#empty? ⇒ Boolean
51 52 53 |
# File 'lib/relaxdb/has_many_proxy.rb', line 51 def empty? @children.empty? end |
#first ⇒ Object
63 64 65 |
# File 'lib/relaxdb/has_many_proxy.rb', line 63 def first @children[0] end |
#inspect ⇒ Object
91 92 93 |
# File 'lib/relaxdb/has_many_proxy.rb', line 91 def inspect @children.inspect end |
#last ⇒ Object
67 68 69 |
# File 'lib/relaxdb/has_many_proxy.rb', line 67 def last @children[size-1] end |
#load_children ⇒ Object
79 80 81 82 |
# File 'lib/relaxdb/has_many_proxy.rb', line 79 def load_children view_name = "#{@client.class}_#{@relationship}" @children = RelaxDB.view(view_name, :key => @client._id) end |
#reload ⇒ Object
75 76 77 |
# File 'lib/relaxdb/has_many_proxy.rb', line 75 def reload @children = load_children end |
#size ⇒ Object
55 56 57 |
# File 'lib/relaxdb/has_many_proxy.rb', line 55 def size @children.size end |