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

Utils.underscored(%w[Condition CreationPolicy DeletionPolicy DependsOn Metadata UpdatePolicy])

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(properties = {}, raw = false) ⇒ Resource

Returns a new instance of Resource.



10
11
12
13
# File 'lib/humidifier/resource.rb', line 10

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



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/humidifier/resource.rb', line 17

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.



62
63
64
# File 'lib/humidifier/resource.rb', line 62

def aws_name
  @aws_name
end

.propsObject

Returns the value of attribute props.



62
63
64
# File 'lib/humidifier/resource.rb', line 62

def props
  @props
end

Class Method Details

.build_property_reader(name) ⇒ Object



65
66
67
68
69
# File 'lib/humidifier/resource.rb', line 65

def build_property_reader(name)
  define_method(name) do
    properties[name.to_s]
  end
end

.build_property_writer(name) ⇒ Object



72
73
74
75
76
# File 'lib/humidifier/resource.rb', line 72

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)


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

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)


30
31
32
# File 'lib/humidifier/resource.rb', line 30

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

#to_cfObject

CFN stack syntax



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

def to_cf
  props_cf = properties.map { |key, value| self.class.props[key].to_cf(value) }.to_h
  { 'Type' => self.class.aws_name, 'Properties' => props_cf }.merge(common_attributes)
end

#update(properties, raw = false) ⇒ Object

Update a set of properties defined by the given properties hash



41
42
43
# File 'lib/humidifier/resource.rb', line 41

def update(properties, raw = false)
  properties.each { |property, value| update_property(property, value, raw) }
end

#update_attributes(attributes) ⇒ Object

Update the attributes of the resource defined by COMMON_ATTRIBUTES



46
47
48
49
50
51
# File 'lib/humidifier/resource.rb', line 46

def update_attributes(attributes)
  attributes.each do |attribute, value|
    raise ArgumentError, "Invalid attribute: #{attribute}" unless COMMON_ATTRIBUTES.value?(attribute)
    public_send(:"#{attribute}=", value)
  end
end

#update_property(property, value, raw = false) ⇒ Object

Update an individual property on this resource



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

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