lib/abstractivator/proc_ext.rb in abstractivator-0.6.0 vs lib/abstractivator/proc_ext.rb in abstractivator-0.7.0
- old
+ new
@@ -6,10 +6,16 @@
def loosen_args
proc do |*args, **kws, &block|
Proc.loose_call(self, args, kws, &block)
end
end
+
+ KEYWORD_PARAMETER_TYPES = %i(key keyreq keyrest).freeze
+
+ def accepts_keywords
+ @accepts_keywords ||= parameters.any?{|param| KEYWORD_PARAMETER_TYPES.include?(param.first)}
+ end
end
class Proc
include MethodAndProcExtensions
@@ -50,10 +56,20 @@
proc do |*args, &block|
self.call(*args.reverse, &block)
end
end
+ def proxy_call(*args, **kws, &block)
+ if accepts_keywords
+ call(*args, **kws, &block)
+ elsif kws.any?
+ call(*(args + [kws]), &block)
+ else
+ call(*args, &block)
+ end
+ end
+
# tries to coerce x into a procedure, then calls it with
# the given argument list.
# If x cannot be coerced into a procedure, returns x.
def self.loose_call(x, args, kws={}, &block)
x = x.to_proc if x.respond_to?(:to_proc)
@@ -110,7 +126,17 @@
end
class Object
def callable?
respond_to?(:call)
+ end
+
+ def proxy_send(method_name, *args, **kws, &block)
+ if method(method_name).accepts_keywords
+ send(method_name, *args, **kws, &block)
+ elsif kws.any?
+ send(method_name, *(args + [kws]), &block)
+ else
+ send(method_name, *args, &block)
+ end
end
end