Class: ActionDispatch::Journey::Route
- Defined in:
- actionpack/lib/action_dispatch/journey/route.rb
Defined Under Namespace
Modules: VerbMatchers
Instance Attribute Summary collapse
-
#app ⇒ Object
readonly
Returns the value of attribute app.
-
#ast ⇒ Object
readonly
Returns the value of attribute ast.
-
#constraints ⇒ Object
(also: #conditions)
readonly
Returns the value of attribute constraints.
-
#defaults ⇒ Object
readonly
Returns the value of attribute defaults.
-
#internal ⇒ Object
readonly
Returns the value of attribute internal.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#precedence ⇒ Object
readonly
Returns the value of attribute precedence.
-
#scope_options ⇒ Object
readonly
Returns the value of attribute scope_options.
Class Method Summary collapse
Instance Method Summary collapse
- #dispatcher? ⇒ Boolean
- #eager_load! ⇒ Object
- #format(path_options) ⇒ Object
- #glob? ⇒ Boolean
-
#initialize(name:, app: nil, path:, constraints: {}, required_defaults: [], defaults: {}, request_method_match: nil, precedence: 0, scope_options: {}, internal: false) ⇒ Route
constructor
path
is a path constraint. - #ip ⇒ Object
- #matches?(request) ⇒ Boolean
- #parts ⇒ Object (also: #segment_keys)
- #required_default?(key) ⇒ Boolean
- #required_defaults ⇒ Object
- #required_keys ⇒ Object
- #required_parts ⇒ Object
-
#requirements ⇒ Object
Needed for ‘bin/rails routes`.
- #requires_matching_verb? ⇒ Boolean
- #score(supplied_keys) ⇒ Object
- #segments ⇒ Object
- #verb ⇒ Object
Constructor Details
#initialize(name:, app: nil, path:, constraints: {}, required_defaults: [], defaults: {}, request_method_match: nil, precedence: 0, scope_options: {}, internal: false) ⇒ Route
path
is a path constraint. constraints
is a hash of constraints to be applied to this route.
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'actionpack/lib/action_dispatch/journey/route.rb', line 56 def initialize(name:, app: nil, path:, constraints: {}, required_defaults: [], defaults: {}, request_method_match: nil, precedence: 0, scope_options: {}, internal: false) @name = name @app = app @path = path @request_method_match = request_method_match @constraints = constraints @defaults = defaults @required_defaults = nil @_required_defaults = required_defaults @required_parts = nil @parts = nil @precedence = precedence @path_formatter = @path.build_formatter @scope_options = @internal = internal @ast = @path.ast.root @path.ast.route = self end |
Instance Attribute Details
#app ⇒ Object (readonly)
Returns the value of attribute app
7 8 9 |
# File 'actionpack/lib/action_dispatch/journey/route.rb', line 7 def app @app end |
#ast ⇒ Object (readonly)
Returns the value of attribute ast
7 8 9 |
# File 'actionpack/lib/action_dispatch/journey/route.rb', line 7 def ast @ast end |
#constraints ⇒ Object (readonly) Also known as: conditions
Returns the value of attribute constraints
7 8 9 |
# File 'actionpack/lib/action_dispatch/journey/route.rb', line 7 def constraints @constraints end |
#defaults ⇒ Object (readonly)
Returns the value of attribute defaults
7 8 9 |
# File 'actionpack/lib/action_dispatch/journey/route.rb', line 7 def defaults @defaults end |
#internal ⇒ Object (readonly)
Returns the value of attribute internal
7 8 9 |
# File 'actionpack/lib/action_dispatch/journey/route.rb', line 7 def internal @internal end |
#name ⇒ Object (readonly)
Returns the value of attribute name
7 8 9 |
# File 'actionpack/lib/action_dispatch/journey/route.rb', line 7 def name @name end |
#path ⇒ Object (readonly)
Returns the value of attribute path
7 8 9 |
# File 'actionpack/lib/action_dispatch/journey/route.rb', line 7 def path @path end |
#precedence ⇒ Object (readonly)
Returns the value of attribute precedence
7 8 9 |
# File 'actionpack/lib/action_dispatch/journey/route.rb', line 7 def precedence @precedence end |
#scope_options ⇒ Object (readonly)
Returns the value of attribute scope_options
7 8 9 |
# File 'actionpack/lib/action_dispatch/journey/route.rb', line 7 def @scope_options end |
Class Method Details
.verb_matcher(verb) ⇒ Object
47 48 49 50 51 |
# File 'actionpack/lib/action_dispatch/journey/route.rb', line 47 def self.verb_matcher(verb) VerbMatchers::VERB_TO_CLASS.fetch(verb) do VerbMatchers::Unknown.new verb.to_s.dasherize.upcase end end |
Instance Method Details
#dispatcher? ⇒ Boolean
141 142 143 |
# File 'actionpack/lib/action_dispatch/journey/route.rb', line 141 def dispatcher? @app.dispatcher? end |
#eager_load! ⇒ Object
77 78 79 80 81 82 |
# File 'actionpack/lib/action_dispatch/journey/route.rb', line 77 def eager_load! path.eager_load! parts required_defaults nil end |
#format(path_options) ⇒ Object
119 120 121 |
# File 'actionpack/lib/action_dispatch/journey/route.rb', line 119 def format() @path_formatter.evaluate end |
#glob? ⇒ Boolean
137 138 139 |
# File 'actionpack/lib/action_dispatch/journey/route.rb', line 137 def glob? path.ast.glob? end |
#ip ⇒ Object
163 164 165 |
# File 'actionpack/lib/action_dispatch/journey/route.rb', line 163 def ip constraints[:ip] || // end |
#matches?(request) ⇒ Boolean
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'actionpack/lib/action_dispatch/journey/route.rb', line 145 def matches?(request) match_verb(request) && constraints.all? { |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 |
#parts ⇒ Object Also known as: segment_keys
114 115 116 |
# File 'actionpack/lib/action_dispatch/journey/route.rb', line 114 def parts @parts ||= segments.map(&:to_sym) end |
#required_default?(key) ⇒ Boolean
127 128 129 |
# File 'actionpack/lib/action_dispatch/journey/route.rb', line 127 def required_default?(key) @_required_defaults.include?(key) end |
#required_defaults ⇒ Object
131 132 133 134 135 |
# File 'actionpack/lib/action_dispatch/journey/route.rb', line 131 def required_defaults @required_defaults ||= @defaults.dup.delete_if do |k, _| parts.include?(k) || !required_default?(k) end end |
#required_keys ⇒ Object
102 103 104 |
# File 'actionpack/lib/action_dispatch/journey/route.rb', line 102 def required_keys required_parts + required_defaults.keys end |
#required_parts ⇒ Object
123 124 125 |
# File 'actionpack/lib/action_dispatch/journey/route.rb', line 123 def required_parts @required_parts ||= path.required_names.map(&:to_sym) end |
#requirements ⇒ Object
Needed for ‘bin/rails routes`. Picks up succinctly defined requirements for a route, for example route
get 'photo/:id', :controller => 'photos', :action => 'show',
:id => /[A-Z]\d{5}/
will have :action=>“show”, :id=>/d{5/} as requirements.
92 93 94 95 96 |
# File 'actionpack/lib/action_dispatch/journey/route.rb', line 92 def requirements @defaults.merge(path.requirements).delete_if { |_, v| /.+?/m == v } end |
#requires_matching_verb? ⇒ Boolean
167 168 169 |
# File 'actionpack/lib/action_dispatch/journey/route.rb', line 167 def requires_matching_verb? !@request_method_match.all? { |x| x == VerbMatchers::All } end |
#score(supplied_keys) ⇒ Object
106 107 108 109 110 111 112 |
# File 'actionpack/lib/action_dispatch/journey/route.rb', line 106 def score(supplied_keys) path.required_names.each do |k| return -1 unless supplied_keys.include?(k) end (required_defaults.length * 2) + path.names.count { |k| supplied_keys.include?(k) } end |
#segments ⇒ Object
98 99 100 |
# File 'actionpack/lib/action_dispatch/journey/route.rb', line 98 def segments path.names end |
#verb ⇒ Object
171 172 173 |
# File 'actionpack/lib/action_dispatch/journey/route.rb', line 171 def verb verbs.join("|") end |