Module: CloudBuilder::DSL

Included in:
Spec, Stack
Defined in:
lib/cloud_builder/dsl.rb

Instance Attribute Summary collapse

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



99
100
101
# File 'lib/cloud_builder/dsl.rb', line 99

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

Instance Attribute Details

#globalsObject

Returns the value of attribute globals.



7
8
9
# File 'lib/cloud_builder/dsl.rb', line 7

def globals
  @globals
end

Class Method Details

.format(key) ⇒ Object



13
14
15
# File 'lib/cloud_builder/dsl.rb', line 13

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

.hashize(value, format_hash_keys = true) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/cloud_builder/dsl.rb', line 17

def self.hashize(value, format_hash_keys = true)
  case value
    when Array then value.map { |v| jsonize v,format_hash_keys }
    when Hash then 
      ret = {}
      value.each do |k,v|
        if format_hash_keys
          ret[DSL.format(k)] = jsonize v,format_hash_keys
        else
          ret[k] = jsonize v,format_hash_keys
        end
      end
      ret
    else
      value.respond_to?(:to_json_data) ? value.to_json_data : value
  end
end

.jsonize(value, format_hash_keys = true) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/cloud_builder/dsl.rb', line 35

def self.jsonize(value, format_hash_keys = true)
  ret = self.hashize(value, format_hash_keys)
  case ret
    when Hash then  
      sh = ActiveSupport::OrderedHash.new
      ret.sort.map do |k,v|
        sh[k] =v
      end
      sh
    else
      ret
  end
end

Instance Method Details

#dirnameObject



95
96
97
# File 'lib/cloud_builder/dsl.rb', line 95

def dirname 
  @dirname
end

#include_brick(name, hash = {}, &block) ⇒ Object



61
62
63
# File 'lib/cloud_builder/dsl.rb', line 61

def include_brick(name, hash={}, &block)
  _bricks[name.to_s] = Brick.new(self, name, hash, &block)
end

#mappings(&block) ⇒ Object



49
50
51
# File 'lib/cloud_builder/dsl.rb', line 49

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

#output(name, &block) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/cloud_builder/dsl.rb', line 73

def output(name, &block)
  if _outputs.has_key?(DSL.format(name.to_s))
    raise NameError('output %s is already defined' % name.to_s)
  end
  
  _outputs[DSL.format(name.to_s)] = Output.new(self, &block)
end

#parameter(name, &block) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/cloud_builder/dsl.rb', line 65

def parameter(name, &block)
  if _parameters.has_key?(DSL.format(name.to_s))
    raise NameError('parameter %s is already defined' % name.to_s)
  end
  
  _parameters[DSL.format(name.to_s)] = Parameter.new(self, &block)
end

#reference_type(name) ⇒ Object



85
86
87
88
89
90
91
92
93
# File 'lib/cloud_builder/dsl.rb', line 85

def reference_type(name)
  if _parameters.has_key?(name)
    :parameter
  elsif _mappings.has_key?(name)
    :map
  elsif _resources.has_key?(name)
    :resource
  end
end

#resource(name, &block) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/cloud_builder/dsl.rb', line 53

def resource(name, &block)
  if _resources.has_key?(name.to_s)
    raise NameError('resource %s is already defined' % name.to_s)
  end
  
  _resources[name.to_s] = Resource.new(self, name, &block)
end

#to_jsonObject



81
82
83
# File 'lib/cloud_builder/dsl.rb', line 81

def to_json
  JSON.pretty_generate(DSL.jsonize(generate_spec, false))
end