Class: AbstractMapper::Branch

Inherits:
Node
  • Object
show all
Includes:
Enumerable
Defined in:
lib/abstract_mapper/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 [#rebuild] 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

#attributes, #block

Class Method Summary collapse

Methods inherited from Node

#initialize, #inspect, #to_s, #transproc

Constructor Details

This class inherits a constructor from AbstractMapper::Node

Class Method Details

.<<(other) ⇒ AbstractMapper::Branch

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

Parameters:

Returns:



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

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

.eachEnumerator

Returns the enumerator for subnodes

Returns:

  • (Enumerator)


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

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

.initialize(*attributes) ⇒ Object



35
36
37
38
# File 'lib/abstract_mapper/branch.rb', line 35

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

.new(*attributes, &block) ⇒ Branch::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::Node)


# File 'lib/abstract_mapper/branch.rb', line 23


.rebuild(&block) { ... } ⇒ 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.rebuild { Node.new(:bar) }
# => <Branch(:foo) [<Node(:bar)>]>

Parameters:

  • block (Proc)

    The block that should return an array of subnodes for the branch

Yields:

  • block

Returns:



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

def rebuild(&block)
  self.class.new(*attributes, &block)
end

.to_sString

Adds subnodes to the default description of the branch

Returns:

  • (String)


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

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)


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

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