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 <<-BLOCK
        first *subs arg must be Array or Hash, not #{ subs[0].inspect }
      BLOCK
    end

  when 2
    # first arg needs to be an array, second a hash
    unless subs[0].is_a? Array
      raise TypeError.new NRSER.squish <<-BLOCK
        first *subs arg needs to be an array, not #{ subs[0].inspect }
      BLOCK
    end

    unless subs[1].is_a? Hash
      raise TypeError.new NRSER.squish <<-BLOCK
        second *subs arg needs to be a Hash, not #{ subs[1].inspect }
      BLOCK
    end

    args, kwds = subs
  else
    raise ArgumentError.new NRSER.squish <<-BLOCK
      must provide one or two *subs arguments, received #{ 1 + subs.length }
    BLOCK
  end

  return {
    args: args,
    kwds: kwds,
    input: input,
  }
end