Closed backus closed 9 years ago
As an intermediary fix before config is rewritten with morpher, we could rewrite the Devtools::Config.attribute
to read
Undefined = Class.new(Object)
# Declare an attribute
#
# @param [Symbol] name
#
# @yieldreturn [Object]
# the default value to use
#
# @api private
#
# @return [self]
#
def self.attribute(name, *types, default: Undefined.new)
define_method(name) do
assert_attribute_type(raw.fetch(name.to_s, default), types)
end
end
private_class_method :attribute
# Assert value is an expected type
#
# @param value [Object] loaded attribute value
# @param types [Array<Class>] allowed types
#
# @raise [TypeError] if invalid type
# @return value otherwise
#
# @api private
def self.assert_attribute_type(value, types)
fail TypeError, value unless types.any?(&value.method(:instance_of?))
value
end
private_class_method :assert_attribute_type
in which case an attribute definition would read like so
attribute :zombify, [TrueClass, FalseClass], default: false
@backus This was fixed with #104
91 refactored the flay implementation and killed some mutations along the way. An unintended side effect was that configuration that specifies
total_score
as a float like soas opposed to an integer
now produces
To avoid issues like this, we should assert expected types on configuration load.