Class: Proc

Inherits:
Object show all
Defined in:
lib/r_kit/utility/proc_extend.rb

Instance Method Summary collapse

Instance Method Details

#extract_parameters(*args, **options, &block) ⇒ Object



3
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
# File 'lib/r_kit/utility/proc_extend.rb', line 3

def extract_parameters *args, **options, &block
  before_rest = true

  parameters.reduce({}) do |params, (type, name)|
    value = case type
    when :opt
      # TODO: default_value? -> this will be set to nil, must define a warning, or raise an error
      # TODO: this is a problem with declaration like: (arg1, arg2 = nil, *args, arg4)
      # we can do smthng like:
      # first proccess 'block'
      # then process 'options'
      # then process 'args'
      # -> first process all req at the begining
      # -> then process all req at the end
      # -> then process all opt at the begining
      # -> then process rest
      # TODO: or, find thehow to access the default value
      before_rest ? args.shift : args.pop
    when :req
      before_rest ? args.shift : args.pop
    when :rest
      before_rest = false
      args
    when :key
      options.delete name # TODO: default_value? -> this will be set to nil
    when :keyreq
      options.delete name
    when :keyrest
      options
    when :block
      block
    end

    params[name] = value
    params
  end
end