Class: Psych::Merge::NodeWrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/psych/merge/node_wrapper.rb

Overview

Wraps Psych::Nodes with comment associations, line information, and signatures.
This provides a unified interface for working with YAML nodes during merging.

Examples:

Basic usage

ast = Psych.parse_stream(yaml)
wrapper = NodeWrapper.new(ast.children.first, lines: source.lines)
wrapper.signature # => [:document, ...]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node, lines:, leading_comments: [], inline_comment: nil, key: nil) ⇒ NodeWrapper

Returns a new instance of NodeWrapper.

Parameters:

  • node (Psych::Nodes::Node)

    Psych node to wrap

  • lines (Array<String>)

    Source lines for content extraction

  • leading_comments (Array<Hash>) (defaults to: [])

    Comments before this node

  • inline_comment (Hash, nil) (defaults to: nil)

    Inline comment on the node’s line

  • key (String, nil) (defaults to: nil)

    Key name if this is a mapping value



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/psych/merge/node_wrapper.rb', line 39

def initialize(node, lines:, leading_comments: [], inline_comment: nil, key: nil)
  @node = node
  @lines = lines
  @leading_comments = leading_comments
  @inline_comment = inline_comment
  @key = key

  # Extract line information from the Psych node.
  #
  # IMPORTANT: Psych (libyaml) line number semantics:
  # - start_line: 0-based, inclusive (first line of the node's content)
  # - end_line: 0-based, EXCLUSIVE (points to the line AFTER the last content line)
  #
  # Example for a mapping value spanning lines 4-5 (1-based):
  #   Psych reports: start_line=3, end_line=5 (0-based)
  #   - start_line=3 means line 4 (1-based) - correct
  #   - end_line=5 means "up to but not including line 5 (0-based)",
  #     i.e., last included line is 4 (0-based) = line 5 (1-based)
  #
  # Conversion to 1-based inclusive range:
  # - @start_line = node.start_line + 1  (0-based inclusive → 1-based inclusive)
  # - @end_line = node.end_line          (0-based exclusive → 1-based inclusive, since exclusive-1+1=same)
  #
  # If Psych/libyaml ever changes end_line to be inclusive, this will need adjustment.
  # See regression test: "does not duplicate keys when destination adds a new nested mapping"
  @start_line = node.start_line + 1 if node.respond_to?(:start_line) && node.start_line
  @end_line = node.end_line if node.respond_to?(:end_line) && node.end_line

  # Handle edge case where end_line might be before start_line
  @end_line = @start_line if @start_line && @end_line && @end_line < @start_line
end

Instance Attribute Details

#end_lineInteger (readonly)

Returns End line (1-based).

Returns:

  • (Integer)

    End line (1-based)



26
27
28
# File 'lib/psych/merge/node_wrapper.rb', line 26

def end_line
  @end_line
end

#inline_commentHash? (readonly)

Returns Inline/trailing comment on the same line.

Returns:

  • (Hash, nil)

    Inline/trailing comment on the same line



20
21
22
# File 'lib/psych/merge/node_wrapper.rb', line 20

def inline_comment
  @inline_comment
end

#keyString? (readonly)

Returns Key name for mapping entries.

Returns:

  • (String, nil)

    Key name for mapping entries



29
30
31
# File 'lib/psych/merge/node_wrapper.rb', line 29

def key
  @key
end

#leading_commentsArray<Hash> (readonly)

Returns Leading comments associated with this node.

Returns:

  • (Array<Hash>)

    Leading comments associated with this node



17
18
19
# File 'lib/psych/merge/node_wrapper.rb', line 17

def leading_comments
  @leading_comments
end

#linesArray<String> (readonly)

Returns Source lines.

Returns:

  • (Array<String>)

    Source lines



32
33
34
# File 'lib/psych/merge/node_wrapper.rb', line 32

def lines
  @lines
end

#nodePsych::Nodes::Node (readonly)

Returns The wrapped Psych node.

Returns:

  • (Psych::Nodes::Node)

    The wrapped Psych node



14
15
16
# File 'lib/psych/merge/node_wrapper.rb', line 14

def node
  @node
end

#start_lineInteger (readonly)

Returns Start line (1-based).

Returns:

  • (Integer)

    Start line (1-based)



23
24
25
# File 'lib/psych/merge/node_wrapper.rb', line 23

def start_line
  @start_line
end

Instance Method Details

#alias?Boolean

Check if this wraps an alias node

Returns:

  • (Boolean)


105
106
107
# File 'lib/psych/merge/node_wrapper.rb', line 105

def alias?
  @node.is_a?(::Psych::Nodes::Alias)
end

#alias_anchorString?

Get the aliased anchor name

Returns:

  • (String, nil)


165
166
167
# File 'lib/psych/merge/node_wrapper.rb', line 165

def alias_anchor
  @node.anchor if alias?
end

#anchorString?

Get the anchor name if this node has one

Returns:

  • (String, nil)


111
112
113
# File 'lib/psych/merge/node_wrapper.rb', line 111

def anchor
  @node.anchor if @node.respond_to?(:anchor)
end

#children(comment_tracker: nil) ⇒ Array<NodeWrapper>

Get children wrapped as NodeWrappers

Parameters:

  • comment_tracker (CommentTracker) (defaults to: nil)

    For associating comments with children

Returns:



118
119
120
121
122
# File 'lib/psych/merge/node_wrapper.rb', line 118

def children(comment_tracker: nil)
  return [] unless @node.respond_to?(:children) && @node.children

  wrap_children(@node.children, comment_tracker)
end

#contentString

Get the content for this node from source lines

Returns:

  • (String)


171
172
173
174
175
# File 'lib/psych/merge/node_wrapper.rb', line 171

def content
  return "" unless @start_line && @end_line

  (@start_line..@end_line).map { |ln| @lines[ln - 1] }.join
end

#freeze_node?Boolean

Check if this is a freeze node

Returns:

  • (Boolean)


81
82
83
# File 'lib/psych/merge/node_wrapper.rb', line 81

def freeze_node?
  false
end

#inspectString

String representation for debugging

Returns:

  • (String)


187
188
189
190
# File 'lib/psych/merge/node_wrapper.rb', line 187

def inspect
  node_type = @node.class.name.split("::").last
  "#<#{self.class.name} type=#{node_type} lines=#{@start_line}..#{@end_line} key=#{@key.inspect}>"
end

#mapping?Boolean

Check if this wraps a mapping node

Returns:

  • (Boolean)


87
88
89
# File 'lib/psych/merge/node_wrapper.rb', line 87

def mapping?
  @node.is_a?(::Psych::Nodes::Mapping)
end

#mapping_entries(comment_tracker: nil) ⇒ Array<Array(NodeWrapper, NodeWrapper)>

Get mapping entries as key-value pairs of NodeWrappers

Parameters:

  • comment_tracker (CommentTracker) (defaults to: nil)

    For associating comments with children

Returns:



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/psych/merge/node_wrapper.rb', line 127

def mapping_entries(comment_tracker: nil)
  return [] unless mapping?

  entries = []
  children = @node.children
  i = 0
  while i < children.length
    key_node = children[i]
    value_node = children[i + 1]
    break unless key_node && value_node

    key_wrapper = wrap_node(key_node, comment_tracker)
    value_wrapper = wrap_node(value_node, comment_tracker, key: extract_key_name(key_node))

    entries << [key_wrapper, value_wrapper]
    i += 2
  end

  entries
end

#scalar?Boolean

Check if this wraps a scalar node

Returns:

  • (Boolean)


99
100
101
# File 'lib/psych/merge/node_wrapper.rb', line 99

def scalar?
  @node.is_a?(::Psych::Nodes::Scalar)
end

#sequence?Boolean

Check if this wraps a sequence node

Returns:

  • (Boolean)


93
94
95
# File 'lib/psych/merge/node_wrapper.rb', line 93

def sequence?
  @node.is_a?(::Psych::Nodes::Sequence)
end

#sequence_items(comment_tracker: nil) ⇒ Array<NodeWrapper>

Get sequence items as NodeWrappers

Parameters:

  • comment_tracker (CommentTracker) (defaults to: nil)

    For associating comments with children

Returns:



151
152
153
154
155
# File 'lib/psych/merge/node_wrapper.rb', line 151

def sequence_items(comment_tracker: nil)
  return [] unless sequence?

  @node.children.map { |child| wrap_node(child, comment_tracker) }
end

#signatureArray?

Generate a signature for this node for matching purposes.
Signatures are used to identify corresponding nodes between template and destination.

Returns:

  • (Array, nil)

    Signature array or nil if not signaturable



75
76
77
# File 'lib/psych/merge/node_wrapper.rb', line 75

def signature
  compute_signature(@node)
end

#to_sString

String representation of the node value.
For scalars, returns the value.
For other nodes, returns inspect.

Returns:

  • (String)


181
182
183
# File 'lib/psych/merge/node_wrapper.rb', line 181

def to_s
  value || inspect
end

#valueString?

Get the scalar value

Returns:

  • (String, nil)


159
160
161
# File 'lib/psych/merge/node_wrapper.rb', line 159

def value
  @node.value if scalar?
end