Module: CfInner

Instance Method Summary collapse

Instance Method Details

#additional_indentObject



8
9
10
# File 'lib/cf_factory/base/cf_inner.rb', line 8

def additional_indent
  0
end

#generateObject



32
33
34
35
36
37
38
39
40
# File 'lib/cf_factory/base/cf_inner.rb', line 32

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

  @result += "\n#{indent}        }"
end

#generate_nameObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/cf_factory/base/cf_inner.rb', line 12

def generate_name()
  indent = " "*additional_indent()
  @result = "#{indent}"
  @result += "#{indent}{\n"
  attributes = self.get_cf_attributes
  @result += "#{indent}          \"#{@name}\" : {\n"
  attributes.keys.each() {|key|
    value = attributes[key]
    if (not (value.class.to_s == "String")) and value.method_defined? :get_cf_attributes
      @result += "#{indent}            \"#{key}\" : #{value.get_cf_attributes},\n}"
    else
      @result += "#{indent}            \"#{key}\" : #{set_quotes(value)},\n"
    end
  }
  
  @result = @result.chomp.chomp(",")
  @result += "\n#{indent}      }"
  @result += "\n#{indent}        }"
end

#get_cf_attributesObject

Raises:

  • (Exception)


4
5
6
# File 'lib/cf_factory/base/cf_inner.rb', line 4

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

#hash_to_string(hash, indent = 0) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/cf_factory/base/cf_inner.rb', line 42

def hash_to_string(hash, indent=0)
  output = ""
  hash.keys.each() do |key|
    value = hash[key]
    output += " " * indent
    case value.class.to_s
    when "Hash"
      output += "        \"#{key}\" : \n{#{hash_to_string(value,indent+5)}},\n"
    else
      output += "        \"#{key}\" : #{set_quotes(value)},\n"
    end
  end
  output
  output = output.chomp().chomp(",")

end

#set_quotes(value) ⇒ Object

Sets leading and trailing quotes



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/cf_factory/base/cf_inner.rb', line 61

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