Class: TreeHaver::Backends::Psych::Node

Inherits:
TreeHaver::Base::Node show all
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

#inner_node, #lines, #source

Instance Method Summary collapse

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

Returns:

  • (Boolean)


314
315
316
# File 'lib/tree_haver/backends/psych.rb', line 314

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

#anchorString?

Psych-specific: Get the anchor name for Alias/anchored nodes

Returns:

  • (String, nil)

    Anchor name



272
273
274
# File 'lib/tree_haver/backends/psych.rb', line 272

def anchor
  inner_node.anchor if inner_node.respond_to?(:anchor)
end

#childrenArray<Node>

Get child nodes

Returns:

  • (Array<Node>)

    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_byteInteger

Get end byte offset

Returns:

  • (Integer)

    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_pointTreeHaver::Base::Point

Get end point (row, column) - 0-based

Returns:



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

#kindString

Alias for type (API compatibility)

Returns:

  • (String)

    node type



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)

Returns:

  • (Boolean)


293
294
295
# File 'lib/tree_haver/backends/psych.rb', line 293

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

#mapping_entriesArray<Array(Node, Node)>

Psych-specific: Get mapping entries as key-value pairs

For Mapping nodes, children alternate key, value, key, value…

Returns:

  • (Array<Array(Node, Node)>)

    Key-value pairs



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)

Returns:

  • (Boolean)


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)

Returns:

  • (Boolean)


300
301
302
# File 'lib/tree_haver/backends/psych.rb', line 300

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

#start_byteInteger

Get start byte offset

Returns:

  • (Integer)

    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_pointTreeHaver::Base::Point

Get start point (row, column) - 0-based

Returns:



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

#tagString?

Psych-specific: Get the tag for tagged nodes

Returns:

  • (String, nil)

    Tag



279
280
281
# File 'lib/tree_haver/backends/psych.rb', line 279

def tag
  inner_node.tag if inner_node.respond_to?(:tag)
end

#textString

Get the text content of this node

Returns:

  • (String)

    Node text



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

#typeString

Get the node type as a string

Returns:

  • (String)

    Node type



195
196
197
# File 'lib/tree_haver/backends/psych.rb', line 195

def type
  inner_node.class.name.split("::").last.downcase
end

#valueString?

Psych-specific: Get the scalar value

Returns:

  • (String, nil)

    Value for scalar nodes



286
287
288
# File 'lib/tree_haver/backends/psych.rb', line 286

def value
  inner_node.value if inner_node.respond_to?(:value)
end