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:



266
267
268
269
270
271
# File 'lib/psych/merge/file_analysis.rb', line 266

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:



260
261
262
# File 'lib/psych/merge/file_analysis.rb', line 260

def comment_tracker
  @comment_tracker
end

#keyNodeWrapper (readonly)

Returns The key node.

Returns:



251
252
253
# File 'lib/psych/merge/file_analysis.rb', line 251

def key
  @key
end

#linesArray<String> (readonly)

Returns Source lines.

Returns:

  • (Array<String>)

    Source lines



257
258
259
# File 'lib/psych/merge/file_analysis.rb', line 257

def lines
  @lines
end

#valueNodeWrapper (readonly)

Returns The value node.

Returns:



254
255
256
# File 'lib/psych/merge/file_analysis.rb', line 254

def value
  @value
end

Instance Method Details

#anchorString?

Get the anchor if present

Returns:

  • (String, nil)


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

def anchor
  @value.anchor
end

#contentString

Get the content for this entry

Returns:

  • (String)


307
308
309
310
311
# File 'lib/psych/merge/file_analysis.rb', line 307

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)


293
294
295
# File 'lib/psych/merge/file_analysis.rb', line 293

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

#freeze_node?Boolean

Check if this is a freeze node

Returns:

  • (Boolean)


326
327
328
# File 'lib/psych/merge/file_analysis.rb', line 326

def freeze_node?
  false
end

#inspectString

String representation

Returns:

  • (String)


356
357
358
# File 'lib/psych/merge/file_analysis.rb', line 356

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)


275
276
277
# File 'lib/psych/merge/file_analysis.rb', line 275

def key_name
  @key.value
end

#line_rangeRange?

Get the line range

Returns:

  • (Range, nil)


299
300
301
302
303
# File 'lib/psych/merge/file_analysis.rb', line 299

def line_range
  return unless start_line && end_line

  start_line..end_line
end

#locationObject

Location-like object for compatibility



320
321
322
# File 'lib/psych/merge/file_analysis.rb', line 320

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

#mapping?Boolean

Check if this is a mapping

Returns:

  • (Boolean)


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

def mapping?
  @value.mapping?
end

#scalar?Boolean

Check if this is a scalar

Returns:

  • (Boolean)


344
345
346
# File 'lib/psych/merge/file_analysis.rb', line 344

def scalar?
  @value.scalar?
end

#sequence?Boolean

Check if this is a sequence

Returns:

  • (Boolean)


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

def sequence?
  @value.sequence?
end

#signatureArray

Generate signature for this entry

Returns:

  • (Array)


315
316
317
# File 'lib/psych/merge/file_analysis.rb', line 315

def signature
  [:mapping_entry, key_name]
end

#start_lineInteger?

Get the start line (from the key)

Returns:

  • (Integer, nil)


281
282
283
284
285
286
287
288
289
# File 'lib/psych/merge/file_analysis.rb', line 281

def start_line
  # Include leading comments in start line
  leading = @comment_tracker.leading_comments_before(@key.start_line || 1)
  if leading.any?
    leading.first[:line]
  else
    @key.start_line
  end
end