~singpolyma/sgx-jmp

ref: 2f7a1b60192a14c86ef9a1f727ea9369cfedc2c6 sgx-jmp/lib/roda_capture.rb -rw-r--r-- 835 bytes
2f7a1b60Stephen Paul Weber Merge branch 'more-tel-selection-coverage' 9 months ago
                                                                                
1
2
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
# frozen-string-literal: true

# Build from official params_capturing plugin
class RodaCapture
	module RequestMethods
		def captures_hash
			@captures_hash ||= {}
		end

	private

		# Add the symbol to the list of capture names if capturing
		def _match_symbol(sym)
			@_sym_captures << sym if @_sym_captures

			super
		end

		# If all arguments are strings or symbols, turn on param capturing during
		# the matching, but turn it back off before yielding to the block.	Add
		# any captures to the params based on the param capture names added by
		# the matchers.
		def if_match(args)
			@_sym_captures = [] if args.all? { |x| x.is_a?(Symbol) }

			super do |*a|
				if @_sym_captures
					@_sym_captures.zip(a).each do |k, v|
						captures_hash[k] = v
					end
					@_sym_captures = nil
				end
				yield(*a)
			end
		end
	end
end