Class: Psych::Merge::NodeWrapper
- Inherits:
-
Object
- Object
- Psych::Merge::NodeWrapper
- 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.
Instance Attribute Summary collapse
-
#end_line ⇒ Integer
readonly
End line (1-based).
-
#inline_comment ⇒ Hash?
readonly
Inline/trailing comment on the same line.
-
#key ⇒ String?
readonly
Key name for mapping entries.
-
#leading_comments ⇒ Array<Hash>
readonly
Leading comments associated with this node.
-
#lines ⇒ Array<String>
readonly
Source lines.
-
#node ⇒ Psych::Nodes::Node
readonly
The wrapped Psych node.
-
#start_line ⇒ Integer
readonly
Start line (1-based).
Instance Method Summary collapse
-
#alias? ⇒ Boolean
Check if this wraps an alias node.
-
#alias_anchor ⇒ String?
Get the aliased anchor name.
-
#anchor ⇒ String?
Get the anchor name if this node has one.
-
#children(comment_tracker: nil) ⇒ Array<NodeWrapper>
Get children wrapped as NodeWrappers.
-
#content ⇒ String
Get the content for this node from source lines.
-
#freeze_node? ⇒ Boolean
Check if this is a freeze node.
-
#initialize(node, lines:, leading_comments: [], inline_comment: nil, key: nil) ⇒ NodeWrapper
constructor
A new instance of NodeWrapper.
-
#inspect ⇒ String
String representation for debugging.
-
#mapping? ⇒ Boolean
Check if this wraps a mapping node.
-
#mapping_entries(comment_tracker: nil) ⇒ Array<Array(NodeWrapper, NodeWrapper)>
Get mapping entries as key-value pairs of NodeWrappers.
-
#scalar? ⇒ Boolean
Check if this wraps a scalar node.
-
#sequence? ⇒ Boolean
Check if this wraps a sequence node.
-
#sequence_items(comment_tracker: nil) ⇒ Array<NodeWrapper>
Get sequence items as NodeWrappers.
-
#signature ⇒ Array?
Generate a signature for this node for matching purposes.
-
#to_s ⇒ String
String representation of the node value.
-
#value ⇒ String?
Get the scalar value.
Constructor Details
#initialize(node, lines:, leading_comments: [], inline_comment: nil, key: nil) ⇒ NodeWrapper
Returns a new instance of NodeWrapper.
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_line ⇒ Integer (readonly)
Returns End line (1-based).
26 27 28 |
# File 'lib/psych/merge/node_wrapper.rb', line 26 def end_line @end_line end |
#inline_comment ⇒ Hash? (readonly)
Returns 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 |
#key ⇒ String? (readonly)
Returns Key name for mapping entries.
29 30 31 |
# File 'lib/psych/merge/node_wrapper.rb', line 29 def key @key end |
#leading_comments ⇒ Array<Hash> (readonly)
Returns Leading comments associated with this node.
17 18 19 |
# File 'lib/psych/merge/node_wrapper.rb', line 17 def leading_comments @leading_comments end |
#lines ⇒ Array<String> (readonly)
Returns Source lines.
32 33 34 |
# File 'lib/psych/merge/node_wrapper.rb', line 32 def lines @lines end |
#node ⇒ Psych::Nodes::Node (readonly)
Returns The wrapped Psych node.
14 15 16 |
# File 'lib/psych/merge/node_wrapper.rb', line 14 def node @node end |
#start_line ⇒ Integer (readonly)
Returns 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
105 106 107 |
# File 'lib/psych/merge/node_wrapper.rb', line 105 def alias? @node.is_a?(::Psych::Nodes::Alias) end |
#alias_anchor ⇒ String?
Get the aliased anchor name
165 166 167 |
# File 'lib/psych/merge/node_wrapper.rb', line 165 def alias_anchor @node.anchor if alias? end |
#anchor ⇒ String?
Get the anchor name if this node has one
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
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 |
#content ⇒ String
Get the content for this node from source lines
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
81 82 83 |
# File 'lib/psych/merge/node_wrapper.rb', line 81 def freeze_node? false end |
#inspect ⇒ String
String representation for debugging
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
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
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
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
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
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 |
#signature ⇒ Array?
Generate a signature for this node for matching purposes.
Signatures are used to identify corresponding nodes between template and destination.
75 76 77 |
# File 'lib/psych/merge/node_wrapper.rb', line 75 def signature compute_signature(@node) end |
#to_s ⇒ String
String representation of the node value.
For scalars, returns the value.
For other nodes, returns inspect.
181 182 183 |
# File 'lib/psych/merge/node_wrapper.rb', line 181 def to_s value || inspect end |
#value ⇒ String?
Get the scalar value
159 160 161 |
# File 'lib/psych/merge/node_wrapper.rb', line 159 def value @node.value if scalar? end |