Module: Compo::ParentTracker

Extended by:
Forwardable
Included in:
ArrayBranch, HashBranch, Leaf
Defined in:
lib/compo/parent_tracker.rb

Overview

Basic implementation of parent tracking as a mixin

This implements #parent, #update_parent and #remove_parent to track the current parent and ID function as instance variables. It also implements #parent, and #id in terms of the ID function.

Subclasses should call #remove_parent in their #initialize methods, to set the parent and ID function to their default, empty values.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#parentComposite (readonly)

Gets this object’s current ID

Examples:

Gets the object’s parent while it has none.

parent_tracker.parent
#=> nil

Gets the object’s parent while it has one.

parent_tracker.parent
#=> :the_current_parent

Returns:



27
28
29
# File 'lib/compo/parent_tracker.rb', line 27

def parent
  @parent
end

Instance Method Details

#remove_parentvoid

This method returns an undefined value.

Blanks out this object’s parent and ID function

Examples:

Update this Leaf’s parent and ID function.

movable.update_parent(new_parent, new_id_function)


61
62
63
# File 'lib/compo/parent_tracker.rb', line 61

def remove_parent
  update_parent(Parentless.new, -> { nil })
end

#update_parent(new_parent, new_id_function) ⇒ void

This method returns an undefined value.

Updates this object’s parent and ID function

Examples:

Update this Leaf’s parent and ID function.

parent_tracker.update_parent(new_parent, new_id_function)


49
50
51
52
# File 'lib/compo/parent_tracker.rb', line 49

def update_parent(new_parent, new_id_function)
  @parent = new_parent
  @id_function = new_id_function
end