Module: CfNamedInner

Included in:
CfFactory::CfAsUpdatePolicy
Defined in:
lib/cf_factory/base/cf_named_inner.rb

Overview

Defines a named JSON resource with attributes to be defined in get_cf_attributes(). NamedInner objects can be passed to other NamedInner objects as attributes for recursive definitions.

Instance Method Summary collapse

Instance Method Details

#additional_indentObject



11
12
13
14
15
16
# File 'lib/cf_factory/base/cf_named_inner.rb', line 11

def additional_indent()
  if @indent.nil?
    @indent = 0
  end
  @indent
end

#generateObject



22
23
24
25
26
27
28
29
# File 'lib/cf_factory/base/cf_named_inner.rb', line 22

def generate()
  indent = " "*self.additional_indent()
  @result =  "#{indent}\"#{@name}\" : {\n"
  attributes = self.get_cf_attributes
  @result += hash_to_string(attributes)
  @result += "\n#{indent}}"
  @result
end

#get_cf_attributesObject

Raises:

  • (Exception)


7
8
9
# File 'lib/cf_factory/base/cf_named_inner.rb', line 7

def get_cf_attributes
  raise Exception.new("must be defined")
end

#hash_to_string(hash, indent = "") ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/cf_factory/base/cf_named_inner.rb', line 31

def hash_to_string(hash, indent="")
  output = ""
  indent = " "*additional_indent()
  hash.keys.each() do |key|
    value = hash[key]
    output += indent
    #puts "value.class.to_s = #{value.class.to_s} indent = #{indent.size}"
    case value.class.to_s
      when "Hash"
        output += "     \"#{key}\" : \n{#{hash_to_string(value,indent+5)}},\n"
      else
        if value.class.to_s == self.class.to_s
          #recursive usage
          output += "     \"#{key}\" : { \n#{value.generate},\n"
        else
          #any other primitive type
          output += "     \"#{key}\" : #{set_quotes(value)},\n"
        end
    end
  end
  output
  output = output.chomp().chomp(",")

end

#set_indent(indent) ⇒ Object



18
19
20
# File 'lib/cf_factory/base/cf_named_inner.rb', line 18

def set_indent(indent)
  @indent = indent
end

#set_quotes(value) ⇒ Object

Sets leading and trailing quotes



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/cf_factory/base/cf_named_inner.rb', line 58

def set_quotes(value)
  if value.class.to_s == "String"
    if value.delete(" ").start_with?("{") || value.delete(" ").start_with?("[")
      value
    else
      "\"#{value}\""
    end
  else
    value
  end
end