Class: Humidifier::Config::Mapper

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

Overview

The parent class for mapper classes. These classes are used to transform arbitrary attributes coming from the user-provided YAML files into valid CloudFormation props that can then be used in the template. This class provides an easy-to-extend DSL that allows for default attributes specifying custom attributes.

Defined Under Namespace

Classes: InvalidResourceAttributeError

Constant Summary collapse

COMMON_ATTRIBUTES =

The list of attributes that are common to all resources that need to be handled separately.

Resource::COMMON_ATTRIBUTES.values

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.attribute(name, &block) ⇒ Object

Defines a custom attribute. The given block will receive the user-provided value for the attribute. The block should return a hash where the keys are valid humidifier properties and the values are valid values for those properties. In the below example, we specify the group attribute which maps to the groups attribute after some transformation.

attribute :group do |group|
  groups = GROUPS[group]
  groups.any? ? { groups: GROUPS[group] } : {}
end


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

def attribute(name, &block)
  define_method(:"attribute_#{name}", &block)
  attribute_methods << name
end

.attribute_methodsObject

The names of the custom attribute methods.



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

def attribute_methods
  @attribute_methods ||= []
end

.defaults(&block) ⇒ Object

Defines the default attributes that should be applied to all resources of this type. The given block will be passed the logical resource name that the user specified for the resource. The block should return a hash where the keys are valid humidifier properties and the values are valid values for those properties. In the example below, the user_name property is set based on the logical name.

defaults do |name|
  { user_name: name }
end


55
56
57
# File 'lib/humidifier/config/mapper.rb', line 55

def defaults(&block)
  define_method(:attribute_defaults, &block)
end

Instance Method Details

#resource_for(clazz, name, attributes) ⇒ Object

Builds a humidifier resource using the given humidifier resource class, the logical name for the resource, and the user-specified attributes.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/humidifier/config/mapper.rb', line 62

def resource_for(clazz, name, attributes)
  mapped =
    respond_to?(:attribute_defaults) ? attribute_defaults(name) : {}

  attributes.each do |key, value|
    mapped.merge!(mapped_from(clazz, key, value))
  end

  common_attributes = common_attributes_from(mapped)

  resource = clazz.new(mapped)
  resource.update_attributes(common_attributes)
  resource
end