lib/parametron.rb in parametron-0.3.0 vs lib/parametron.rb in parametron-0.3.4
- old
+ new
@@ -23,11 +23,13 @@
def method_added(name)
return if name != @_method_name or instance_variable_get(:"@_METHOD_#{name}_WRAPPED")
instance_variable_set(:"@_METHOD_#{name}_WRAPPED", true)
original = instance_method(name.to_sym)
+
remove_method(name.to_sym)
+
define_method(name) do |params={}|
new_params = _rename_params!(_cast!(_validate!(_set_defaults!(params))))
original.bind(self).call(new_params)
end
end
@@ -43,10 +45,16 @@
next if val.nil? and not v.required?
new_par[key] =
case
when v.cast.to_s == "Integer" then Integer(val)
when v.cast.to_s == "Float" then Float(val)
- when Proc === v.cast then v.cast.call(val)
+ when Proc === v.cast
+ case v.cast.arity
+ when 0 then v.cast.call()
+ when 1 then v.cast.call(val)
+ when 2 then v.cast.call(val, new_par)
+ else v.cast.call(val, new_par, params)
+ end
else
raise MalformedParams.new("Unknown cast type: '#{v.cast.inspect}'")
end
end
new_par