Module: YAMLTools

Defined in:
lib/yaml_tools.rb,
lib/tools/combine.rb,
lib/tools/compare.rb,
lib/tools/utility.rb

Defined Under Namespace

Classes: Combiner, Comparer

Class Method Summary collapse

Class Method Details

.createDocument(levels) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/tools/utility.rb', line 4

def self.createDocument(levels)
  document = Psych::Nodes::Document.new()
  documentRoot = Psych::Nodes::Mapping.new();
  documentRoot.children.push(*levels)
  document.children << documentRoot

  stream = Psych::Nodes::Stream.new
  stream.children << document
  output = stream.to_yaml

  # Remove initial document start
  output.slice!(0, 4) if (output.start_with?("---\n"))

  output
end

.flatten_merge_keys(s) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/tools/utility.rb', line 20

def self.flatten_merge_keys(s)
  level = []

  sourceChildren = s.each_slice(2).to_a

  mergeKeys = sourceChildren.find_all {|i| i[0].value == "<<" }

  if (mergeKeys.length > 0) then
    if (mergeKeys.length == 1) then
      # Add merge key
      level << mergeKeys.first[0]
      level << mergeKeys.first[1]
    else
      newSequence = Psych::Nodes::Sequence.new(nil, nil, true, Psych::Nodes::Sequence::FLOW)

      mergeKeys.each {|m|
        if (m[1].is_a?(Psych::Nodes::Alias)) then
          newSequence.children << m[1]
        elsif (m[1].is_a?(Psych::Nodes::Sequence)) then
          newSequence.children.concat(m[1].children)
        end
      }

      level << Psych::Nodes::Scalar.new("<<")
      level << newSequence
    end
  end

  sourceChildren.each {|sourcePair|
    sourceKey = sourcePair[0]
    sourceValue = sourcePair[1]

    if (sourceValue.is_a?(Psych::Nodes::Mapping)) then
      childLevel = flatten_merge_keys(sourceValue.children)

      newMapping = Psych::Nodes::Mapping.new(sourceValue.anchor, sourceValue.tag, sourceValue.implicit, sourceValue.style)

      if (childLevel.length > 0) then
        newMapping.children.push(*childLevel)
      end

      level << sourceKey
      level << newMapping
    else
      if (sourceKey.value != "<<") then
        level << sourceKey
        level << sourceValue
      end
    end
  }

  level
end