1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/actionpack/lib/action_dispatch/journey/route.rb

125 lines
3 KiB
Ruby
Raw Normal View History

module ActionDispatch
module Journey # :nodoc:
class Route # :nodoc:
attr_reader :app, :path, :defaults, :name
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.
def initialize(name, app, path, constraints, required_defaults, defaults)
@name = name
@app = app
@path = path
@constraints = constraints
@defaults = defaults
@required_defaults = nil
@_required_defaults = required_defaults
@required_parts = nil
@parts = nil
@decorated_ast = nil
@precedence = 0
2014-05-21 14:56:12 -04:00
@path_formatter = @path.build_formatter
end
def ast
@decorated_ast ||= begin
2013-01-06 07:52:36 -05:00
decorated_ast = path.ast
decorated_ast.find_all(&:terminal?).each { |n| n.memo = self }
2013-01-06 07:52:36 -05:00
decorated_ast
end
end
def requirements # :nodoc:
# needed for rails `rake routes`
@defaults.merge(path.requirements).delete_if { |_,v|
/.+?/ == v
}
end
def segments
path.names
end
def required_keys
required_parts + required_defaults.keys
end
def score(constraints)
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
@parts ||= segments.map(&:to_sym)
end
alias :segment_keys :parts
def format(path_options)
2014-05-21 14:56:12 -04:00
@path_formatter.evaluate path_options
end
def required_parts
@required_parts ||= path.required_names.map(&:to_sym)
end
def required_default?(key)
@_required_defaults.include?(key)
end
def required_defaults
@required_defaults ||= @defaults.dup.delete_if do |k,_|
parts.include?(k) || !required_default?(k)
end
end
def glob?
!path.spec.grep(Nodes::Star).empty?
end
def dispatcher?
@app.dispatcher?
end
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))
when TrueClass
request.send(method).present?
when FalseClass
request.send(method).blank?
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
def verb
constraints[:request_method] || //
end
end
end
end