Method: Wx.args_as_list

Defined in:
lib/wx/helpers.rb

.args_as_list(param_spec, *mixed_args) ⇒ Object

Convert mixed positional / named args into a list to be passed to an underlying API method. param_spec is an Array of Parameter structs containing the keyword name and default value for each possible argument. mixed_args is an array which may optionally end with a set of named arguments



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
# File 'lib/wx/helpers.rb', line 11

def self.args_as_list(param_spec, *mixed_args)

  begin
    # get keyword arguments from mixed args if supplied, else empty
    kwa = mixed_args.last.kind_of?(Hash) ? mixed_args.pop : {}
    out_args = []
    param_spec.each_with_index do | param, i |
      if arg = mixed_args[i] # use the supplied list arg 
        out_args << arg
      elsif kwa.key?(param.name) # use the keyword arg
        out_args << kwa.delete(param.name)
      else # use the default argument
        out_args << param.default
      end
    end
  rescue
    Kernel.raise ArgumentError, 
               "Bad arg composition of #{mixed_args.inspect}"
  end

  unless kwa.empty?
    Kernel.raise ArgumentError, 
               "Unknown keyword argument(s) : #{kwa.keys.inspect}"
  end

  out_args
end