Class: Humidifier::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/humidifier/resource.rb

Overview

Superclass for all AWS resources

Constant Summary collapse

COMMON_ATTRIBUTES =

Attributes that are available to every stack

Humidifier.underscore(
  %w[Condition CreationPolicy DeletionPolicy DependsOn Metadata
     UpdatePolicy]
)

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(properties = {}) ⇒ Resource

Returns a new instance of Resource.


15
16
17
18
# File 'lib/humidifier/resource.rb', line 15

def initialize(properties = {})
  self.properties = {}
  update(properties)
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


23
24
25
26
27
28
29
30
31
32
33
# 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

Class Attribute Details

.aws_nameObject

Returns the value of attribute aws_name


80
81
82
# File 'lib/humidifier/resource.rb', line 80

def aws_name
  @aws_name
end

.propsObject

Returns the value of attribute props


80
81
82
# File 'lib/humidifier/resource.rb', line 80

def props
  @props
end

Class Method Details

.build_property_reader(name) ⇒ Object


83
84
85
86
87
# 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) ⇒ Object


90
91
92
93
94
# 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

.prop?(prop) ⇒ Boolean

true if this resource has the given property

Returns:

  • (Boolean)

97
98
99
# File 'lib/humidifier/resource.rb', line 97

def prop?(prop)
  props.key?(prop)
end

Instance Method Details

#respond_to_missing?(name) ⇒ Boolean

Patches respond_to_missing? to include property accessors

Returns:

  • (Boolean)

36
37
38
# File 'lib/humidifier/resource.rb', line 36

def respond_to_missing?(name, *)
  valid_accessor?(name) || super
end

#to_cfObject

CFN stack syntax


41
42
43
44
45
46
47
48
49
50
51
# 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) ⇒ Object

Update a set of properties defined by the given properties hash


54
55
56
57
58
# File 'lib/humidifier/resource.rb', line 54

def update(properties)
  properties.each do |property, value|
    update_property(property, value)
  end
end

#update_attributes(attributes) ⇒ Object

Update the attributes of the resource defined by COMMON_ATTRIBUTES


61
62
63
64
65
66
67
68
69
# 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) ⇒ Object

Update an individual property on this resource


72
73
74
75
76
77
# 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