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?, #native_type, #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
318 319 320 |
# File 'lib/tree_haver/backends/psych.rb', line 318 def alias? inner_node.is_a?(::Psych::Nodes::Alias) end |
#anchor ⇒ String?
Psych-specific: Get the anchor name for Alias/anchored nodes
276 277 278 |
# File 'lib/tree_haver/backends/psych.rb', line 276 def anchor inner_node.anchor if inner_node.respond_to?(:anchor) end |
#children ⇒ Array<Node>
Get child nodes
227 228 229 230 231 |
# File 'lib/tree_haver/backends/psych.rb', line 227 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
247 248 249 250 251 252 253 |
# File 'lib/tree_haver/backends/psych.rb', line 247 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
267 268 269 270 271 |
# File 'lib/tree_haver/backends/psych.rb', line 267 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)
205 206 207 |
# File 'lib/tree_haver/backends/psych.rb', line 205 def kind type end |
#mapping? ⇒ Boolean
Psych-specific: Check if this is a mapping (hash)
297 298 299 |
# File 'lib/tree_haver/backends/psych.rb', line 297 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...
327 328 329 330 331 332 333 334 335 |
# File 'lib/tree_haver/backends/psych.rb', line 327 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)
311 312 313 |
# File 'lib/tree_haver/backends/psych.rb', line 311 def scalar? inner_node.is_a?(::Psych::Nodes::Scalar) end |
#sequence? ⇒ Boolean
Psych-specific: Check if this is a sequence (array)
304 305 306 |
# File 'lib/tree_haver/backends/psych.rb', line 304 def sequence? inner_node.is_a?(::Psych::Nodes::Sequence) end |
#start_byte ⇒ Integer
Get start byte offset
236 237 238 239 240 241 242 |
# File 'lib/tree_haver/backends/psych.rb', line 236 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
258 259 260 261 262 |
# File 'lib/tree_haver/backends/psych.rb', line 258 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
283 284 285 |
# File 'lib/tree_haver/backends/psych.rb', line 283 def tag inner_node.tag if inner_node.respond_to?(:tag) end |
#text ⇒ String
Get the text content of this node
212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/tree_haver/backends/psych.rb', line 212 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
199 200 201 |
# File 'lib/tree_haver/backends/psych.rb', line 199 def type inner_node.class.name.split('::').last.downcase end |
#value ⇒ String?
Psych-specific: Get the scalar value
290 291 292 |
# File 'lib/tree_haver/backends/psych.rb', line 290 def value inner_node.value if inner_node.respond_to?(:value) end |