module Cura::Attributes::HasChildren

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

Public Class Methods

new(*arguments) click to toggle source
Calls superclass method
# File lib/cura/attributes/has_children.rb, line 13
def initialize(*arguments)
  @children = []

  super
end

Public Instance Methods

add_child(component_or_type, attributes={}) click to toggle source

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.

@param [#to_sym, Component] component_or_type

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

@param [#to_h] attributes

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.

@return [Component]

# 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) click to toggle source

Add multiple children to this group.

@param [<Component>] children @return [<Component>]

# File lib/cura/attributes/has_children.rb, line 81
def add_children(*children)
  children.each { |child| add_child(child) }
end
children(recursive=false) click to toggle source

Get the children of this object.

@param [Boolean] recursive Determines if the children should be gathered recursively to retrieve all of this object's decendants. @return [<Component>]

# 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?() click to toggle source

Determine if this group has children.

@return [Boolean]

# File lib/cura/attributes/has_children.rb, line 115
def children?
  @children.any?
end
delete_child(component) click to toggle source

Remove a child from this object's children.

@param [Component] component @return [Component]

# 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) click to toggle source

Remove a child from this object's children at the given index.

@param [#to_i] index @return [Component]

# File lib/cura/attributes/has_children.rb, line 89
def delete_child_at(index)
  @children.delete_at(index.to_i)
end
delete_children() click to toggle source

Remove all children.

@return [Group]

# 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) click to toggle source

Traverse the children of this object.

@return [Array]

# File lib/cura/attributes/has_children.rb, line 22
def each(&block)
  @children.each(&block)
end

Protected Instance Methods

draw_children() click to toggle source
# File lib/cura/attributes/has_children.rb, line 129
def draw_children
  children.each(&:draw)
end
update_children() click to toggle source
# File lib/cura/attributes/has_children.rb, line 125
def update_children
  children.each(&:update)
end
validate_component(component) click to toggle source
# File lib/cura/attributes/has_children.rb, line 121
def validate_component(component)
  raise Error::InvalidComponent unless component.is_a?(Component::Base)
end