Module: Cura::Attributes::HasChildren
Overview
Allows an object to have child components. TODO: Lots of code is the same as HasWindows
Instance Method Summary collapse
-
#add_child(component) ⇒ Component
Add a child to this group.
-
#add_children(*children) ⇒ <Component>
Add multiple children to this group.
-
#children(recursive = false) ⇒ <Component>
Get the children of this object.
-
#children? ⇒ Boolean
Determine if this group has children.
-
#delete_child(component) ⇒ Component
Remove a child from this object’s children.
-
#delete_child_at(index) ⇒ Component
Remove a child from this object’s children at the given index.
-
#delete_children ⇒ Group
Remove all children.
-
#each(&block) ⇒ Array
Traverse the children of this object.
- #initialize(*arguments) ⇒ Object
Instance Method Details
#add_child(component) ⇒ Component
Add a child to this group.
45 46 47 48 49 50 51 |
# File 'lib/cura/attributes/has_children.rb', line 45 def add_child(component) raise TypeError, "component must be a Cura::Component" unless component.is_a?(Component::Base) @children << component component end |
#add_children(*children) ⇒ <Component>
Add multiple children to this group.
57 58 59 |
# File 'lib/cura/attributes/has_children.rb', line 57 def add_children(*children) children.each { |child| add_child(child) } end |
#children(recursive = false) ⇒ <Component>
Get the children of this object.
33 34 35 36 37 38 39 |
# File 'lib/cura/attributes/has_children.rb', line 33 def children(recursive=false) if recursive @children.collect { |child| child.respond_to?(:children) ? [child] + child.children(true) : child }.flatten # TODO: Shouldn't flatten? else @children end end |
#children? ⇒ Boolean
Determine if this group has children.
91 92 93 |
# File 'lib/cura/attributes/has_children.rb', line 91 def children? @children.any? end |
#delete_child(component) ⇒ Component
Remove a child from this object’s children.
73 74 75 76 77 |
# File 'lib/cura/attributes/has_children.rb', line 73 def delete_child(component) validate_component(component) delete_child_at(@children.index(component)) end |
#delete_child_at(index) ⇒ Component
Remove a child from this object’s children at the given index.
65 66 67 |
# File 'lib/cura/attributes/has_children.rb', line 65 def delete_child_at(index) @children.delete_at(index.to_i) end |
#delete_children ⇒ Group
Remove all children.
82 83 84 85 86 |
# File 'lib/cura/attributes/has_children.rb', line 82 def delete_children (0...@children.count).to_a.reverse_each { |index| delete_child_at(index) } # TODO: Why reverse? self end |
#each(&block) ⇒ Array
Traverse the children of this object.
25 26 27 |
# File 'lib/cura/attributes/has_children.rb', line 25 def each(&block) @children.each(&block) end |
#initialize(*arguments) ⇒ Object
16 17 18 19 20 |
# File 'lib/cura/attributes/has_children.rb', line 16 def initialize(*arguments) @children = [] super end |