lib/compel/contract.rb in compel-0.1.1 vs lib/compel/contract.rb in compel-0.1.2
- old
+ new
@@ -2,11 +2,10 @@
class Contract
attr_reader :errors,
:conditions,
- :coerced_params,
:serialized_errors
def initialize(params, &block)
if params.nil? || !Coercion.valid?(params, Hash)
raise ParamTypeError, 'params must be an Hash'
@@ -42,11 +41,11 @@
# All values must coerce before going through validation,
# raise exception to avoid validation
# If the param value has already been coerced from digging into child Hash
- # use that value instead, so we don't loose the previous coerced values
+ # use that value instead, so we don't lose the previous coerced values
coerced_value = Coercion.coerce! \
(@coerced_params[param.name].nil? ? param.value : @coerced_params[param.name]), param.type, param.options
# Only add to coerced values if not nil
@@ -65,13 +64,18 @@
self
end
def param(name, type, options = {}, &block)
@conditions[name] = \
- Param.new(name, type, @params[name], options, &block)
+ Param.new(name, type, @params.delete(name), options, &block)
end
+ def coerced_params
+ # @params has all params that are not affected by the validation
+ @params.merge(@coerced_params)
+ end
+
def serialize
coerced_params.tap do |hash|
if !valid?
hash[:errors] = serialized_errors
end
@@ -92,9 +96,11 @@
exception.params = coerced_params
exception.errors = serialized_errors
raise exception, 'params are invalid'
end
+
+ coerced_params
end
end
end