Class: Ast::Merge::NodeTyping::Wrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/ast/merge/node_typing/wrapper.rb

Overview

Node wrapper that adds a merge_type attribute to an existing node.
This uses a simple delegation pattern to preserve all original node
behavior while adding the merge_type.

Direct Known Subclasses

FrozenWrapper

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node, merge_type) ⇒ Wrapper

Create a new node type wrapper.

Parameters:

  • node (Object)

    The original node to wrap

  • merge_type (Symbol)

    The custom merge type



20
21
22
23
# File 'lib/ast/merge/node_typing/wrapper.rb', line 20

def initialize(node, merge_type)
  @node = node
  @merge_type = merge_type
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

Delegate all unknown methods to the wrapped node.
This allows the wrapper to be used transparently in place of the node.



27
28
29
30
31
32
33
# File 'lib/ast/merge/node_typing/wrapper.rb', line 27

def method_missing(method, *args, &block)
  if @node.respond_to?(method)
    @node.send(method, *args, &block)
  else
    super
  end
end

Instance Attribute Details

#merge_typeSymbol (readonly)

Returns The custom merge type for this node.

Returns:

  • (Symbol)

    The custom merge type for this node



14
15
16
# File 'lib/ast/merge/node_typing/wrapper.rb', line 14

def merge_type
  @merge_type
end

#nodeObject (readonly)

Returns The original node being wrapped.

Returns:

  • (Object)

    The original node being wrapped



11
12
13
# File 'lib/ast/merge/node_typing/wrapper.rb', line 11

def node
  @node
end

Instance Method Details

#==(other) ⇒ Object

Forward equality check to the wrapped node.



52
53
54
55
56
57
58
# File 'lib/ast/merge/node_typing/wrapper.rb', line 52

def ==(other)
  if other.is_a?(Wrapper)
    @node == other.node && @merge_type == other.merge_type
  else
    @node == other
  end
end

#eql?(other) ⇒ Boolean

Forward eql? to the wrapped node.

Returns:

  • (Boolean)


66
67
68
# File 'lib/ast/merge/node_typing/wrapper.rb', line 66

def eql?(other)
  self == other
end

#hashObject

Forward hash to the wrapped node.



61
62
63
# File 'lib/ast/merge/node_typing/wrapper.rb', line 61

def hash
  [@node, @merge_type].hash
end

#inspectObject

Forward inspect to show both the type and node.



71
72
73
# File 'lib/ast/merge/node_typing/wrapper.rb', line 71

def inspect
  "#<NodeTyping::Wrapper merge_type=#{@merge_type.inspect} node=#{@node.inspect}>"
end

#respond_to_missing?(method, include_private = false) ⇒ Boolean

Check if the wrapped node responds to a method.

Returns:

  • (Boolean)


36
37
38
# File 'lib/ast/merge/node_typing/wrapper.rb', line 36

def respond_to_missing?(method, include_private = false)
  @node.respond_to?(method, include_private) || super
end

#typed_node?Boolean

Returns true to indicate this is a node type wrapper.

Returns:

  • (Boolean)


41
42
43
# File 'lib/ast/merge/node_typing/wrapper.rb', line 41

def typed_node?
  true
end

#unwrapObject

Unwrap to get the original node.

Returns:

  • (Object)

    The original unwrapped node



47
48
49
# File 'lib/ast/merge/node_typing/wrapper.rb', line 47

def unwrap
  @node
end