Class: Foundries::Recording::Node
- Inherits:
-
Object
- Object
- Foundries::Recording::Node
- Defined in:
- lib/foundries/recording/node.rb
Instance Attribute Summary collapse
-
#children ⇒ Object
readonly
Returns the value of attribute children.
-
#factory ⇒ Object
readonly
Returns the value of attribute factory.
-
#self_signature ⇒ Object
readonly
Returns the value of attribute self_signature.
-
#signature ⇒ Object
readonly
Returns the value of attribute signature.
-
#traits ⇒ Object
readonly
Returns the value of attribute traits.
-
#tree_size ⇒ Object
readonly
Returns the value of attribute tree_size.
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
- #hash ⇒ Object
-
#initialize(factory:, traits: [], children: []) ⇒ Node
constructor
A new instance of Node.
- #normalize ⇒ Object
- #to_h ⇒ Object
- #to_s ⇒ Object
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
#children ⇒ Object (readonly)
Returns the value of attribute children.
6 7 8 |
# File 'lib/foundries/recording/node.rb', line 6 def children @children end |
#factory ⇒ Object (readonly)
Returns the value of attribute factory.
6 7 8 |
# File 'lib/foundries/recording/node.rb', line 6 def factory @factory end |
#self_signature ⇒ Object (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 |
#signature ⇒ Object (readonly)
Returns the value of attribute signature.
18 19 20 |
# File 'lib/foundries/recording/node.rb', line 18 def signature @signature end |
#traits ⇒ Object (readonly)
Returns the value of attribute traits.
6 7 8 |
# File 'lib/foundries/recording/node.rb', line 6 def traits @traits end |
#tree_size ⇒ Object (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 |
#hash ⇒ Object
43 44 45 |
# File 'lib/foundries/recording/node.rb', line 43 def hash signature.hash end |
#normalize ⇒ Object
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_h ⇒ Object
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_s ⇒ Object
47 48 49 |
# File 'lib/foundries/recording/node.rb', line 47 def to_s signature end |