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_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.

Parameters:

  • component_or_type (#to_sym, Component)

    A Symbol representing the child component type or a Component::Base instance. When a Symbol is given, a new child component will be initialized of that type. See Component::Base.type.

  • attributes (#to_h) (defaults to: {})

    When component_or_type is a Class, then these attributes will be used to initialize the child component. When component_or_type is a Symbol, then these attributes will be used to initialize the child component. When component_or_type is a Component::Base, then these attributes will be used to update the child component.

Returns:



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.

Parameters:

Returns:



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.

Parameters:

  • recursive (Boolean) (defaults to: false)

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

Returns:



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.

Returns:

  • (Boolean)


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.

Parameters:

Returns:



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.

Parameters:

  • index (#to_i)

Returns:



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_childrenGroup

Remove all children.

Returns:

  • (Group)


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.

Returns:

  • (Array)


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