Class: Compo::ArrayComposite

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Composite
Defined in:
lib/compo/array_composite.rb

Overview

Implementation of Composite that stores its children in an Array.

IDs for items entering a ListComposite must be numeric, and will change if an item with an ID less than the item in question is deleted or inserted. This means the ID function for objects in a ListComposite may report different values at different times.

Adding an object at an occupied ID moves the occupant and those at successive IDs up by one.

Direct Known Subclasses

ArrayBranch

Instance Method Summary collapse

Methods included from Composite

#add, #get_child, #remove, #remove_id

Constructor Details

#initializeArrayComposite

Initialises an ArrayComposite

Examples:

Initializes an ArrayComposite.

comp = ArrayComposite.new


23
24
25
# File 'lib/compo/array_composite.rb', line 23

def initialize
  @children = []
end

Instance Method Details

#childrenHash

Returns the ArrayComposite’s children, as a Hash

Examples:

Gets the children of an empty ArrayComposite.

comp.children
#=> {}

Gets the children of a populated ArrayComposite.

comp.children
#=> {0: :first, 1: :second}

Returns:

  • (Hash)

    The Hash mapping the IDs of children to their values.



38
39
40
# File 'lib/compo/array_composite.rb', line 38

def children
  Hash[(0...@children.size).zip(@children)]
end