Class: TreeHaver::Backends::Psych::Node
- Inherits:
-
TreeHaver::Base::Node
- Object
- TreeHaver::Base::Node
- TreeHaver::Backends::Psych::Node
- Defined in:
- lib/tree_haver/backends/psych.rb
Overview
Psych node wrapper
Wraps Psych::Nodes::* classes to provide TreeHaver::Node-compatible interface.
Psych node types:
-
Stream: Root container
-
Document: YAML document (multiple per stream possible)
-
Mapping: Hash/object
-
Sequence: Array/list
-
Scalar: Primitive value (string, number, boolean, null)
-
Psych::Nodes::Alias: YAML anchor reference
Instance Attribute Summary
Attributes inherited from TreeHaver::Base::Node
Instance Method Summary collapse
-
#alias? ⇒ Boolean
Psych-specific: Check if this is an alias.
-
#anchor ⇒ String?
Psych-specific: Get the anchor name for Alias/anchored nodes.
-
#children ⇒ Array<Node>
Get child nodes.
-
#end_byte ⇒ Integer
Get end byte offset.
-
#end_point ⇒ TreeHaver::Base::Point
Get end point (row, column) - 0-based.
-
#kind ⇒ String
Alias for type (API compatibility).
-
#mapping? ⇒ Boolean
Psych-specific: Check if this is a mapping (hash).
-
#mapping_entries ⇒ Array<Array(Node, Node)>
Psych-specific: Get mapping entries as key-value pairs.
-
#scalar? ⇒ Boolean
Psych-specific: Check if this is a scalar (primitive).
-
#sequence? ⇒ Boolean
Psych-specific: Check if this is a sequence (array).
-
#start_byte ⇒ Integer
Get start byte offset.
-
#start_point ⇒ TreeHaver::Base::Point
Get start point (row, column) - 0-based.
-
#tag ⇒ String?
Psych-specific: Get the tag for tagged nodes.
-
#text ⇒ String
Get the text content of this node.
-
#type ⇒ String
Get the node type as a string.
-
#value ⇒ String?
Psych-specific: Get the scalar value.
Methods inherited from TreeHaver::Base::Node
#<=>, #==, #child, #child_by_field_name, #child_count, #each, #end_line, #first_child, #has_error?, #initialize, #inspect, #last_child, #missing?, #named?, #next_sibling, #parent, #prev_sibling, #source_position, #start_line, #to_s
Constructor Details
This class inherits a constructor from TreeHaver::Base::Node
Instance Method Details
#alias? ⇒ Boolean
Psych-specific: Check if this is an alias
314 315 316 |
# File 'lib/tree_haver/backends/psych.rb', line 314 def alias? inner_node.is_a?(::Psych::Nodes::Alias) end |
#anchor ⇒ String?
Psych-specific: Get the anchor name for Alias/anchored nodes
272 273 274 |
# File 'lib/tree_haver/backends/psych.rb', line 272 def anchor inner_node.anchor if inner_node.respond_to?(:anchor) end |
#children ⇒ Array<Node>
Get child nodes
223 224 225 226 227 |
# File 'lib/tree_haver/backends/psych.rb', line 223 def children return [] unless inner_node.respond_to?(:children) && inner_node.children inner_node.children.map { |child| Node.new(child, source: source, lines: lines) } end |
#end_byte ⇒ Integer
Get end byte offset
243 244 245 246 247 248 249 |
# File 'lib/tree_haver/backends/psych.rb', line 243 def end_byte return start_byte + text.bytesize unless inner_node.respond_to?(:end_line) line = inner_node.end_line || 0 col = inner_node.end_column || 0 calculate_byte_offset(line, col) end |
#end_point ⇒ TreeHaver::Base::Point
Get end point (row, column) - 0-based
263 264 265 266 267 |
# File 'lib/tree_haver/backends/psych.rb', line 263 def end_point row = (inner_node.respond_to?(:end_line) ? inner_node.end_line : 0) || 0 col = (inner_node.respond_to?(:end_column) ? inner_node.end_column : 0) || 0 TreeHaver::Base::Point.new(row, col) end |
#kind ⇒ String
Alias for type (API compatibility)
201 202 203 |
# File 'lib/tree_haver/backends/psych.rb', line 201 def kind type end |
#mapping? ⇒ Boolean
Psych-specific: Check if this is a mapping (hash)
293 294 295 |
# File 'lib/tree_haver/backends/psych.rb', line 293 def mapping? inner_node.is_a?(::Psych::Nodes::Mapping) end |
#mapping_entries ⇒ Array<Array(Node, Node)>
Psych-specific: Get mapping entries as key-value pairs
For Mapping nodes, children alternate key, value, key, value…
323 324 325 326 327 328 329 330 331 |
# File 'lib/tree_haver/backends/psych.rb', line 323 def mapping_entries return [] unless mapping? pairs = [] children.each_slice(2) do |key, val| pairs << [key, val] if key && val end pairs end |
#scalar? ⇒ Boolean
Psych-specific: Check if this is a scalar (primitive)
307 308 309 |
# File 'lib/tree_haver/backends/psych.rb', line 307 def scalar? inner_node.is_a?(::Psych::Nodes::Scalar) end |
#sequence? ⇒ Boolean
Psych-specific: Check if this is a sequence (array)
300 301 302 |
# File 'lib/tree_haver/backends/psych.rb', line 300 def sequence? inner_node.is_a?(::Psych::Nodes::Sequence) end |
#start_byte ⇒ Integer
Get start byte offset
232 233 234 235 236 237 238 |
# File 'lib/tree_haver/backends/psych.rb', line 232 def start_byte return 0 unless inner_node.respond_to?(:start_line) line = inner_node.start_line || 0 col = inner_node.start_column || 0 calculate_byte_offset(line, col) end |
#start_point ⇒ TreeHaver::Base::Point
Get start point (row, column) - 0-based
254 255 256 257 258 |
# File 'lib/tree_haver/backends/psych.rb', line 254 def start_point row = (inner_node.respond_to?(:start_line) ? inner_node.start_line : 0) || 0 col = (inner_node.respond_to?(:start_column) ? inner_node.start_column : 0) || 0 TreeHaver::Base::Point.new(row, col) end |
#tag ⇒ String?
Psych-specific: Get the tag for tagged nodes
279 280 281 |
# File 'lib/tree_haver/backends/psych.rb', line 279 def tag inner_node.tag if inner_node.respond_to?(:tag) end |
#text ⇒ String
Get the text content of this node
208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/tree_haver/backends/psych.rb', line 208 def text case inner_node when ::Psych::Nodes::Scalar inner_node.value.to_s when ::Psych::Nodes::Alias "*#{inner_node.anchor}" else # For container nodes, extract from source using location extract_text_from_location end end |
#type ⇒ String
Get the node type as a string
195 196 197 |
# File 'lib/tree_haver/backends/psych.rb', line 195 def type inner_node.class.name.split("::").last.downcase end |
#value ⇒ String?
Psych-specific: Get the scalar value
286 287 288 |
# File 'lib/tree_haver/backends/psych.rb', line 286 def value inner_node.value if inner_node.respond_to?(:value) end |