Class: Compo::Composites::Array

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

Overview

Implementation of Composite that stores its children in an Array.

IDs for items entering an Array 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 an Array 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

Branches::Array

Instance Method Summary collapse

Methods included from Composite

#add, #get_child, #get_child_such_that, #remove, #remove_id

Constructor Details

#initializeArray

Initialises an array composite

Examples:

Initializes an array composite.

comp = Compo::Composites::Array.new


24
25
26
# File 'lib/compo/composites/array.rb', line 24

def initialize
  @children = []
end

Instance Method Details

#childrenHash

Returns the array composite’s children, as a Hash

Examples:

Gets the children of an empty array composite.

comp.children
#=> {}

Gets the children of a populated array composite.

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

Returns:

  • (Hash)

    The Hash mapping the IDs of children to their values.



39
40
41
# File 'lib/compo/composites/array.rb', line 39

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