Class: Foundries::Recording::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/foundries/recording/node.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(factory:, traits: [], children: []) ⇒ Node

Returns a new instance of Node.



8
9
10
11
12
13
14
15
16
# File 'lib/foundries/recording/node.rb', line 8

def initialize(factory:, traits: [], children: [])
  @factory = factory.to_sym
  @traits = traits.map(&:to_sym).sort.freeze
  @children = children.freeze
  @self_signature = compute_self_signature
  @signature = compute_signature
  @tree_size = 1 + children.sum(&:tree_size)
  freeze
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



6
7
8
# File 'lib/foundries/recording/node.rb', line 6

def children
  @children
end

#factoryObject (readonly)

Returns the value of attribute factory.



6
7
8
# File 'lib/foundries/recording/node.rb', line 6

def factory
  @factory
end

#self_signatureObject (readonly)

Returns the value of attribute self_signature.



18
19
20
# File 'lib/foundries/recording/node.rb', line 18

def self_signature
  @self_signature
end

#signatureObject (readonly)

Returns the value of attribute signature.



18
19
20
# File 'lib/foundries/recording/node.rb', line 18

def signature
  @signature
end

#traitsObject (readonly)

Returns the value of attribute traits.



6
7
8
# File 'lib/foundries/recording/node.rb', line 6

def traits
  @traits
end

#tree_sizeObject (readonly)

Returns the value of attribute tree_size.



18
19
20
# File 'lib/foundries/recording/node.rb', line 18

def tree_size
  @tree_size
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



37
38
39
# File 'lib/foundries/recording/node.rb', line 37

def ==(other)
  other.is_a?(self.class) && signature == other.signature
end

#hashObject



43
44
45
# File 'lib/foundries/recording/node.rb', line 43

def hash
  signature.hash
end

#normalizeObject



20
21
22
23
24
25
26
27
# File 'lib/foundries/recording/node.rb', line 20

def normalize
  normalized_children = children.map(&:normalize)
  deduped = normalized_children
    .group_by(&:self_signature)
    .map { |_sig, group| group.max_by(&:tree_size) }
    .sort_by(&:signature)
  self.class.new(factory: factory, traits: traits, children: deduped)
end

#to_hObject



29
30
31
32
33
34
35
# File 'lib/foundries/recording/node.rb', line 29

def to_h
  {
    factory: factory.to_s,
    traits: traits.map(&:to_s),
    children: children.map(&:to_h)
  }
end

#to_sObject



47
48
49
# File 'lib/foundries/recording/node.rb', line 47

def to_s
  signature
end