module CloudFormer::HasPropertiesAndAttributes::ClassMethods

Public Instance Methods

aws_attribute(name, options={}) click to toggle source
# File lib/cloud_former/has_properties_and_attributes.rb, line 24
def aws_attribute(name, options={})
  @aws_attributes ||= []
  @aws_attributes << PropertyOrAttribute.new(name, options)
  make_aws_accessor(name, options)
end
aws_attribute_wrapper(name) click to toggle source
# File lib/cloud_former/has_properties_and_attributes.rb, line 30
def aws_attribute_wrapper(name)
  @aws_attribute_wrapper = name
end
aws_attributes() click to toggle source
# 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() click to toggle source
# 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={}) click to toggle source
# File lib/cloud_former/has_properties_and_attributes.rb, line 18
def aws_property(name, options={})
  @aws_properties ||= []
  @aws_properties << PropertyOrAttribute.new(name, options)
  make_aws_accessor(name, options)
end
check_type_of_aws_value(name, val, options) click to toggle source
# File lib/cloud_former/has_properties_and_attributes.rb, line 34
def check_type_of_aws_value(name, val, options)
  if !val.is_a?(options[:type])
    failed = true
    if options[: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 options[:type] == Boolean && val == true || val == false
      failed = false
    end

    if failed
      raise ArgumentError.new(
        "A #{options[:type].name} is required for #{name} in #{self.name}, but a #{val.class.name} was given."
      )
    end
  end
end
get_aws_attribute_wrapper() click to toggle source
# 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) click to toggle source
# File lib/cloud_former/has_properties_and_attributes.rb, line 63
def make_aws_accessor(name, options)
  define_method(name) do |*args, &block|
    if block
      if options[:type] < CloudFormer::ResourceProperty || options[:type] < CloudFormer::Resource
        value = if options[:list]
          count = args.shift
          if count == :list
            instances = List.new(options, *args, &block).entries
            self.nested_resources.concat(instances) if options[:type] < CloudFormer::Resource
            instances
          else
            count.times.map do |i|
              instance = options[:type].new(*args) { instance_exec(i, &block) }
              self.nested_resources << instance if options[:type] < CloudFormer::Resource
              instance
            end
          end
        else
          instance = options[:type].new(*args, &block)
          self.nested_resources << instance if instance.is_a?(CloudFormer::Resource)
          instance
        end
      else
        raise ArgumentError, "Can't construct #{options[:type]} using block syntax."
      end
      instance_variable_set("@#{name}_property_instance_var", value)
    else
      if args.size == 1
        val = args.first
        if options[:list] && !val.respond_to?(:each)
          if (!val.respond_to?(:acts_as_list?) || !val.acts_as_list?) && options[: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 options[:list]
          unless val.respond_to?(:acts_as_list?) && val.acts_as_list?
            if options[:list] == :ok
              if val.respond_to?(:each)
                val.each do |item|
                  self.class.check_type_of_aws_value(name, item, options)
                end
              else
                self.class.check_type_of_aws_value(name, val, options)
              end
            else
              val.each do |item|
                self.class.check_type_of_aws_value(name, item, options)
              end
            end
          end
        else
          self.class.check_type_of_aws_value(name, val, options)
        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