Class: Cmds::Params

Inherits:
Object
  • Object
show all
Defined in:
lib/cmds/util/params.rb

Class Method Summary collapse

Class Method Details

.normalize(*params, &input) ⇒ Object

Cmds instance execution methods take a splat and block



4
5
6
7
8
9
10
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
47
48
49
50
51
52
53
54
# File 'lib/cmds/util/params.rb', line 4

def self.normalize *params, &input
  args = []
  kwds = {}
  input = input_block.nil? ? nil : input_block.call

  case subs.length
  when 0
    # nothing to do
  when 1
    # can either be a hash, which is interpreted as a keywords,
    # or an array, which is interpreted as positional arguments
    case subs[0]
    when Hash
      kwds = subs[0]

    when Array
      args = subs[0]

    else
      raise TypeError.new NRSER.squish "        first *subs arg must be Array or Hash, not \#{ subs[0].inspect }\n      BLOCK\n    end\n\n  when 2\n    # first arg needs to be an array, second a hash\n    unless subs[0].is_a? Array\n      raise TypeError.new NRSER.squish <<-BLOCK\n        first *subs arg needs to be an array, not \#{ subs[0].inspect }\n      BLOCK\n    end\n\n    unless subs[1].is_a? Hash\n      raise TypeError.new NRSER.squish <<-BLOCK\n        second *subs arg needs to be a Hash, not \#{ subs[1].inspect }\n      BLOCK\n    end\n\n    args, kwds = subs\n  else\n    raise ArgumentError.new NRSER.squish <<-BLOCK\n      must provide one or two *subs arguments, received \#{ 1 + subs.length }\n    BLOCK\n  end\n\n  return {\n    args: args,\n    kwds: kwds,\n    input: input,\n  }\nend\n"