Module Composite
In: lib/webgen/composite.rb
Enumerable Composite dot/f_51.png

Module Composite

Implementation of the Composite pattern.

Usage example:

 class Test
   include Composite
 end

 t = Test.new
 t.add_child("Hello")
 t.add_child("Lester")
 t.each do |child| print child end

Methods

Included Modules

Enumerable

Public Instance methods

Adds the child

[Source]

    # File lib/webgen/composite.rb, line 71
71:   def add_child( child )
72:     @children = [] unless defined?( @children )
73:     @children.push( child ) unless child.nil? || @children.include?( child )
74:   end

Adds all objects in the array

[Source]

    # File lib/webgen/composite.rb, line 54
54:   def add_children( array )
55:     if array.kind_of?( Array )
56:       @children = [] unless defined?( @children )
57:       @children.concat( array.compact )
58:     else
59:       raise ArgumentError, "Parameter must be array"
60:     end
61:   end

Returns the array of children

[Source]

    # File lib/webgen/composite.rb, line 47
47:   def children
48:     @children = [] unless defined?( @children )
49:     @children
50:   end

Depending on the type of argument one of these actions is taken

Numeric
the child at the specified position is deleted
else
the specified child is deleted

[Source]

    # File lib/webgen/composite.rb, line 81
81:   def del_child( child )
82:     if child.kind_of?( Numeric )
83:       @children.delete_at( child ) if defined?( @children )
84:     else
85:       @children.delete( child ) if defined?( @children )
86:     end
87:   end

Deletes all children

[Source]

    # File lib/webgen/composite.rb, line 65
65:   def del_children
66:     @children = []
67:   end

Iterates over all childrenldren

[Source]

    # File lib/webgen/composite.rb, line 91
91:   def each   # :yields: child
92:     @children.each {|child| yield child } if defined?( @children )
93:   end

Checks if there are any children

[Source]

    # File lib/webgen/composite.rb, line 97
97:   def has_children?
98:     defined?( @children ) && children.length > 0
99:   end

[Validate]