class ActiveAws::CloudFormation::Template::Resource
Constants
- METHOD_TYPES_MAP
- TYPES
Attributes
name[RW]
type[RW]
Public Class Methods
array_attr(*names)
click to toggle source
Allows us to do:
class LoadBalancer < Resource array_attr :availability_zones
Then to go:
load_balancer 'LoadBalancer' do availability_zones 'ap-southeast-1a', 'ap-southeast-1b'
Or, alternatively
load_balancer 'LoadBalancer' do availability_zone 'ap-southeast-1a' availability_zone 'ap-southeast-1b'
# File lib/active_aws/cloud_formation/template/resource.rb, line 32 def array_attr(*names) names.each {|name| define_method(name) do |*args| if properties.key?(name) properties.store(name, properties.fetch(name).concat(args)) else properties.store(name, args) end end define_method(name.to_s.singularize) do |arg| if properties.key?(name) properties.fetch(name) << arg else properties.store(name, [arg]) end end } end
build(params = {})
click to toggle source
# File lib/active_aws/cloud_formation/template/resource.rb, line 77 def build(params = {}) name = params[:name] type = params[:type] raise 'Resource name cannot be blank or empty' if name.blank? raise "Resource name '#{name}' is non alphanumeric" unless name =~ /^[[:alnum:]]+$/ raise 'Resource type cannot be black or empty' if type.blank? raise "Resource type '#{type}' unknown or not yet handled" unless TYPES.include?(type) if arr = METHOD_TYPES_MAP.select {|k, v| v.is_a? Array }.find {|k, (type_name, class_ref)| type_name == type } method_name, (type_name, class_ref) = arr # At this point, LoadBalancer.new is also private class_ref.send(:new, params) else # We can call new instead of Resource.send(:new, params) since we *are* Resource new(params) end end
new(params = {})
click to toggle source
# File lib/active_aws/cloud_formation/template/resource.rb, line 96 def initialize(params = {}) @name = params[:name] if params[:name] @type = params[:type] if params[:type] raise 'Resource name cannot be blank or empty' if @name.blank? raise "Resource name '#{@name}' is non alphanumeric" unless @name =~ /^[[:alnum:]]+$/ raise 'Resource type cannot be black or empty' if @type.blank? raise "Resource type '#{@type}' unknown or not yet handled" unless TYPES.include?(@type) props = params.except(:name, :type) unless props.empty? @properties = Properties.new @properties.merge!(props) end add_validations_based_on_type end
type(type)
click to toggle source
# File lib/active_aws/cloud_formation/template/resource.rb, line 14 def type(type) end
Public Instance Methods
camelize_keys(hash)
click to toggle source
# File lib/active_aws/cloud_formation/template/resource.rb, line 123 def camelize_keys(hash) hash.each_with_object({}) {|(k, v), h| key = k.is_a?(Symbol) ? k.to_s.camelize : k h[key] = case when v.is_a?(Hash) camelize_keys(v) when v.is_a?(Array) v.map {|e| e.is_a?(Hash) ? camelize_keys(e) : e } else v end } end
method_missing(method_name, *args, &block)
click to toggle source
Calls superclass method
# File lib/active_aws/cloud_formation/template/resource.rb, line 137 def method_missing(method_name, *args, &block) if args.size == 1 properties.store(method_name, args.first) elsif block_given? DSLBlock.eval_using(properties, block) if block_given? else super end end
properties(&block)
click to toggle source
# File lib/active_aws/cloud_formation/template/resource.rb, line 111 def properties(&block) @properties ||= Properties.new @properties.instance_eval(&block) if block_given? @properties end
to_h()
click to toggle source
# File lib/active_aws/cloud_formation/template/resource.rb, line 117 def to_h h = { 'Type' => self.type } h['Properties'] = camelize_keys(@properties) unless @properties.nil? || @properties.empty? h end
Private Instance Methods
add_validations_based_on_type()
click to toggle source
# File lib/active_aws/cloud_formation/template/resource.rb, line 207 def add_validations_based_on_type case @type when 'AWS::ElasticLoadBalancing::LoadBalancer' validate :listeners do raise "Resource '#{@name}' (AWS::ElasticLoadBalancing::LoadBalancer) Listeners cannot be empty" if listeners.empty? end end end
validate(property, &block)
click to toggle source
# File lib/active_aws/cloud_formation/template/resource.rb, line 203 def validate(property, &block) validations[:property] = block end
validations()
click to toggle source
# File lib/active_aws/cloud_formation/template/resource.rb, line 199 def validations @validations ||= Hash.new end