Class: Aws::Resources::Source

Inherits:
Object
  • Object
show all
Defined in:
lib/aws-sdk-resources/source.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(definition, file = nil) ⇒ Source

Returns a new instance of Source.



7
8
9
10
# File 'lib/aws-sdk-resources/source.rb', line 7

def initialize(definition, file = nil)
  @definition = definition
  @file = file
end

Instance Attribute Details

#definitionHash (readonly)

Returns:

  • (Hash)


13
14
15
# File 'lib/aws-sdk-resources/source.rb', line 13

def definition
  @definition
end

#fileString? (readonly)

Returns:

  • (String, nil)


16
17
18
# File 'lib/aws-sdk-resources/source.rb', line 16

def file
  @file
end

Instance Method Details

#formatObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/aws-sdk-resources/source.rb', line 18

def format
  json = JSON.pretty_generate(definition, indent: '  ', space: '')
  stack = [[]]
  json.lines.each do |line|
    if line.match(/({|\[)$/)
      stack.push([])
    end
    stack.last.push(line)
    if line.match(/(}|\]),?$/)
      frame = stack.pop
      if frame.size == 3 && !frame[1].match(/[{}]/)
        frame = [frame[0].rstrip, '', frame[1].strip, '', frame[2].lstrip]
      end
      stack.last.push(frame.join)
    end
  end
  stack.last.join
end