Class: PageEz::Options

Inherits:
Object
  • Object
show all
Defined in:
lib/page_ez/options.rb

Class Method Summary collapse

Class Method Details

.merge(options, dynamic_options = nil, *args) ⇒ Object



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
# File 'lib/page_ez/options.rb', line 5

def self.merge(options, dynamic_options = nil, *args)
  dynamic_options ||= -> { {} }

  keys_to_extract = dynamic_options.parameters.filter_map do |type, name|
    if type == :keyreq || type == :key
      name
    end
  end

  if args.last.is_a?(Hash)
    if dynamic_options.arity == 0
      options.merge(*args)
    else
      kwargs = args.pop

      if keys_to_extract.empty?
        options.merge(dynamic_options.call(*args, **kwargs))
      else
        sliced = kwargs.slice(*keys_to_extract)
        except = kwargs.except(*keys_to_extract)
        options.merge(
          dynamic_options.call(*args, **sliced)
        ).merge(
          except
        )
      end
    end
  else
    options.merge(dynamic_options.call(*args))
  end
end