Class: Opencdd::CompositionTree::Node

Inherits:
Struct
  • Object
show all
Includes:
Enumerable
Defined in:
lib/opencdd/composition_tree.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#childrenObject

Returns the value of attribute children

Returns:

  • (Object)

    the current value of children



7
8
9
# File 'lib/opencdd/composition_tree.rb', line 7

def children
  @children
end

#entityObject

Returns the value of attribute entity

Returns:

  • (Object)

    the current value of entity



7
8
9
# File 'lib/opencdd/composition_tree.rb', line 7

def entity
  @entity
end

Instance Method Details

#classesObject

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

#depthObject



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

Returns:

  • (Boolean)


22
23
24
# File 'lib/opencdd/composition_tree.rb', line 22

def leaf?
  children.nil? || children.empty?
end

#propertiesObject



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

#sizeObject



31
32
33
# File 'lib/opencdd/composition_tree.rb', line 31

def size
  walk.count
end

#walk {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



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