Module: Roda::RodaPlugins::Flow::RequestMethods

Defined in:
lib/roda/plugins/flow.rb

Instance Method Summary collapse

Instance Method Details

#if_match(args, &block) ⇒ Object



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
# File 'lib/roda/plugins/flow.rb', line 9

def if_match(args, &block)
  path = @remaining_path
  # For every block, we make sure to reset captures so that
  # nesting matchers won't mess with each other's captures.
  @captures.clear

  kwargs = {}

  if args.last === Base::RequestMethods::TERM && args.first.is_a?(::Hash)
    kwargs = args.shift
  end

  kwargs = args.pop if args.last.is_a?(::Hash)

  return unless match_all(args)

  container_keys = Array(kwargs.fetch(:resolve, []))

  resolutions = container_keys.map do |resolution|
    if resolution.respond_to?(:call)
      resoltion.call(captures)
    else
      roda_class.resolve(resolution)
    end
  end

  block = kwargs.fetch(:to, block)
  injections = kwargs.fetch(:inject, [])
  method_injections = kwargs.fetch(:call_with, [])

  unless block.nil? || block.respond_to?(:call)
    block, method = block.to_s.split('#')
    if method
      block = roda_class.resolve(block).call(*injections).method(method)
    else
      block = roda_class.resolve(block).call(*injections)
    end
  end

  block_result(block.call(*(captures + resolutions + method_injections)))
  throw :halt, response.finish
ensure
  @remaining_path = path
end