Module: CloudFormer::HasPropertiesAndAttributes::ClassMethods
- Defined in:
- lib/cloud_former/has_properties_and_attributes.rb
Instance Method Summary collapse
- #aws_attribute(name, options = {}) ⇒ Object
- #aws_attribute_wrapper(name) ⇒ Object
- #aws_attributes ⇒ Object
- #aws_properties ⇒ Object
- #aws_property(name, options = {}) ⇒ Object
- #check_type_of_aws_value(name, val, options) ⇒ Object
- #get_aws_attribute_wrapper ⇒ Object
- #make_aws_accessor(name, options) ⇒ Object
Instance Method Details
#aws_attribute(name, options = {}) ⇒ Object
24 25 26 27 28 |
# File 'lib/cloud_former/has_properties_and_attributes.rb', line 24 def aws_attribute(name, ={}) @aws_attributes ||= [] @aws_attributes << PropertyOrAttribute.new(name, ) make_aws_accessor(name, ) end |
#aws_attribute_wrapper(name) ⇒ Object
30 31 32 |
# File 'lib/cloud_former/has_properties_and_attributes.rb', line 30 def aws_attribute_wrapper(name) @aws_attribute_wrapper = name end |
#aws_attributes ⇒ Object
145 146 147 148 149 150 151 152 153 154 |
# File 'lib/cloud_former/has_properties_and_attributes.rb', line 145 def aws_attributes answer = [] if defined?(@aws_attributes) answer = @aws_attributes end if superclass && superclass.respond_to?(:aws_attributes) answer += superclass.aws_attributes end answer end |
#aws_properties ⇒ Object
134 135 136 137 138 139 140 141 142 143 |
# File 'lib/cloud_former/has_properties_and_attributes.rb', line 134 def aws_properties answer = [] if defined?(@aws_properties) answer = @aws_properties end if superclass && superclass.respond_to?(:aws_properties) answer += superclass.aws_properties end answer end |
#aws_property(name, options = {}) ⇒ Object
18 19 20 21 22 |
# File 'lib/cloud_former/has_properties_and_attributes.rb', line 18 def aws_property(name, ={}) @aws_properties ||= [] @aws_properties << PropertyOrAttribute.new(name, ) make_aws_accessor(name, ) end |
#check_type_of_aws_value(name, val, options) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/cloud_former/has_properties_and_attributes.rb', line 34 def check_type_of_aws_value(name, val, ) if !val.is_a?([:type]) failed = true if [:type] == String if val.is_a?(Fixnum) failed = false elsif val.is_a?(PseudoParameter) failed = false elsif val.respond_to?(:acts_as_string?) && val.acts_as_string? failed = false end end if val.is_a?(Parameter) || val.is_a?(Resource) || val.is_a?(Function) failed = false end if [:type] == Boolean && val == true || val == false failed = false end if failed raise ArgumentError.new( "A #{[:type].name} is required for #{name} in #{self.name}, but a #{val.class.name} was given." ) end end end |
#get_aws_attribute_wrapper ⇒ Object
156 157 158 159 160 161 162 |
# File 'lib/cloud_former/has_properties_and_attributes.rb', line 156 def get_aws_attribute_wrapper if defined?(@aws_attribute_wrapper) @aws_attribute_wrapper else nil end end |
#make_aws_accessor(name, options) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/cloud_former/has_properties_and_attributes.rb', line 63 def make_aws_accessor(name, ) define_method(name) do |*args, &block| if block if [:type] < CloudFormer::ResourceProperty || [:type] < CloudFormer::Resource value = if [:list] count = args.shift if count == :list instances = List.new(, *args, &block).entries self.nested_resources.concat(instances) if [:type] < CloudFormer::Resource instances else count.times.map do |i| instance = [:type].new(*args) { instance_exec(i, &block) } self.nested_resources << instance if [:type] < CloudFormer::Resource instance end end else instance = [:type].new(*args, &block) self.nested_resources << instance if instance.is_a?(CloudFormer::Resource) instance end else raise ArgumentError, "Can't construct #{[:type]} using block syntax." end instance_variable_set("@#{name}_property_instance_var", value) else if args.size == 1 val = args.first if [:list] && !val.respond_to?(:each) if (!val.respond_to?(:acts_as_list?) || !val.acts_as_list?) && [:list] != :ok raise ArgumentError.new( "A list is required for #{name} in #{self.class.name}, but a #{val.class.name} was given." ) end end if [:list] unless val.respond_to?(:acts_as_list?) && val.acts_as_list? if [:list] == :ok if val.respond_to?(:each) val.each do |item| self.class.check_type_of_aws_value(name, item, ) end else self.class.check_type_of_aws_value(name, val, ) end else val.each do |item| self.class.check_type_of_aws_value(name, item, ) end end end else self.class.check_type_of_aws_value(name, val, ) end instance_variable_set("@#{name}_property_instance_var", val) elsif args.size > 1 raise ArgumentError, "wrong number of arguments (#{args.size} for 0..1)" end end if instance_variable_defined?("@#{name}_property_instance_var") instance_variable_get("@#{name}_property_instance_var") else nil end end end |