Module: ParallelAncestry
- Extended by:
- ModuleCluster::Define::Block::ClassOrModule, ParallelAncestry
- Included in:
- ParallelAncestry
- Defined in:
- lib/parallel-ancestry.rb,
lib/parallel-ancestry/ParallelAncestry.rb
Defined Under Namespace
Modules: Inheritance, ModuleSubclassInheritance Classes: InstanceAncestryStruct
Instance Method Summary collapse
-
#ancestor(instance) { ... } ⇒ Object
(also: #parent)
Return parent for instance that matches match_ancestor_block.
-
#ancestor_chain(instance) { ... } ⇒ Array<Object>
Returns ancestor chain defined for provided object.
-
#children(instance) ⇒ Array<Object>
Return a list of children for provided object.
-
#has_children?(instance) ⇒ true, false
Return whether provided object has children.
-
#has_parents?(instance) ⇒ true, false
Return whether provided object has parents.
-
#highest_children(instance) { ... } ⇒ Array<Object>
Returns the highest parent in each parent tree matching block condition.
-
#lowest_parents(instance) { ... } ⇒ Array<Object>
Returns the lowest parent in each parent tree matching block condition.
-
#match_ancestor(instance, ancestor_match_block) { ... } ⇒ Object
Returns the first ancestor (determined by ancestor_match_block) for which match_block is true.
-
#parents(instance) ⇒ Array<Object>
Return a list of parents for provided object.
-
#register_child_for_parent(child, parent) ⇒ Array<Object>
Register instance as child of another instance.
Instance Method Details
#ancestor(instance) { ... } ⇒ Object Also known as: parent
Return parent for instance that matches match_ancestor_block.
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/parallel-ancestry/ParallelAncestry.rb', line 118 def ancestor( instance, & match_ancestor_block ) ancestor_instance = nil parents = parents( instance ) # If we don't have ancestors explicitly declared for this instance, and if it is not # a ::Class or ::Module (both are ::Modules) then we have an instance of a class, # so we can use the instance's class if parents.empty? and instance != ::Class instance_class = instance.class if match_ancestor_block.call( instance_class ) ancestor_instance = instance.class end else parents.each do |this_parent| # we need a way to go from multiple parents to the one that makes up this chain # we use the match_ancestor_block to determine this - it should return true/false if match_ancestor_block.call( this_parent ) ancestor_instance = this_parent break end end end return ancestor_instance end |
#ancestor_chain(instance) { ... } ⇒ Array<Object>
Returns ancestor chain defined for provided object.
170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/parallel-ancestry/ParallelAncestry.rb', line 170 def ancestor_chain( instance, & match_ancestor_block ) ancestor_chain = [ this_ancestor = instance ] while this_ancestor = ancestor( this_ancestor, & match_ancestor_block ) ancestor_chain.push( this_ancestor ) end return ancestor_chain end |
#children(instance) ⇒ Array<Object>
Return a list of children for provided object.
33 34 35 36 37 |
# File 'lib/parallel-ancestry/ParallelAncestry.rb', line 33 def children( instance ) return ancestor_struct( instance ).children ||= ::UniqueArray.new( self ) end |
#has_children?(instance) ⇒ true, false
Return whether provided object has children.
72 73 74 75 76 |
# File 'lib/parallel-ancestry/ParallelAncestry.rb', line 72 def has_children?( instance ) return ! children( instance ).empty? end |
#has_parents?(instance) ⇒ true, false
Return whether provided object has parents.
59 60 61 62 63 |
# File 'lib/parallel-ancestry/ParallelAncestry.rb', line 59 def has_parents?( instance ) return ! parents( instance ).empty? end |
#highest_children(instance) { ... } ⇒ Array<Object>
Returns the highest parent in each parent tree matching block condition. For simple linear trees, this is simply the first parent, but more complex trees quickly diverge into multiple branches, each of which then requires a highest match.
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 |
# File 'lib/parallel-ancestry/ParallelAncestry.rb', line 250 def highest_children( instance, & match_ancestor_block ) # the first super module available for each tree highest_children_array = [ ] children( instance ).each do |this_child| # if we match this parent we are done with this branch and can go to the next if match_ancestor_block.call( this_child ) highest_children_array.push( this_child ) # otherwise our branch expands and we have to finish it before the next parent elsif has_children?( this_child ) children_for_branch = highest_children( this_child, & match_ancestor_block ) highest_children_array.concat( children_for_branch ) end end return highest_children_array end |
#lowest_parents(instance) { ... } ⇒ Array<Object>
Returns the lowest parent in each parent tree matching block condition. For simple linear trees, this is simply the first parent, but more complex trees quickly diverge into multiple branches, each of which then requires a lowest match.
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/parallel-ancestry/ParallelAncestry.rb', line 202 def lowest_parents( instance, & match_ancestor_block ) # the first super module available for each tree lowest_parents_array = [ ] parents( instance ).each do |this_parent| # if we match this parent we are done with this branch and can go to the next if match_ancestor_block.call( this_parent ) lowest_parents_array.push( this_parent ) # otherwise our branch expands and we have to finish it before the next parent elsif has_parents?( this_parent ) parents_for_branch = lowest_parents( this_parent, & match_ancestor_block ) lowest_parents_array.concat( parents_for_branch ) end end return lowest_parents_array end |
#match_ancestor(instance, ancestor_match_block) { ... } ⇒ Object
Returns the first ancestor (determined by ancestor_match_block) for which match_block is true.
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 |
# File 'lib/parallel-ancestry/ParallelAncestry.rb', line 305 def match_ancestor( instance, ancestor_match_block, & match_block ) matched_ancestor = nil this_ancestor = instance if has_parents?( this_ancestor ) begin if match_block.call( this_ancestor ) matched_ancestor = this_ancestor break end break if this_ancestor.equal?( ::Object ) end while this_ancestor = ancestor( this_ancestor, & ancestor_match_block ) elsif match_block.call( this_ancestor ) matched_ancestor = this_ancestor else matched_ancestor = match_ancestor( this_ancestor.class, ancestor_match_block, & match_block ) end return matched_ancestor end |
#parents(instance) ⇒ Array<Object>
Return a list of parents for provided object.
46 47 48 49 50 |
# File 'lib/parallel-ancestry/ParallelAncestry.rb', line 46 def parents( instance ) return ancestor_struct( instance ).parents ||= ::UniqueArray.new( self ) end |
#register_child_for_parent(child, parent) ⇒ Array<Object>
Register instance as child of another instance.
86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/parallel-ancestry/ParallelAncestry.rb', line 86 def register_child_for_parent( child, parent ) parents_of_child = parents( child ) children_of_parent = children( parent ) # child order shouldn't be relevant children_of_parent.push( child ) # parent order determines who wins conflicts, so we keep youngest first parents_of_child.unshift( parent ) return self end |