Class: Opencdd::CompositionTree::Node
- Inherits:
-
Struct
- Object
- Struct
- Opencdd::CompositionTree::Node
- Includes:
- Enumerable
- Defined in:
- lib/opencdd/composition_tree.rb
Instance Attribute Summary collapse
-
#children ⇒ Object
Returns the value of attribute children.
-
#entity ⇒ Object
Returns the value of attribute entity.
Instance Method Summary collapse
-
#classes ⇒ Object
Polymorphic dispatch via Entity#type — no
is_a?chains. - #depth ⇒ Object
- #each(&block) ⇒ Object
- #leaf? ⇒ Boolean
- #properties ⇒ Object
- #size ⇒ Object
- #walk {|_self| ... } ⇒ Object
Instance Attribute Details
#children ⇒ Object
Returns the value of attribute children
7 8 9 |
# File 'lib/opencdd/composition_tree.rb', line 7 def children @children end |
#entity ⇒ Object
Returns the value of attribute entity
7 8 9 |
# File 'lib/opencdd/composition_tree.rb', line 7 def entity @entity end |
Instance Method Details
#classes ⇒ Object
Polymorphic dispatch via Entity#type — no is_a? chains.
Adding a new entity type that should participate in trees
means giving it the right type (already required); no
edits to this file.
39 40 41 |
# File 'lib/opencdd/composition_tree.rb', line 39 def classes walk.filter_map { |n| n.entity if n.entity.type == :class } end |
#depth ⇒ Object
26 27 28 29 |
# File 'lib/opencdd/composition_tree.rb', line 26 def depth return 1 if leaf? 1 + children.map(&:depth).max end |
#each(&block) ⇒ Object
10 11 12 13 |
# File 'lib/opencdd/composition_tree.rb', line 10 def each(&block) return enum_for(:each) unless block walk(&block) end |
#leaf? ⇒ Boolean
22 23 24 |
# File 'lib/opencdd/composition_tree.rb', line 22 def leaf? children.nil? || children.empty? end |
#properties ⇒ Object
43 44 45 |
# File 'lib/opencdd/composition_tree.rb', line 43 def properties walk.filter_map { |n| n.entity if n.entity.type == :property } end |
#size ⇒ Object
31 32 33 |
# File 'lib/opencdd/composition_tree.rb', line 31 def size walk.count end |
#walk {|_self| ... } ⇒ Object
15 16 17 18 19 20 |
# File 'lib/opencdd/composition_tree.rb', line 15 def walk(&block) return enum_for(:walk) unless block yield self children&.each { |c| c.walk(&block) } self end |