Class: AbstractMapper::Node

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

Overview

An immutable node of the abstract syntax tree (AST), that describes either some “end-up” transformation, or a level of nested input data.

Every node is expected to accept attributes and (possibly) block, and implement ‘#transproc` that implements the `#call` method to transform input data to the output.

Nodes describe only the structure of AST, they know neither how to build the tree with DSL (see [AbstractMapper::Builder]), nor how to optimize it later (see [AbstractMapper::Optimizer]).

Direct Known Subclasses

Branch

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*attributes, &block) ⇒ Node

Returns a new instance of Node.



33
34
35
36
37
# File 'lib/abstract_mapper/node.rb', line 33

def initialize(*attributes, &block)
  @attributes = attributes.freeze
  @block      = block
  freeze
end

Instance Attribute Details

#attributesArray (readonly)

Returns The list of node-specific attributes.

Returns:

  • (Array)

    The list of node-specific attributes



24
25
26
# File 'lib/abstract_mapper/node.rb', line 24

def attributes
  @attributes
end

#blockProc (readonly)

Returns The block given to initializer.

Returns:

  • (Proc)

    The block given to initializer



30
31
32
# File 'lib/abstract_mapper/node.rb', line 30

def block
  @block
end

Instance Method Details

#inspectString

Returns a human-readable string representating the node

Returns:

  • (String)


52
53
54
# File 'lib/abstract_mapper/node.rb', line 52

def inspect
  "<#{self}>"
end

#to_sString

Converts the node into string with its name and attributes

Returns:

  • (String)


60
61
62
# File 'lib/abstract_mapper/node.rb', line 60

def to_s
  "#{__name__}#{__attributes__}"
end

#transprocObject

This method is abstract.

The transformation function for the branch



44
45
46
# File 'lib/abstract_mapper/node.rb', line 44

def transproc
  Transproc::Function.new(-> v { v }, {})
end