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_or_type, attributes = {}) ⇒ 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_or_type, attributes = {}) ⇒ Component
Add a child to this group.
If a Hash-like object is given, it must have the ‘:type` key which will be used to determine which class to initialize. The rest of the Hash will be used to set the attributes of the instance.
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/cura/attributes/has_children.rb', line 52 def add_child(component_or_type, attributes={}) component = if component_or_type.is_a?(Class) raise Error::InvalidComponent unless component_or_type < Component::Base component_or_type.new elsif component_or_type.respond_to?(:to_sym) type = component_or_type.to_sym component_class = Component.find_by_type(type) raise Error::InvalidComponent if component_class.nil? component_class.new else raise Error::InvalidComponent unless component_or_type.is_a?(Component::Base) component_or_type end component.update_attributes(attributes) @children << component component end |
#add_children(*children) ⇒ <Component>
Add multiple children to this group.
81 82 83 |
# File 'lib/cura/attributes/has_children.rb', line 81 def add_children(*children) children.each { |child| add_child(child) } end |
#children(recursive = false) ⇒ <Component>
Get the children of this object.
30 31 32 33 34 35 36 |
# File 'lib/cura/attributes/has_children.rb', line 30 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.
115 116 117 |
# File 'lib/cura/attributes/has_children.rb', line 115 def children? @children.any? end |
#delete_child(component) ⇒ Component
Remove a child from this object’s children.
97 98 99 100 101 |
# File 'lib/cura/attributes/has_children.rb', line 97 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.
89 90 91 |
# File 'lib/cura/attributes/has_children.rb', line 89 def delete_child_at(index) @children.delete_at(index.to_i) end |
#delete_children ⇒ Group
Remove all children.
106 107 108 109 110 |
# File 'lib/cura/attributes/has_children.rb', line 106 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.
22 23 24 |
# File 'lib/cura/attributes/has_children.rb', line 22 def each(&block) @children.each(&block) end |
#initialize(*arguments) ⇒ Object
13 14 15 16 17 |
# File 'lib/cura/attributes/has_children.rb', line 13 def initialize(*arguments) @children = [] super end |