def initialize(app, conditions, defaults, name)
unless app.respond_to?(:call)
raise ArgumentError, 'app must be a valid rack application' \
' and respond to call'
end
@app = app
@name = name ? name.to_sym : nil
@defaults = (defaults || {}).freeze
@conditions = {}
conditions.each do |method, pattern|
next unless method && pattern
pattern = Regexp.compile("\\A#{Regexp.escape(pattern)}\\Z") if pattern.is_a?(String)
if pattern.is_a?(Regexp)
pattern = Utils.normalize_extended_expression(pattern)
pattern = RegexpWithNamedGroups.new(pattern)
pattern.extend(GeneratableRegexp::InstanceMethods)
pattern.defaults = @defaults
end
@conditions[method] = pattern.freeze
end
@named_captures = {}
@conditions.map { |method, condition|
next unless condition.respond_to?(:named_captures)
@named_captures[method] = Hash[condition.named_captures.map { |k, v|
[k.to_sym, v.last - 1]
}].freeze
}
@named_captures.freeze
@has_significant_params = @conditions.any? { |method, condition|
(condition.respond_to?(:required_params) && condition.required_params.any?) ||
(condition.respond_to?(:required_defaults) && condition.required_defaults.any?)
}
if @conditions.has_key?(:path_info) &&
!Utils.regexp_anchored?(@conditions[:path_info])
@prefix = true
@app = Prefix.new(@app)
else
@prefix = false
end
@conditions.freeze
end