Class: MongoTree::Children

Inherits:
Array
  • Object
show all
Defined in:
lib/mongo_tree/children.rb

Instance Method Summary collapse

Constructor Details

#initialize(array, parent, &update_method) ⇒ Children

Returns a new instance of Children.



3
4
5
6
7
# File 'lib/mongo_tree/children.rb', line 3

def initialize(array, parent, &update_method)
  @parent = parent
  @update_method = block_given? ? update_method : Proc.new { |child, parent| child.parent_id = parent.id }
  super(array)
end

Instance Method Details

#<<(*docs) ⇒ Object Also known as: push, concat



9
10
11
12
13
14
15
16
17
# File 'lib/mongo_tree/children.rb', line 9

def <<(*docs)
  @parent.save if @parent.new?
  flatten_deeper(docs).collect do |doc|
    @update_method.call(doc, @parent)
    doc.save if doc.changed? || doc.new?
  end

  super.uniq
end