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
-
#parent ⇒ Composite
readonly
Gets this object’s current ID.
Instance Method Summary collapse
-
#initialize ⇒ Void
Initialises the ParentTracker.
-
#root? ⇒ Boolean
Gets whether this ParentTracker is the root of its composite tree.
-
#update_parent(new_parent, new_id_proc) ⇒ void
Updates this object’s parent and ID function.
Instance Attribute Details
#parent ⇒ Composite (readonly)
Gets this object’s current ID
58 59 60 |
# File 'lib/compo/mixins/parent_tracker.rb', line 58 def parent @parent end |
Instance Method Details
#initialize ⇒ Void
Initialises the ParentTracker
This constructor sets the tracker up so it initially has an instance of Parentless as its ‘parent’.
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.
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
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 |