Class: Hash

Inherits:
Object show all
Defined in:
lib/hcl/monkey_patch.rb

Instance Method Summary collapse

Instance Method Details

#to_hcl(indent = 0) ⇒ Object



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
# File 'lib/hcl/monkey_patch.rb', line 22

def to_hcl(indent = 0)
  return "" if self.empty?
  hcl = ""
  spaces = " " * indent

  self.each do |k, v|
    next if v.hcl_object?
    next if v.hcl_list? and v.size > 0 and v.first.hcl_object?

    hcl << spaces
    hcl << HCL.escape_key(k) << " = "
    hcl << v.to_hcl(indent + 4)
    hcl << "\n"
  end

  self.each do |k, v|
    if v.hcl_object?
	key = HCL.escape_key(k)
	hcl << spaces
	hcl << key << " {\n"
	hcl << v.to_hcl(indent + 4)
	hcl << spaces << "}\n"
    end
    if v.hcl_list? and v.size > 0 and v.first.hcl_object?
	key = HCL.escape_key(k)
	hcl << spaces
	hcl << key << " = ["
	v.each do |i|
 if i.hcl_object?
   hcl << "\n" << spaces << "{\n"
 else
   hcl << spaces
 end
 hcl << i.to_hcl(indent + 4)
 if i.hcl_object?
   hcl << spaces << "},\n"
 end
	end
	hcl << spaces << "]\n"
    end
  end

  hcl
end