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.
11 12 13 14 |
# File 'lib/humidifier/resource.rb', line 11 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
19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/humidifier/resource.rb', line 19 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.
76 77 78 |
# File 'lib/humidifier/resource.rb', line 76 def aws_name @aws_name end |
.props ⇒ Object
Returns the value of attribute props.
76 77 78 |
# File 'lib/humidifier/resource.rb', line 76 def props @props end |
Class Method Details
.build_property_reader(name) ⇒ Object
79 80 81 82 83 |
# File 'lib/humidifier/resource.rb', line 79 def build_property_reader(name) define_method(name) do properties[name.to_s] end end |
.build_property_writer(name) ⇒ Object
86 87 88 89 90 |
# File 'lib/humidifier/resource.rb', line 86 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
93 94 95 |
# File 'lib/humidifier/resource.rb', line 93 def prop?(prop) props.key?(prop) end |
Instance Method Details
#respond_to_missing?(name) ⇒ Boolean
Patches respond_to_missing? to include property accessors
32 33 34 |
# File 'lib/humidifier/resource.rb', line 32 def respond_to_missing?(name, *) valid_accessor?(name) || super end |
#to_cf ⇒ Object
CFN stack syntax
37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/humidifier/resource.rb', line 37 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
50 51 52 53 54 |
# File 'lib/humidifier/resource.rb', line 50 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
57 58 59 60 61 62 63 64 65 |
# File 'lib/humidifier/resource.rb', line 57 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
68 69 70 71 72 73 |
# File 'lib/humidifier/resource.rb', line 68 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 |