Class: Serega::SeregaUtils::ParamsCount
- Inherits:
-
Object
- Object
- Serega::SeregaUtils::ParamsCount
- Defined in:
- lib/serega/utils/params_count.rb
Overview
Utility to count regular parameters of callable object
Class Method Summary collapse
-
.call(object, max_count:) ⇒ Integer
Count parameters for callable object.
Class Method Details
.call(object, max_count:) ⇒ Integer
Count parameters for callable object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/serega/utils/params_count.rb', line 23 def call(object, max_count:) # Procs (but not lambdas) can accept all provided parameters parameters = object.is_a?(Proc) ? object.parameters : object.method(:call).parameters return 1 if parameters[0] == NO_NAMED_REST_PARAM return max_count if object.is_a?(Proc) && !object.lambda? count = 0 # If all we have is no-name *rest parameters, then we assume we need to provide # 1 argument. It is now always correct, but in serialization context it's most common that # only one argument is needed. parameters.each do |parameter| next if parameter == NO_NAMED_REST_PARAM # Workaround for procs like :odd?.to_proc param_type = parameter[0] case param_type when :req then count += 1 when :opt then count += 1 if count < max_count when :rest then count += max_count - count if max_count > count end # else :opt, :keyreq, :key, :keyrest, :block - do nothing end count end |