Module: Compo::Composite

Extended by:
Forwardable
Includes:
Enumerable
Included in:
ArrayComposite, HashComposite, NullComposite, Parentless
Defined in:
lib/compo/composite.rb

Overview

Mixin for objects that can contain other objects

Objects implementing this interface should implement add!, remove! or remove_id!, and id_function:

add! - Given a desired ID and child, adds the child to the children

of this object; returns the child if successful, nil
otherwise.

remove! - Given a child, removes and returns it from the children; if

not provided, this is implemented in terms of remove_id!.

remove_id! - Given an ID, removes and returns the child with this ID from

the children; if not provided, this is implemented in terms
of remove!.

children - Returns the children, as a Hash mapping from current IDs to

their child values.

id_function - Given a newly inserted child, returns a proc that will

always return the child's current ID so long as it is part
of the Composite.

Instance Method Summary collapse

Instance Method Details

#add(id, child) ⇒ Object

Adds a child to this Composite

Examples:

Adds a child with intended id 3.

composite.add_child(3, leaf)

Parameters:

  • id (Object)

    The intended ID of the child in this Composite. The actual ID may not be the same as this; consult the proc supplied to the child via #update_parent.

  • child (Object)

    The child to add to this Composite.

Returns:

  • (Object)

    The added child if successful; nil otherwise.



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

def add(id, child)
  add!(id, child).tap(&method(:assign_parent_to))
end

#get_child(id) ⇒ Object

Gets the child in this Composite with the given ID

Examples:

Gets the child with ID :in, if children is 3.

composite.get_child(:in)
#=> 3

Fails to get the child with ID :out, if children is 3.

composite.get_child(:out)
#=> nil

Parameters:

  • id (Object)

    The ID of the child to get from this Composite.

Returns:

  • (Object)

    The child if successful; nil otherwise.



87
88
89
# File 'lib/compo/composite.rb', line 87

def get_child(id)
  children[id]
end

#remove(child) ⇒ Object

Removes a child from this Composite directly

This method can fail (for example, if the child does not exist in the Composite).

Examples:

Removes a child.

composite.remove(child)

Parameters:

  • child (Object)

    The child to remove from this object.

Returns:

  • (Object)

    The removed child if successful; nil otherwise.



54
55
56
# File 'lib/compo/composite.rb', line 54

def remove(child)
  remove!(child).tap(&method(:remove_parent_of))
end

#remove_id(id) ⇒ Object

Removes a child from this Composite, given its ID

This method can fail (for example, if the ID does not exist in the Composite).

Examples:

Removes the child with ID :foo.

composite.remove_id(:foo)

Parameters:

  • id

    The ID of the child to remove from this object.

Returns:

  • (Object)

    The removed child if successful; nil otherwise.



70
71
72
# File 'lib/compo/composite.rb', line 70

def remove_id(id)
  remove_id!(id).tap(&method(:remove_parent_of))
end