2012-12-19 15:54:47 -05:00
|
|
|
module ActionDispatch
|
2012-12-19 19:24:25 -05:00
|
|
|
module Journey # :nodoc:
|
|
|
|
class Route # :nodoc:
|
2013-01-15 08:38:10 -05:00
|
|
|
attr_reader :app, :path, :defaults, :name
|
2012-12-19 15:54:47 -05:00
|
|
|
|
|
|
|
attr_reader :constraints
|
|
|
|
alias :conditions :constraints
|
|
|
|
|
|
|
|
attr_accessor :precedence
|
|
|
|
|
|
|
|
##
|
|
|
|
# +path+ is a path constraint.
|
|
|
|
# +constraints+ is a hash of constraints to be applied to this route.
|
2015-06-08 20:18:06 -04:00
|
|
|
def initialize(name, app, path, constraints, required_defaults, defaults)
|
2012-12-19 15:54:47 -05:00
|
|
|
@name = name
|
|
|
|
@app = app
|
|
|
|
@path = path
|
|
|
|
|
|
|
|
@constraints = constraints
|
|
|
|
@defaults = defaults
|
|
|
|
@required_defaults = nil
|
2015-08-17 16:59:17 -04:00
|
|
|
@_required_defaults = required_defaults
|
2012-12-19 15:54:47 -05:00
|
|
|
@required_parts = nil
|
|
|
|
@parts = nil
|
|
|
|
@decorated_ast = nil
|
|
|
|
@precedence = 0
|
2014-05-21 14:56:12 -04:00
|
|
|
@path_formatter = @path.build_formatter
|
2012-12-19 15:54:47 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def ast
|
2013-01-06 07:38:00 -05:00
|
|
|
@decorated_ast ||= begin
|
2013-01-06 07:52:36 -05:00
|
|
|
decorated_ast = path.ast
|
2015-08-17 16:36:09 -04:00
|
|
|
decorated_ast.find_all(&:terminal?).each { |n| n.memo = self }
|
2013-01-06 07:52:36 -05:00
|
|
|
decorated_ast
|
2013-01-06 07:38:00 -05:00
|
|
|
end
|
2012-12-19 15:54:47 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def requirements # :nodoc:
|
|
|
|
# needed for rails `rake routes`
|
2015-01-07 18:53:57 -05:00
|
|
|
@defaults.merge(path.requirements).delete_if { |_,v|
|
2012-12-19 15:54:47 -05:00
|
|
|
/.+?/ == v
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def segments
|
2013-01-07 08:09:22 -05:00
|
|
|
path.names
|
2012-12-19 15:54:47 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def required_keys
|
2013-01-15 12:18:59 -05:00
|
|
|
required_parts + required_defaults.keys
|
2012-12-19 15:54:47 -05:00
|
|
|
end
|
|
|
|
|
2012-12-20 15:42:39 -05:00
|
|
|
def score(constraints)
|
2012-12-19 15:54:47 -05:00
|
|
|
required_keys = path.required_names
|
|
|
|
supplied_keys = constraints.map { |k,v| v && k.to_s }.compact
|
|
|
|
|
|
|
|
return -1 unless (required_keys - supplied_keys).empty?
|
|
|
|
|
|
|
|
score = (supplied_keys & path.names).length
|
|
|
|
score + (required_defaults.length * 2)
|
|
|
|
end
|
|
|
|
|
|
|
|
def parts
|
2014-10-27 12:28:53 -04:00
|
|
|
@parts ||= segments.map(&:to_sym)
|
2012-12-19 15:54:47 -05:00
|
|
|
end
|
|
|
|
alias :segment_keys :parts
|
|
|
|
|
2012-12-20 15:42:39 -05:00
|
|
|
def format(path_options)
|
2014-05-21 14:56:12 -04:00
|
|
|
@path_formatter.evaluate path_options
|
2013-03-03 14:18:01 -05:00
|
|
|
end
|
|
|
|
|
2012-12-19 15:54:47 -05:00
|
|
|
def required_parts
|
2014-10-27 12:28:53 -04:00
|
|
|
@required_parts ||= path.required_names.map(&:to_sym)
|
2012-12-19 15:54:47 -05:00
|
|
|
end
|
|
|
|
|
2013-01-15 12:18:59 -05:00
|
|
|
def required_default?(key)
|
2015-06-08 20:18:06 -04:00
|
|
|
@_required_defaults.include?(key)
|
2013-01-15 12:18:59 -05:00
|
|
|
end
|
|
|
|
|
2012-12-19 15:54:47 -05:00
|
|
|
def required_defaults
|
2013-01-15 12:18:59 -05:00
|
|
|
@required_defaults ||= @defaults.dup.delete_if do |k,_|
|
|
|
|
parts.include?(k) || !required_default?(k)
|
2012-12-19 15:54:47 -05:00
|
|
|
end
|
|
|
|
end
|
2013-01-15 08:38:10 -05:00
|
|
|
|
2014-04-20 05:08:32 -04:00
|
|
|
def glob?
|
|
|
|
!path.spec.grep(Nodes::Star).empty?
|
|
|
|
end
|
|
|
|
|
2013-07-16 08:27:22 -04:00
|
|
|
def dispatcher?
|
2014-05-24 22:03:12 -04:00
|
|
|
@app.dispatcher?
|
2013-07-16 08:27:22 -04:00
|
|
|
end
|
|
|
|
|
2013-01-15 08:38:10 -05:00
|
|
|
def matches?(request)
|
|
|
|
constraints.all? do |method, value|
|
|
|
|
case value
|
|
|
|
when Regexp, String
|
|
|
|
value === request.send(method).to_s
|
|
|
|
when Array
|
|
|
|
value.include?(request.send(method))
|
2013-04-11 22:12:19 -04:00
|
|
|
when TrueClass
|
|
|
|
request.send(method).present?
|
|
|
|
when FalseClass
|
|
|
|
request.send(method).blank?
|
2013-01-15 08:38:10 -05:00
|
|
|
else
|
|
|
|
value === request.send(method)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def ip
|
|
|
|
constraints[:ip] || //
|
|
|
|
end
|
|
|
|
|
2015-08-14 13:09:09 -04:00
|
|
|
def requires_matching_verb?
|
|
|
|
constraints[:request_method]
|
|
|
|
end
|
|
|
|
|
2013-01-15 08:38:10 -05:00
|
|
|
def verb
|
|
|
|
constraints[:request_method] || //
|
|
|
|
end
|
2012-12-19 15:54:47 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|