lib/humidifier/reservoir/base_mapper.rb in humidifier-reservoir-0.1.0 vs lib/humidifier/reservoir/base_mapper.rb in humidifier-reservoir-0.2.0
- old
+ new
@@ -4,10 +4,14 @@
# arbitrary attributes coming from the user-provided YAML files into valid
# CloudFormation attributes that can then be used in the template. This
# class provides an easy-to-extend DSL that allows for default attributes
# specifying custom attributes.
class BaseMapper
+ # The list of attributes that are common to all resources that need to be
+ # handles separately.
+ COMMON_ATTRIBUTES = Resource::COMMON_ATTRIBUTES.values
+
class << self
# 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
@@ -45,19 +49,38 @@
# Builds a humidifier resource using the given humidifier resource class,
# the logical name for the resource, and the user-specified attributes.
def resource_for(clazz, name, attributes)
mapped = respond_to?(:attribute_defaults) ? attribute_defaults(name) : {}
- attributes.each { |key, value| mapped.merge!(mapped_from(clazz, key, value)) }
- clazz.new(mapped)
+
+ 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
private
- def mapped_from(clazz, key, value)
+ def common_attributes_from(mapped)
+ COMMON_ATTRIBUTES.each_with_object({}) do |common_attribute, extract|
+ extracted = mapped.delete(common_attribute)
+ extract[common_attribute] = extracted if extracted
+ end
+ end
+
+ def mapped_from(clazz, key, value) # rubocop:disable Metrics/MethodLength
if self.class.attribute_methods.include?(key.to_sym)
public_send(:"attribute_#{key}", value)
elsif clazz.prop?(key)
+ { key.to_sym => value }
+ elsif clazz.prop?("#{key}_id")
+ { :"#{key}_id" => Humidifier.ref(value) }
+ elsif COMMON_ATTRIBUTES.include?(key.to_sym)
{ key.to_sym => value }
else
raise Error, "Invalid attribute: #{key}"
end
end