# 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
