lib/parametron.rb in parametron-0.2.0 vs lib/parametron.rb in parametron-0.2.2
- old
+ new
@@ -24,11 +24,11 @@
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|
+ define_method(name) do |params={}|
new_params = _rename_params!(_validate!(_set_defaults!(params)))
original.bind(self).call(new_params)
end
end
end
@@ -47,10 +47,16 @@
new_par = params.dup
_validators_list.each do |v|
if new_par[v.name].nil? && new_par[v.name.to_sym].nil?
case v.default
when nil then next
- when Proc then new_par[v.name.to_sym] = v.default.call(self)
+ when Proc
+ case v.default.arity
+ when 0 then new_par[v.name.to_sym] = v.default.call()
+ when 1 then new_par[v.name.to_sym] = v.default.call(self)
+ else
+ raise ArgumentError.new "Too much arguments for #{v.name} default lambda"
+ end
else
new_par[v.name.to_sym] = v.default
end
end
end