Module: Compo::ParentTracker

Extended by:
Forwardable
Included in:
Branch
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.

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:



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

def parent
  @parent
end

Instance Method Details

#initializeVoid

Initialises the ParentTracker

This constructor sets the tracker up so it initially has an instance of Parentless as its ‘parent’.

Examples:

Creates a new ParentTracker.

ParentTracker.new

Returns:

  • (Void)


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

def initialize
  super()
  remove_parent
end

#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)


76
77
78
# File 'lib/compo/parent_tracker.rb', line 76

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)


61
62
63
64
65
66
67
# File 'lib/compo/parent_tracker.rb', line 61

def update_parent(new_parent, new_id_function)
  fail 'Parent cannot be nil: use #remove_parent.' if new_parent.nil?
  fail 'ID function cannot be nil: use -> { nil }.' if new_id_function.nil?

  @parent = new_parent
  @id_function = new_id_function
end