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:



29
30
31
# File 'lib/compo/parent_tracker.rb', line 29

def parent
  @parent
end

Instance Method Details

#initializeObject



13
14
15
16
# File 'lib/compo/parent_tracker.rb', line 13

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)


66
67
68
# File 'lib/compo/parent_tracker.rb', line 66

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)


51
52
53
54
55
56
57
# File 'lib/compo/parent_tracker.rb', line 51

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