Class: CloudFormatter::Instance

Inherits:
Object
  • Object
show all
Defined in:
lib/cloud_formatter/instance.rb

Instance Method Summary collapse

Constructor Details

#initialize(template) ⇒ Instance


3
4
5
6
7
# File 'lib/cloud_formatter/instance.rb', line 3

def initialize(template)
  @template   = template
  @type       = nil
  @properties = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(field, *params) ⇒ Object


36
37
38
39
40
41
42
# File 'lib/cloud_formatter/instance.rb', line 36

def method_missing(field, *params)
  if @template.reference_type(field.to_s) == :map
    Reference::Map.new(field)
  else
    @current_map[DSL.format(field)] = DSL.jsonize(params.first)
  end
end

Instance Method Details

#propertiesObject


13
14
15
16
17
18
# File 'lib/cloud_formatter/instance.rb', line 13

def properties
  old_map = @current_map
  @current_map = @properties
  yield
  @current_map = old_map
end

#ref(name) ⇒ Object


28
29
30
# File 'lib/cloud_formatter/instance.rb', line 28

def ref(name)
  Reference.new(name)
end

#tags(tag_map) ⇒ Object


20
21
22
23
24
25
26
# File 'lib/cloud_formatter/instance.rb', line 20

def tags(tag_map)
  list = @current_map[TAGS] ||= []
  tag_map = Hash[tag_map.map { |key, value| [key.to_s, value] }]
  tag_map.keys.sort.each do |key|
    list << {KEY => DSL.format(key), VALUE => DSL.jsonize(tag_map[key])}
  end
end

#to_json_dataObject


32
33
34
# File 'lib/cloud_formatter/instance.rb', line 32

def to_json_data
  {TYPE => @type, PROPERTIES => @properties}
end

#type(value) ⇒ Object


9
10
11
# File 'lib/cloud_formatter/instance.rb', line 9

def type(value)
  @type = value
end