Module: Compo::Mixins::ParentTracker

Extended by:
Forwardable
Included in:
Branches::Branch
Defined in:
lib/compo/mixins/parent_tracker.rb

Overview

Basic implementation of parent tracking as a mixin

Adding this to a Composite allows the composite to be aware of its current parent.

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:

  • (Composite)

    The current parent.



58
59
60
# File 'lib/compo/mixins/parent_tracker.rb', line 58

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)


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

def initialize
  super()
  Compo::Composites::Parentless.for(self)
end

#root?Boolean

Gets whether this ParentTracker is the root of its composite tree

This is equivalent to the ParentTracker having no parent.

Examples:

Checks if a ParentTracker with no parent is a root.

orphan.root?
#=> true

Checks if a ParentTracker with a parent is a root.

parented.root?
#=> false

Returns:

  • (Boolean)


43
44
45
# File 'lib/compo/mixins/parent_tracker.rb', line 43

def root?
  parent.on_node { |p| p }.nil?
end

#update_parent(new_parent, new_id_proc) ⇒ 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)


80
81
82
83
84
85
86
# File 'lib/compo/mixins/parent_tracker.rb', line 80

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

  @parent = new_parent
  @id_proc = new_id_proc
end