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.
43 44 45 46 47 48 49 |
# File 'lib/cura/attributes/has_children.rb', line 43 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.
55 56 57 |
# File 'lib/cura/attributes/has_children.rb', line 55 def add_children(*children) children.each { |child| add_child(child) } end |
#children(recursive = false) ⇒ <Component>
Get the children of this object.
31 32 33 34 35 36 37 |
# File 'lib/cura/attributes/has_children.rb', line 31 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.
88 89 90 |
# File 'lib/cura/attributes/has_children.rb', line 88 def children? @children.any? end |
#delete_child(component) ⇒ Component
Remove a child from this object’s children.
71 72 73 74 75 |
# File 'lib/cura/attributes/has_children.rb', line 71 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.
63 64 65 |
# File 'lib/cura/attributes/has_children.rb', line 63 def delete_child_at(index) @children.delete_at(index.to_i) end |
#delete_children ⇒ Group
Remove all children.
80 81 82 83 |
# File 'lib/cura/attributes/has_children.rb', line 80 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.
23 24 25 |
# File 'lib/cura/attributes/has_children.rb', line 23 def each(&block) @children.each(&block) end |
#initialize(*arguments) ⇒ Object
14 15 16 17 18 |
# File 'lib/cura/attributes/has_children.rb', line 14 def initialize(*arguments) @children = [] super end |