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

Inherits:
Object
  • Object
show all
Defined in:
lib/ast/merge/node_typing.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



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

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.



72
73
74
75
76
77
78
# File 'lib/ast/merge/node_typing.rb', line 72

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



59
60
61
# File 'lib/ast/merge/node_typing.rb', line 59

def merge_type
  @merge_type
end

#nodeObject (readonly)

Returns The original node being wrapped.

Returns:

  • (Object)

    The original node being wrapped



56
57
58
# File 'lib/ast/merge/node_typing.rb', line 56

def node
  @node
end

Instance Method Details

#==(other) ⇒ Object

Forward equality check to the wrapped node.



97
98
99
100
101
102
103
# File 'lib/ast/merge/node_typing.rb', line 97

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)


111
112
113
# File 'lib/ast/merge/node_typing.rb', line 111

def eql?(other)
  self == other
end

#hashObject

Forward hash to the wrapped node.



106
107
108
# File 'lib/ast/merge/node_typing.rb', line 106

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

#inspectObject

Forward inspect to show both the type and node.



116
117
118
# File 'lib/ast/merge/node_typing.rb', line 116

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)


81
82
83
# File 'lib/ast/merge/node_typing.rb', line 81

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)


86
87
88
# File 'lib/ast/merge/node_typing.rb', line 86

def typed_node?
  true
end

#unwrapObject

Unwrap to get the original node.

Returns:

  • (Object)

    The original unwrapped node



92
93
94
# File 'lib/ast/merge/node_typing.rb', line 92

def unwrap
  @node
end