Class: ActionDispatch::Journey::Route
- Inherits:
-
Object
- Object
- ActionDispatch::Journey::Route
- Defined in:
- lib/action_dispatch/journey/route.rb
Overview
:nodoc:
Defined Under Namespace
Modules: VerbMatchers
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.
-
#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.
Class Method Summary collapse
- .build(name, app, path, constraints, required_defaults, defaults) ⇒ Object
- .verb_matcher(verb) ⇒ Object
Instance Method Summary collapse
- #ast ⇒ Object
- #dispatcher? ⇒ Boolean
- #format(path_options) ⇒ Object
- #glob? ⇒ Boolean
-
#initialize(name, app, path, constraints, required_defaults, defaults, request_method_match, precedence, 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
:nodoc:.
- #requires_matching_verb? ⇒ Boolean
- #score(constraints) ⇒ Object
- #segments ⇒ Object
- #verb ⇒ Object
Constructor Details
#initialize(name, app, path, constraints, required_defaults, defaults, request_method_match, precedence, internal = false) ⇒ Route
path
is a path constraint. constraints
is a hash of constraints to be applied to this route.
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/action_dispatch/journey/route.rb', line 58 def initialize(name, app, path, constraints, required_defaults, defaults, request_method_match, precedence, 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 @decorated_ast = nil @precedence = precedence @path_formatter = @path.build_formatter @internal = internal 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 |
#internal ⇒ Object (readonly)
Returns the value of attribute internal.
6 7 8 |
# File 'lib/action_dispatch/journey/route.rb', line 6 def internal @internal 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 (readonly)
Returns the value of attribute precedence.
4 5 6 |
# File 'lib/action_dispatch/journey/route.rb', line 4 def precedence @precedence end |
Class Method Details
.build(name, app, path, constraints, required_defaults, defaults) ⇒ Object
50 51 52 53 |
# File 'lib/action_dispatch/journey/route.rb', line 50 def self.build(name, app, path, constraints, required_defaults, defaults) request_method_match = verb_matcher(constraints.delete(:request_method)) new name, app, path, constraints, required_defaults, defaults, request_method_match, 0 end |
.verb_matcher(verb) ⇒ Object
44 45 46 47 48 |
# File 'lib/action_dispatch/journey/route.rb', line 44 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
#ast ⇒ Object
76 77 78 79 80 81 82 |
# File 'lib/action_dispatch/journey/route.rb', line 76 def ast @decorated_ast ||= begin decorated_ast = path.ast decorated_ast.find_all(&:terminal?).each { |n| n.memo = self } decorated_ast end end |
#dispatcher? ⇒ Boolean
136 137 138 |
# File 'lib/action_dispatch/journey/route.rb', line 136 def dispatcher? @app.dispatcher? end |
#format(path_options) ⇒ Object
114 115 116 |
# File 'lib/action_dispatch/journey/route.rb', line 114 def format() @path_formatter.evaluate end |
#glob? ⇒ Boolean
132 133 134 |
# File 'lib/action_dispatch/journey/route.rb', line 132 def glob? !path.spec.grep(Nodes::Star).empty? end |
#ip ⇒ Object
158 159 160 |
# File 'lib/action_dispatch/journey/route.rb', line 158 def ip constraints[:ip] || // end |
#matches?(request) ⇒ Boolean
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/action_dispatch/journey/route.rb', line 140 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
109 110 111 |
# File 'lib/action_dispatch/journey/route.rb', line 109 def parts @parts ||= segments.map(&:to_sym) end |
#required_default?(key) ⇒ Boolean
122 123 124 |
# File 'lib/action_dispatch/journey/route.rb', line 122 def required_default?(key) @_required_defaults.include?(key) end |
#required_defaults ⇒ Object
126 127 128 129 130 |
# File 'lib/action_dispatch/journey/route.rb', line 126 def required_defaults @required_defaults ||= @defaults.dup.delete_if do |k,_| parts.include?(k) || !required_default?(k) end end |
#required_keys ⇒ Object
95 96 97 |
# File 'lib/action_dispatch/journey/route.rb', line 95 def required_keys required_parts + required_defaults.keys end |
#required_parts ⇒ Object
118 119 120 |
# File 'lib/action_dispatch/journey/route.rb', line 118 def required_parts @required_parts ||= path.required_names.map(&:to_sym) end |
#requirements ⇒ Object
:nodoc:
84 85 86 87 88 89 |
# File 'lib/action_dispatch/journey/route.rb', line 84 def requirements # :nodoc: # needed for rails `rails routes` @defaults.merge(path.requirements).delete_if { |_,v| /.+?/ == v } end |
#requires_matching_verb? ⇒ Boolean
162 163 164 |
# File 'lib/action_dispatch/journey/route.rb', line 162 def requires_matching_verb? !@request_method_match.all? { |x| x == VerbMatchers::All } end |
#score(constraints) ⇒ Object
99 100 101 102 103 104 105 106 107 |
# File 'lib/action_dispatch/journey/route.rb', line 99 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
91 92 93 |
# File 'lib/action_dispatch/journey/route.rb', line 91 def segments path.names end |
#verb ⇒ Object
166 167 168 |
# File 'lib/action_dispatch/journey/route.rb', line 166 def verb verbs.join('|') end |