Class: AbstractMapper::AST::Branch

Inherits:
Node
  • Object
show all
Includes:
Enumerable
Defined in:
lib/abstract_mapper/ast/branch.rb

Overview

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]).

Instance Attribute Summary

Attributes inherited from Node

#block

Class Method Summary collapse

Methods inherited from Node

#==, #initialize, #inspect, #to_s, #transproc

Constructor Details

This class inherits a constructor from AbstractMapper::AST::Node

Class Method Details

.<<(other) ⇒ AbstractMapper::Branch

Returns a new branch with the other node added to its subnodes

Parameters:

Returns:

  • (AbstractMapper::Branch)


74
75
76
# File 'lib/abstract_mapper/ast/branch.rb', line 74

def <<(other)
  update { entries << other }
end

.eachEnumerator

Returns the enumerator for subnodes

Returns:

  • (Enumerator)


64
65
66
# File 'lib/abstract_mapper/ast/branch.rb', line 64

def each(&block)
  @subnodes.each(&block)
end

.eql?(other) ⇒ Boolean

Checks equality of branches by type, attributes and subnodes

Parameters:

  • other (Other)

Returns:

  • (Boolean)


105
106
107
# File 'lib/abstract_mapper/ast/branch.rb', line 105

def eql?(other)
  super && entries.eql?(other.entries)
end

.initialize(attributes = {}) ⇒ Object



37
38
39
40
# File 'lib/abstract_mapper/ast/branch.rb', line 37

def initialize(attributes = {})
  @subnodes = block_given? ? yield : []
  super(attributes, &nil)
end

.new(*attributes, &block) ⇒ Branch::AST::Node

Creates a new branch

Parameters:

  • attributes (Object, Array<Object>)

    The list of type-specific attributes of the branch

  • block (Proc)

    The block that returns an array of subnodes for the branch

Returns:

  • (Branch::AST::Node)


# File 'lib/abstract_mapper/ast/branch.rb', line 25

.to_sString

Adds subnodes to the default description of the branch

Returns:

  • (String)


95
96
97
# File 'lib/abstract_mapper/ast/branch.rb', line 95

def to_s
  "#{super} [#{map(&:inspect).join(", ")}]"
end

.transprocTransproc::Function

This method is abstract.

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.

Returns:

  • (Transproc::Function)


87
88
89
# File 'lib/abstract_mapper/ast/branch.rb', line 87

def transproc
  map(&:transproc).inject(:>>)
end

.update { ... } ⇒ AbstractMapper::Branch

Returns a new branch of the same type, with the same attributes, but with a different collection of subnodes, transmitted by the block.

Examples:

branch = Branch.new(:foo)
# => <Branch(:foo) []>
branch.update { AST::Node.new(:bar) }
# => <Branch(:foo) [<AST::Node(:bar)>]>

Yields:

  • block

Returns:

  • (AbstractMapper::Branch)


55
56
57
# File 'lib/abstract_mapper/ast/branch.rb', line 55

def update
  self.class.new(attributes) { yield }
end