Class: Arbre::ChildElementCollection
- Inherits:
-
ElementCollection
- Object
- ElementCollection
- Arbre::ChildElementCollection
- Defined in:
- lib/arbre/child_element_collection.rb
Overview
An element collection used to hold some other element’s children. Adds functionality to update a child’s parent when it is added, and supports inserting at specific positions in the collection.
Instance Attribute Summary collapse
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
Instance Method Summary collapse
-
#add(element) ⇒ Object
Adding and removing.
- #clear ⇒ Object
-
#eql?(other) ⇒ Boolean
Equality.
-
#initialize(parent) ⇒ ChildElementCollection
constructor
Initialization.
- #insert_after(existing, element) ⇒ Object
-
#insert_at(index, element) ⇒ Object
Inserting.
- #insert_before(existing, element) ⇒ Object
- #remove(element) ⇒ Object
Methods inherited from ElementCollection
#&, #+, #-, #<<, #==, #[], #concat, #to_a, #to_ary, #to_s
Constructor Details
#initialize(parent) ⇒ ChildElementCollection
Initialization
11 12 13 14 |
# File 'lib/arbre/child_element_collection.rb', line 11 def initialize(parent) super([]) @parent = parent end |
Instance Attribute Details
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
16 17 18 |
# File 'lib/arbre/child_element_collection.rb', line 16 def parent @parent end |
Instance Method Details
#add(element) ⇒ Object
Adding and removing
28 29 30 |
# File 'lib/arbre/child_element_collection.rb', line 28 def add(element) insert_at length, element end |
#clear ⇒ Object
39 40 41 42 |
# File 'lib/arbre/child_element_collection.rb', line 39 def clear @elements.each { |element| element.parent = nil } super end |
#eql?(other) ⇒ Boolean
Equality
21 22 23 |
# File 'lib/arbre/child_element_collection.rb', line 21 def eql?(other) super && parent == other.parent end |
#insert_after(existing, element) ⇒ Object
58 59 60 61 62 63 |
# File 'lib/arbre/child_element_collection.rb', line 58 def insert_after(existing, element) index = @elements.index(existing) or raise ArgumentError, "existing element #{existing} not found" insert_at index+1, element end |
#insert_at(index, element) ⇒ Object
Inserting
47 48 49 50 51 52 53 54 55 56 |
# File 'lib/arbre/child_element_collection.rb', line 47 def insert_at(index, element) if include?(element) index -= 1 if @elements.index(element) <= index @elements.delete element else assign_to_parent element end @elements.insert index, element self end |
#insert_before(existing, element) ⇒ Object
65 66 67 68 69 70 |
# File 'lib/arbre/child_element_collection.rb', line 65 def insert_before(existing, element) index = @elements.index(existing) or raise ArgumentError, "existing element #{existing} not found" insert_at index, element end |
#remove(element) ⇒ Object
32 33 34 35 36 37 |
# File 'lib/arbre/child_element_collection.rb', line 32 def remove(element) return unless include?(element) element.parent = nil @elements.delete element end |