Module: CloudFormatter::DSL

Included in:
Spec
Defined in:
lib/cloud_formatter/dsl.rb

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(field, value) ⇒ Object


44
45
46
# File 'lib/cloud_formatter/dsl.rb', line 44

def method_missing(field, value)
  instance_variable_set("@#{field}", value)
end

Class Method Details

.format(key) ⇒ Object


3
4
5
# File 'lib/cloud_formatter/dsl.rb', line 3

def self.format(key)
  key.to_s.gsub(/(.)_(.)/) { $1 + $2.upcase }.gsub(/^(.)/) { $1.upcase }
end

.jsonize(value) ⇒ Object


7
8
9
10
11
12
13
# File 'lib/cloud_formatter/dsl.rb', line 7

def self.jsonize(value)
  case value
    when Array then value.map { |v| jsonize v }
    else
      value.respond_to?(:to_json_data) ? value.to_json_data : value
  end
end

Instance Method Details

#add_instance(resource_type, name, *params) ⇒ Object


39
40
41
42
# File 'lib/cloud_formatter/dsl.rb', line 39

def add_instance(resource_type, name, *params)
  descriptor = _resource_descriptors[resource_type]
  _instances[name] = descriptor.create(params)
end

#create(&block) ⇒ Object


23
24
25
# File 'lib/cloud_formatter/dsl.rb', line 23

def create(&block)
  Creator.new(self).instance_eval(&block)
end

#mappings(&block) ⇒ Object


15
16
17
# File 'lib/cloud_formatter/dsl.rb', line 15

def mappings(&block)
  _mappings.instance_eval(&block)
end

#reference_type(name) ⇒ Object


31
32
33
34
35
36
37
# File 'lib/cloud_formatter/dsl.rb', line 31

def reference_type(name)
  if _mappings.has_key?(name)
    :map
  elsif _resource_descriptors.has_key?(name)
    :resource
  end
end

#resource_type(name, &block) ⇒ Object


19
20
21
# File 'lib/cloud_formatter/dsl.rb', line 19

def resource_type(name, &block)
  _resource_descriptors[name.to_s] = ResourceDescriptor.new(self, name, &block)
end

#to_jsonObject


27
28
29
# File 'lib/cloud_formatter/dsl.rb', line 27

def to_json
  JSON.pretty_generate(generate_spec)
end