class Humidifier::Resource
Superclass for all AWS resources
Constants
- COMMON_ATTRIBUTES
Attributes that are available to every stack
Attributes
aws_name[RW]
props[RW]
properties[RW]
Public Class Methods
build_property_reader(name)
click to toggle source
@private builds a cached method for a property reader
# File lib/humidifier/resource.rb, line 83 def build_property_reader(name) define_method(name) do properties[name.to_s] end end
build_property_writer(name)
click to toggle source
@private builds a cached method for a property writer
# File lib/humidifier/resource.rb, line 90 def build_property_writer(name) define_method(name) do |value| update_property(name.to_s[0..-2], value) end end
new(properties = {})
click to toggle source
# File lib/humidifier/resource.rb, line 15 def initialize(properties = {}) self.properties = {} update(properties) end
prop?(prop)
click to toggle source
true if this resource has the given property
# File lib/humidifier/resource.rb, line 97 def prop?(prop) props.key?(prop) end
Public Instance Methods
method_missing(name, *args)
click to toggle source
Patches method_missing
to include property accessors After the first method call, builds the accessor methods to get a speed boost
Calls superclass method
# File lib/humidifier/resource.rb, line 23 def method_missing(name, *args) if !valid_accessor?(name) super elsif self.class.prop?(name.to_s) self.class.build_property_reader(name) send(name) else self.class.build_property_writer(name) send(name, args.first) end end
respond_to_missing?(name, *)
click to toggle source
Patches respond_to_missing? to include property accessors
Calls superclass method
# File lib/humidifier/resource.rb, line 36 def respond_to_missing?(name, *) valid_accessor?(name) || super end
to_cf()
click to toggle source
CFN stack syntax
# File lib/humidifier/resource.rb, line 41 def to_cf props_cf = properties.map do |key, value| self.class.props[key].to_cf(value) end common_attributes.merge!( "Type" => self.class.aws_name, "Properties" => props_cf.to_h ) end
update(properties)
click to toggle source
Update a set of properties defined by the given properties hash
# File lib/humidifier/resource.rb, line 54 def update(properties) properties.each do |property, value| update_property(property, value) end end
update_attributes(attributes)
click to toggle source
Update the attributes of the resource defined by COMMON_ATTRIBUTES
# File lib/humidifier/resource.rb, line 61 def update_attributes(attributes) attributes.each do |attribute, value| unless COMMON_ATTRIBUTES.value?(attribute) raise ArgumentError, "Invalid attribute: #{attribute}" end public_send(:"#{attribute}=", value) end end
update_property(property, value)
click to toggle source
Update an individual property on this resource
# File lib/humidifier/resource.rb, line 72 def update_property(property, value) property = property.to_s property = validate_property(property) value = validate_value(property, value) properties[property] = value end
Private Instance Methods
common_attributes()
click to toggle source
# File lib/humidifier/resource.rb, line 104 def common_attributes COMMON_ATTRIBUTES.each_with_object({}) do |(name, prop), result| value = send(prop) result[name] = value if value end end
valid_accessor?(method)
click to toggle source
# File lib/humidifier/resource.rb, line 111 def valid_accessor?(method) (self.class.props.keys & [method.to_s, method.to_s[0..-2]]).any? end
validate_property(property)
click to toggle source
# File lib/humidifier/resource.rb, line 115 def validate_property(property) unless self.class.prop?(property) raise ArgumentError, "Attempting to set invalid property for " \ "#{self.class.name}: #{property}" end property end
validate_value(property, value)
click to toggle source
# File lib/humidifier/resource.rb, line 124 def validate_value(property, value) prop = self.class.props[property] unless prop.valid?(value) raise ArgumentError, "Invalid value for #{property}: #{value.inspect}" end value end