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:



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

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:



446
447
448
# File 'lib/psych/merge/file_analysis.rb', line 446

def comment_tracker
  @comment_tracker
end

#keyNodeWrapper (readonly)

Returns The key node.

Returns:



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

def key
  @key
end

#linesArray<String> (readonly)

Returns Source lines.

Returns:

  • (Array<String>)

    Source lines



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

def lines
  @lines
end

#valueNodeWrapper (readonly)

Returns The value node.

Returns:



440
441
442
# File 'lib/psych/merge/file_analysis.rb', line 440

def value
  @value
end

Instance Method Details

#anchorString?

Get the anchor if present

Returns:

  • (String, nil)


553
554
555
# File 'lib/psych/merge/file_analysis.rb', line 553

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)


462
463
464
465
466
467
468
469
470
# File 'lib/psych/merge/file_analysis.rb', line 462

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)


510
511
512
513
514
# File 'lib/psych/merge/file_analysis.rb', line 510

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)


496
497
498
# File 'lib/psych/merge/file_analysis.rb', line 496

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

#freeze_node?Boolean

Check if this is a freeze node

Returns:

  • (Boolean)


529
530
531
# File 'lib/psych/merge/file_analysis.rb', line 529

def freeze_node?
  false
end

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

Returns:

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


478
479
480
# File 'lib/psych/merge/file_analysis.rb', line 478

def inline_comment_region
  comment_attachment.inline_region
end

#inspectString

String representation

Returns:

  • (String)


559
560
561
# File 'lib/psych/merge/file_analysis.rb', line 559

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)


484
485
486
# File 'lib/psych/merge/file_analysis.rb', line 484

def key_name
  @key.value
end

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

Returns:

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


473
474
475
# File 'lib/psych/merge/file_analysis.rb', line 473

def leading_comment_region
  comment_attachment.leading_region
end

#line_rangeRange?

Get the line range

Returns:

  • (Range, nil)


502
503
504
505
506
# File 'lib/psych/merge/file_analysis.rb', line 502

def line_range
  return unless start_line && end_line

  start_line..end_line
end

#locationObject

Location-like object for compatibility



523
524
525
# File 'lib/psych/merge/file_analysis.rb', line 523

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

#mapping?Boolean

Check if this is a mapping

Returns:

  • (Boolean)


535
536
537
# File 'lib/psych/merge/file_analysis.rb', line 535

def mapping?
  @value.mapping?
end

#scalar?Boolean

Check if this is a scalar

Returns:

  • (Boolean)


547
548
549
# File 'lib/psych/merge/file_analysis.rb', line 547

def scalar?
  @value.scalar?
end

#sequence?Boolean

Check if this is a sequence

Returns:

  • (Boolean)


541
542
543
# File 'lib/psych/merge/file_analysis.rb', line 541

def sequence?
  @value.sequence?
end

#signatureArray

Generate signature for this entry

Returns:

  • (Array)


518
519
520
# File 'lib/psych/merge/file_analysis.rb', line 518

def signature
  [:mapping_entry, key_name]
end

#start_lineInteger?

Get the start line (from the key)

Returns:

  • (Integer, nil)


490
491
492
# File 'lib/psych/merge/file_analysis.rb', line 490

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