Class: Psych::Merge::MappingEntry

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

Overview

Represents a key-value entry in a YAML mapping

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key:, value:, lines:, comment_tracker:) ⇒ MappingEntry

Returns a new instance of MappingEntry.

Parameters:



347
348
349
350
351
352
# File 'lib/psych/merge/file_analysis.rb', line 347

def initialize(key:, value:, lines:, comment_tracker:)
  @key = key
  @value = value
  @lines = lines
  @comment_tracker = comment_tracker
end

Instance Attribute Details

#comment_trackerCommentTracker (readonly)

Returns Comment tracker.

Returns:



341
342
343
# File 'lib/psych/merge/file_analysis.rb', line 341

def comment_tracker
  @comment_tracker
end

#keyNodeWrapper (readonly)

Returns The key node.

Returns:



332
333
334
# File 'lib/psych/merge/file_analysis.rb', line 332

def key
  @key
end

#linesArray<String> (readonly)

Returns Source lines.

Returns:

  • (Array<String>)

    Source lines



338
339
340
# File 'lib/psych/merge/file_analysis.rb', line 338

def lines
  @lines
end

#valueNodeWrapper (readonly)

Returns The value node.

Returns:



335
336
337
# File 'lib/psych/merge/file_analysis.rb', line 335

def value
  @value
end

Instance Method Details

#anchorString?

Get the anchor if present

Returns:

  • (String, nil)


448
449
450
# File 'lib/psych/merge/file_analysis.rb', line 448

def anchor
  @value.anchor
end

#comment_attachmentAst::Merge::Comment::Attachment

Get a passive shared comment attachment for this mapping entry.

Returns:

  • (Ast::Merge::Comment::Attachment)


357
358
359
360
361
362
363
364
365
# File 'lib/psych/merge/file_analysis.rb', line 357

def comment_attachment
  @comment_attachment ||= @comment_tracker.comment_attachment_for(
    self,
    line_num: @key.start_line,
    leading_comments: @comment_tracker.leading_comments_before(@key.start_line || 1),
    inline_comment: @comment_tracker.inline_comment_at(@key.start_line || 1),
    key_name: key_name,
  )
end

#contentString

Get the content for this entry

Returns:

  • (String)


405
406
407
408
409
# File 'lib/psych/merge/file_analysis.rb', line 405

def content
  return "" unless start_line && end_line

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

#end_lineInteger?

Get the end line (from the value)

Returns:

  • (Integer, nil)


391
392
393
# File 'lib/psych/merge/file_analysis.rb', line 391

def end_line
  @value.end_line || @key.end_line
end

#freeze_node?Boolean

Check if this is a freeze node

Returns:

  • (Boolean)


424
425
426
# File 'lib/psych/merge/file_analysis.rb', line 424

def freeze_node?
  false
end

#inline_comment_regionAst::Merge::Comment::Region?

Returns:

  • (Ast::Merge::Comment::Region, nil)


373
374
375
# File 'lib/psych/merge/file_analysis.rb', line 373

def inline_comment_region
  comment_attachment.inline_region
end

#inspectString

String representation

Returns:

  • (String)


454
455
456
# File 'lib/psych/merge/file_analysis.rb', line 454

def inspect
  "#<#{self.class.name} key=#{key_name.inspect} lines=#{start_line}..#{end_line}>"
end

#key_nameString?

Get the key name as a string

Returns:

  • (String, nil)


379
380
381
# File 'lib/psych/merge/file_analysis.rb', line 379

def key_name
  @key.value
end

#leading_comment_regionAst::Merge::Comment::Region?

Returns:

  • (Ast::Merge::Comment::Region, nil)


368
369
370
# File 'lib/psych/merge/file_analysis.rb', line 368

def leading_comment_region
  comment_attachment.leading_region
end

#line_rangeRange?

Get the line range

Returns:

  • (Range, nil)


397
398
399
400
401
# File 'lib/psych/merge/file_analysis.rb', line 397

def line_range
  return unless start_line && end_line

  start_line..end_line
end

#locationObject

Location-like object for compatibility



418
419
420
# File 'lib/psych/merge/file_analysis.rb', line 418

def location
  @location ||= FreezeNode::Location.new(start_line, end_line)
end

#mapping?Boolean

Check if this is a mapping

Returns:

  • (Boolean)


430
431
432
# File 'lib/psych/merge/file_analysis.rb', line 430

def mapping?
  @value.mapping?
end

#scalar?Boolean

Check if this is a scalar

Returns:

  • (Boolean)


442
443
444
# File 'lib/psych/merge/file_analysis.rb', line 442

def scalar?
  @value.scalar?
end

#sequence?Boolean

Check if this is a sequence

Returns:

  • (Boolean)


436
437
438
# File 'lib/psych/merge/file_analysis.rb', line 436

def sequence?
  @value.sequence?
end

#signatureArray

Generate signature for this entry

Returns:

  • (Array)


413
414
415
# File 'lib/psych/merge/file_analysis.rb', line 413

def signature
  [:mapping_entry, key_name]
end

#start_lineInteger?

Get the start line (from the key)

Returns:

  • (Integer, nil)


385
386
387
# File 'lib/psych/merge/file_analysis.rb', line 385

def start_line
  leading_comment_region&.start_line || @key.start_line
end