Module: Cura::Attributes::HasChildren

Includes:
Enumerable
Included in:
Component::Group
Defined in:
lib/cura/attributes/has_children.rb

Overview

Allows an object to have child components. TODO: Lots of code is the same as HasWindows

Instance Method Summary collapse

Instance Method Details

#add_child(component) ⇒ Component

Add a child to this group.

Parameters:

Returns:

Raises:

  • (TypeError)


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.

Parameters:

Returns:



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.

Parameters:

  • recursive (Boolean) (defaults to: false)

    Determines if the children should be gathered recursively to retrieve all of this object’s decendants.

Returns:



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.

Returns:

  • (Boolean)


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.

Parameters:

Returns:



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.

Parameters:

  • index (#to_i)

Returns:



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_childrenGroup

Remove all children.

Returns:

  • (Group)


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.

Returns:

  • (Array)


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