Class: ActionDispatch::Journey::Route
- Inherits:
-
Object
- Object
- ActionDispatch::Journey::Route
- Defined in:
- lib/action_dispatch/journey/route.rb
Overview
:nodoc:
Instance Attribute Summary collapse
-
#app ⇒ Object
readonly
Returns the value of attribute app.
-
#constraints ⇒ Object
(also: #conditions)
readonly
Returns the value of attribute constraints.
-
#defaults ⇒ Object
readonly
Returns the value of attribute defaults.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#precedence ⇒ Object
Returns the value of attribute precedence.
Instance Method Summary collapse
- #ast ⇒ Object
- #dispatcher? ⇒ Boolean
- #format(path_options) ⇒ Object
- #glob? ⇒ Boolean
-
#initialize(name, app, path, constraints, defaults = {}) ⇒ Route
constructor
path
is a path constraint. - #ip ⇒ Object
- #matches?(request) ⇒ Boolean
- #optimized_path ⇒ Object
- #optional_parts ⇒ Object
- #parts ⇒ Object (also: #segment_keys)
- #required_default?(key) ⇒ Boolean
- #required_defaults ⇒ Object
- #required_keys ⇒ Object
- #required_parts ⇒ Object
-
#requirements ⇒ Object
:nodoc:.
- #score(constraints) ⇒ Object
- #segments ⇒ Object
- #verb ⇒ Object
Constructor Details
#initialize(name, app, path, constraints, defaults = {}) ⇒ Route
path
is a path constraint. constraints
is a hash of constraints to be applied to this route.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/action_dispatch/journey/route.rb', line 14 def initialize(name, app, path, constraints, defaults = {}) @name = name @app = app @path = path # Unwrap any constraints so we can see what's inside for route generation. # This allows the formatter to skip over any mounted applications or redirects # that shouldn't be matched when using a url_for without a route name. while app.is_a?(Routing::Mapper::Constraints) do app = app.app end @dispatcher = app.is_a?(Routing::RouteSet::Dispatcher) @constraints = constraints @defaults = defaults @required_defaults = nil @required_parts = nil @parts = nil @decorated_ast = nil @precedence = 0 end |
Instance Attribute Details
#app ⇒ Object (readonly)
Returns the value of attribute app.
4 5 6 |
# File 'lib/action_dispatch/journey/route.rb', line 4 def app @app end |
#constraints ⇒ Object (readonly) Also known as: conditions
Returns the value of attribute constraints.
6 7 8 |
# File 'lib/action_dispatch/journey/route.rb', line 6 def constraints @constraints end |
#defaults ⇒ Object (readonly)
Returns the value of attribute defaults.
4 5 6 |
# File 'lib/action_dispatch/journey/route.rb', line 4 def defaults @defaults end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
4 5 6 |
# File 'lib/action_dispatch/journey/route.rb', line 4 def name @name end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
4 5 6 |
# File 'lib/action_dispatch/journey/route.rb', line 4 def path @path end |
#precedence ⇒ Object
Returns the value of attribute precedence.
9 10 11 |
# File 'lib/action_dispatch/journey/route.rb', line 9 def precedence @precedence end |
Instance Method Details
#ast ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/action_dispatch/journey/route.rb', line 36 def ast @decorated_ast ||= begin decorated_ast = path.ast decorated_ast.grep(Nodes::Terminal).each { |n| n.memo = self } decorated_ast end end |
#dispatcher? ⇒ Boolean
108 109 110 |
# File 'lib/action_dispatch/journey/route.rb', line 108 def dispatcher? @dispatcher end |
#format(path_options) ⇒ Object
74 75 76 77 78 79 80 |
# File 'lib/action_dispatch/journey/route.rb', line 74 def format() .delete_if do |key, value| value.to_s == defaults[key].to_s && !required_parts.include?(key) end Visitors::Formatter.new().accept(path.spec) end |
#glob? ⇒ Boolean
104 105 106 |
# File 'lib/action_dispatch/journey/route.rb', line 104 def glob? !path.spec.grep(Nodes::Star).empty? end |
#ip ⇒ Object
131 132 133 |
# File 'lib/action_dispatch/journey/route.rb', line 131 def ip constraints[:ip] || // end |
#matches?(request) ⇒ Boolean
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/action_dispatch/journey/route.rb', line 112 def matches?(request) constraints.all? do |method, value| next true unless request.respond_to?(method) 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 |
#optimized_path ⇒ Object
82 83 84 |
# File 'lib/action_dispatch/journey/route.rb', line 82 def optimized_path Visitors::OptimizedPath.new.accept(path.spec) end |
#optional_parts ⇒ Object
86 87 88 |
# File 'lib/action_dispatch/journey/route.rb', line 86 def optional_parts path.optional_names.map { |n| n.to_sym } end |
#parts ⇒ Object Also known as: segment_keys
69 70 71 |
# File 'lib/action_dispatch/journey/route.rb', line 69 def parts @parts ||= segments.map { |n| n.to_sym } end |
#required_default?(key) ⇒ Boolean
94 95 96 |
# File 'lib/action_dispatch/journey/route.rb', line 94 def required_default?(key) (constraints[:required_defaults] || []).include?(key) end |
#required_defaults ⇒ Object
98 99 100 101 102 |
# File 'lib/action_dispatch/journey/route.rb', line 98 def required_defaults @required_defaults ||= @defaults.dup.delete_if do |k,_| parts.include?(k) || !required_default?(k) end end |
#required_keys ⇒ Object
55 56 57 |
# File 'lib/action_dispatch/journey/route.rb', line 55 def required_keys required_parts + required_defaults.keys end |
#required_parts ⇒ Object
90 91 92 |
# File 'lib/action_dispatch/journey/route.rb', line 90 def required_parts @required_parts ||= path.required_names.map { |n| n.to_sym } end |
#requirements ⇒ Object
:nodoc:
44 45 46 47 48 49 |
# File 'lib/action_dispatch/journey/route.rb', line 44 def requirements # :nodoc: # needed for rails `rake routes` path.requirements.merge(@defaults).delete_if { |_,v| /.+?/ == v } end |
#score(constraints) ⇒ Object
59 60 61 62 63 64 65 66 67 |
# File 'lib/action_dispatch/journey/route.rb', line 59 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 |
#segments ⇒ Object
51 52 53 |
# File 'lib/action_dispatch/journey/route.rb', line 51 def segments path.names end |
#verb ⇒ Object
135 136 137 |
# File 'lib/action_dispatch/journey/route.rb', line 135 def verb constraints[:request_method] || // end |