Class: Humidifier::Resource
- Inherits:
-
Object
- Object
- Humidifier::Resource
- Defined in:
- lib/humidifier/resource.rb
Overview
Superclass for all AWS resources
Constant Summary collapse
- COMMON_ATTRIBUTES =
Attributes that are available to every stack
Utils.underscored(%w[Condition CreationPolicy DeletionPolicy DependsOn Metadata UpdatePolicy])
Class Attribute Summary collapse
-
.aws_name ⇒ Object
Returns the value of attribute aws_name.
-
.props ⇒ Object
Returns the value of attribute props.
Class Method Summary collapse
- .build_property_reader(name) ⇒ Object
- .build_property_writer(name) ⇒ Object
-
.prop?(prop) ⇒ Boolean
true if this resource has the given property.
Instance Method Summary collapse
-
#initialize(properties = {}, raw = false) ⇒ Resource
constructor
A new instance of Resource.
-
#method_missing(name, *args) ⇒ Object
Patches method_missing to include property accessors After the first method call, builds the accessor methods to get a speed boost.
-
#respond_to_missing?(name) ⇒ Boolean
Patches respond_to_missing? to include property accessors.
-
#to_cf ⇒ Object
CFN stack syntax.
-
#update(properties, raw = false) ⇒ Object
Update a set of properties defined by the given properties hash.
-
#update_attributes(attributes) ⇒ Object
Update the attributes of the resource defined by COMMON_ATTRIBUTES.
-
#update_property(property, value, raw = false) ⇒ Object
Update an individual property on this resource.
Constructor Details
#initialize(properties = {}, raw = false) ⇒ Resource
Returns a new instance of Resource.
13 14 15 16 |
# File 'lib/humidifier/resource.rb', line 13 def initialize(properties = {}, raw = false) self.properties = {} update(properties, raw) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object
Patches method_missing to include property accessors After the first method call, builds the accessor methods to get a speed boost
21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/humidifier/resource.rb', line 21 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 |
Class Attribute Details
.aws_name ⇒ Object
Returns the value of attribute aws_name.
78 79 80 |
# File 'lib/humidifier/resource.rb', line 78 def aws_name @aws_name end |
.props ⇒ Object
Returns the value of attribute props.
78 79 80 |
# File 'lib/humidifier/resource.rb', line 78 def props @props end |
Class Method Details
.build_property_reader(name) ⇒ Object
81 82 83 84 85 |
# File 'lib/humidifier/resource.rb', line 81 def build_property_reader(name) define_method(name) do properties[name.to_s] end end |
.build_property_writer(name) ⇒ Object
88 89 90 91 92 |
# File 'lib/humidifier/resource.rb', line 88 def build_property_writer(name) define_method(name) do |value| update_property(name.to_s[0..-2], value) end end |
.prop?(prop) ⇒ Boolean
true if this resource has the given property
95 96 97 |
# File 'lib/humidifier/resource.rb', line 95 def prop?(prop) props.key?(prop) end |
Instance Method Details
#respond_to_missing?(name) ⇒ Boolean
Patches respond_to_missing? to include property accessors
34 35 36 |
# File 'lib/humidifier/resource.rb', line 34 def respond_to_missing?(name, *) valid_accessor?(name) || super end |
#to_cf ⇒ Object
CFN stack syntax
39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/humidifier/resource.rb', line 39 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, raw = false) ⇒ Object
Update a set of properties defined by the given properties hash
52 53 54 55 56 |
# File 'lib/humidifier/resource.rb', line 52 def update(properties, raw = false) properties.each do |property, value| update_property(property, value, raw) end end |
#update_attributes(attributes) ⇒ Object
Update the attributes of the resource defined by COMMON_ATTRIBUTES
59 60 61 62 63 64 65 66 67 |
# File 'lib/humidifier/resource.rb', line 59 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, raw = false) ⇒ Object
Update an individual property on this resource
70 71 72 73 74 75 |
# File 'lib/humidifier/resource.rb', line 70 def update_property(property, value, raw = false) property = property.to_s property = validate_property(property, raw) value = validate_value(property, value, raw) properties[property] = value end |