Class: Yaml::Merge::NodeWrapper

Inherits:
Ast::Merge::NodeWrapperBase
  • Object
show all
Defined in:
lib/yaml/merge/node_wrapper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#document_indexObject (readonly)

Returns the value of attribute document_index.



6
7
8
# File 'lib/yaml/merge/node_wrapper.rb', line 6

def document_index
  @document_index
end

Instance Method Details

#body_nodeObject



50
51
52
53
54
55
56
# File 'lib/yaml/merge/node_wrapper.rb', line 50

def body_node
  return unless document?

  semantic_children.find { |child| %w[block_node flow_node].include?(child.type.to_s) }&.then do |node|
    wrap_child(node).unwrap_value_node
  end
end

#comment?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/yaml/merge/node_wrapper.rb', line 28

def comment?
  type?(:comment)
end

#container?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/yaml/merge/node_wrapper.rb', line 32

def container?
  document? || mapping?
end

#document?Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/yaml/merge/node_wrapper.rb', line 12

def document?
  type?(:document)
end

#header_lines_before(child) ⇒ Object



100
101
102
103
104
105
106
107
108
# File 'lib/yaml/merge/node_wrapper.rb', line 100

def header_lines_before(child)
  return [] unless start_line && child&.start_line

  first = start_line
  last = child.start_line - 1
  return [] if last < first

  (first..last).filter_map { |line_num| @lines[line_num - 1] }
end

#key_nameObject



36
37
38
39
40
41
# File 'lib/yaml/merge/node_wrapper.rb', line 36

def key_name
  return unless mapping_pair?

  key = semantic_children.first
  scalar_text(key)
end

#mapping?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/yaml/merge/node_wrapper.rb', line 16

def mapping?
  %i[block_mapping flow_mapping].include?(type)
end

#mapping_pair?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/yaml/merge/node_wrapper.rb', line 24

def mapping_pair?
  type.to_s.end_with?('mapping_pair')
end

#mapping_pairsObject



71
72
73
74
75
76
77
78
# File 'lib/yaml/merge/node_wrapper.rb', line 71

def mapping_pairs
  return [] unless mapping?

  semantic_children.filter_map do |child|
    wrapper = wrap_child(child)
    wrapper if wrapper.mapping_pair?
  end
end

#mergeable_childrenObject



80
81
82
83
84
85
86
87
88
89
# File 'lib/yaml/merge/node_wrapper.rb', line 80

def mergeable_children
  if document?
    body = body_node
    body&.mapping? ? body.mapping_pairs : []
  elsif mapping?
    mapping_pairs
  else
    []
  end
end

#process_additional_options(options) ⇒ Object



8
9
10
# File 'lib/yaml/merge/node_wrapper.rb', line 8

def process_additional_options(options)
  @document_index = options[:document_index]
end

#semantic_childrenObject



91
92
93
94
95
96
97
98
# File 'lib/yaml/merge/node_wrapper.rb', line 91

def semantic_children
  return [] unless @node.respond_to?(:children)

  @node.children.select do |child|
    (!child.respond_to?(:named?) || child.named?) &&
      child.type.to_s != 'comment'
  end
end

#sequence?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/yaml/merge/node_wrapper.rb', line 20

def sequence?
  %i[block_sequence flow_sequence].include?(type)
end

#unwrap_value_nodeObject



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/yaml/merge/node_wrapper.rb', line 58

def unwrap_value_node
  current = self
  loop do
    break unless %i[block_node flow_node].include?(current.type)

    child = current.semantic_children.find { |candidate| !%w[anchor tag].include?(candidate.type.to_s) }
    break unless child

    current = current.wrap_child(child)
  end
  current
end

#value_nodeObject



43
44
45
46
47
48
# File 'lib/yaml/merge/node_wrapper.rb', line 43

def value_node
  return unless mapping_pair?

  value = semantic_children[1]
  value ? wrap_child(value) : nil
end