Class: ActionHandler::ArgsMaker

Inherits:
Object
  • Object
show all
Defined in:
lib/action_handler/args_maker.rb

Instance Method Summary collapse

Instance Method Details

#make_args(method, supplier, context: nil) ⇒ Object



11
12
13
14
15
16
17
18
19
20
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
# File 'lib/action_handler/args_maker.rb', line 11

def make_args(method, supplier, context: nil)
  supplier_args = [context].compact

  values = []
  keywords = {}

  method.parameters.each do |kind, name|
    unless supplier.respond_to?(name)
      raise ActionHandler::ActionArgumentError.new(
        method,
        "parameter #{name} is not defined in #{supplier}",
      )
    end

    case kind
    when :req
      values << supplier.send(name, *supplier_args)
    when :keyreq
      keywords[name] = supplier.send(name, *supplier_args)
    when :opt, :key
      raise ActionHandler::ActionArgumentError.new(method, <<~ERR)
        Do not use optional arguments.
        ActionHandler always injects arguments even if the value is nil,
        so the optional values never be used.
      ERR
    when :rest, :keyrest
      raise ActionHandler::ActionArgumentError.new(
        method,
        'rest arguments cannot be used',
      )
    end
  end

  values << keywords unless keywords.empty?
  values
end