Module: EcoRake::Base::MethodHelpers
- Included in:
- SymbolResolver, Options::Forwarding::Rule
- Defined in:
- lib/eco-rake/base/method_helpers.rb
Instance Method Summary collapse
- #aux_curry(meth, on_missing = :curry) ⇒ Lambda, ...
-
#safe_call(meth, *args, **kargs, &block) ⇒ Object
Calls
method
with just the parameters it requires.
Instance Method Details
#aux_curry(meth, on_missing = :curry) ⇒ Lambda, ...
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/eco-rake/base/method_helpers.rb', line 21 def aux_curry(meth, on_missing = :curry) # rubocop:disable Metrics/AbcSize lambda do |*args, **kargs, &block| params = meth.parameters kparams = params.select { |type, _| (type == :keyreq) || (type == :key) } aparams = params.select { |type, _| (type == :req) || (type == :opt) } kreq_miss = kparams.select { |type, _| type == :keyreq }.map(&:last) - kargs.keys req_miss = aparams.select { |type, _| type == :req }.count - args.count req_miss = [req_miss, 0].max ready = kreq_miss.empty? && req_miss.zero? if on_missing == :safe argrest = params.find { |type, _| type == :rest } keyrest = params.find { |type, _| type == :keyrest } kextras = kargs.keys - kparams.map(&:last) kextras.each { |name| kargs.delete(name) } unless keyrest args = args[0..(aparams.count-1)] unless argrest || args.count <= aparams.count unless ready kreq_miss.each { |name| kargs[name] = nil } ary_nil = Array.new(req_miss, nil) args = args.dup.push(*ary_nil) ready = true end end return meth.call(*args, **kargs, &block) if ready || on_missing == :call return if on_missing == :return # (default) on_missing == :curry lambda do |*oth, **koth, &blk| curried = aux_curry(meth, on_missing) curried[*args, *oth, **kargs, **koth, &(blk || block)] end end end |
#safe_call(meth, *args, **kargs, &block) ⇒ Object
Note:
missing arguments required arguments will be passed as nil
value.
Calls method
with just the parameters it requires
7 8 9 10 11 12 |
# File 'lib/eco-rake/base/method_helpers.rb', line 7 def safe_call(meth, *args, **kargs, &block) msg = "Expecting Method or Proc. Given: #{meth.class}" raise ArgumentError, msg unless [Method, Proc].any? {|c| meth.is_a?(c)} aux_curry(meth, :safe).call(*args, **kargs, &block) end |