class AbstractMapper::AST::Branch
A special type of the composed node, that describes transformation, applied to some level of nested input.
Unlike the simple node, describing a transformation of data, the branch carries a collection of subnodes along with methods to [#update] itself with the same attributes and different subnodes.
Tne branch only stores subnodes and composes transformations. Its has no access to DSL
and knows neither how to build a tree (see [AbstractMapper::Builder]), nor how to optimize it later (see [AbstractMapper::Optimizer]).
@api public
Public Class Methods
@private
# File lib/abstract_mapper/ast/branch.rb, line 37 def initialize(attributes = {}) @subnodes = block_given? ? yield : [] super(attributes, &nil) end
Public Instance Methods
Returns a new branch with the other node added to its subnodes
@param [AbstractMapper::AST::Node] other
@return [AbstractMapper::Branch]
# File lib/abstract_mapper/ast/branch.rb, line 74 def <<(other) update { entries << other } end
@!method each Returns the enumerator for subnodes
@return [Enumerator]
# File lib/abstract_mapper/ast/branch.rb, line 64 def each(&block) @subnodes.each(&block) end
Checks equality of branches by type, attributes and subnodes
@param [Other] other
@return [Boolean]
# File lib/abstract_mapper/ast/branch.rb, line 105 def eql?(other) super && entries.eql?(other.entries) end
Adds subnodes to the default description of the branch
@return [String]
# File lib/abstract_mapper/ast/branch.rb, line 95 def to_s "#{super} [#{map(&:inspect).join(", ")}]" end
The composition of transformations from all subnodes of the branch
To be reloaded by the subclasses to apply the composition to a corresponding level of nested data.
@return [Transproc::Function]
@abstract
# File lib/abstract_mapper/ast/branch.rb, line 87 def transproc map(&:transproc).inject(:>>) end
Returns a new branch of the same type, with the same attributes, but with a different collection of subnodes, transmitted by the block.
@example
branch = Branch.new(:foo) # => <Branch(:foo) []> branch.update { AST::Node.new(:bar) } # => <Branch(:foo) [<AST::Node(:bar)>]>
@return [AbstractMapper::Branch]
@yield block
# File lib/abstract_mapper/ast/branch.rb, line 55 def update self.class.new(attributes) { yield } end
Private Instance Methods
Substitutes the name of the class by the special name “Root” to describe the root node of AST
.
# File lib/abstract_mapper/ast/branch.rb, line 113 def __name__ instance_of?(Branch) ? "Root" : super end