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
- #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 |
# File 'lib/action_dispatch/journey/route.rb', line 14 def initialize(name, app, path, constraints, defaults = {}) @name = name @app = app @path = path @constraints = constraints @defaults = defaults @required_defaults = nil @required_parts = nil @parts = nil @decorated_ast = nil @precedence = 0 @path_formatter = @path.build_formatter 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
29 30 31 32 33 34 35 |
# File 'lib/action_dispatch/journey/route.rb', line 29 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
93 94 95 |
# File 'lib/action_dispatch/journey/route.rb', line 93 def dispatcher? @app.dispatcher? end |
#format(path_options) ⇒ Object
67 68 69 |
# File 'lib/action_dispatch/journey/route.rb', line 67 def format() @path_formatter.evaluate end |
#glob? ⇒ Boolean
89 90 91 |
# File 'lib/action_dispatch/journey/route.rb', line 89 def glob? !path.spec.grep(Nodes::Star).empty? end |
#ip ⇒ Object
116 117 118 |
# File 'lib/action_dispatch/journey/route.rb', line 116 def ip constraints[:ip] || // end |
#matches?(request) ⇒ Boolean
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/action_dispatch/journey/route.rb', line 97 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 |
#optional_parts ⇒ Object
71 72 73 |
# File 'lib/action_dispatch/journey/route.rb', line 71 def optional_parts path.optional_names.map { |n| n.to_sym } end |
#parts ⇒ Object Also known as: segment_keys
62 63 64 |
# File 'lib/action_dispatch/journey/route.rb', line 62 def parts @parts ||= segments.map { |n| n.to_sym } end |
#required_default?(key) ⇒ Boolean
79 80 81 |
# File 'lib/action_dispatch/journey/route.rb', line 79 def required_default?(key) (constraints[:required_defaults] || []).include?(key) end |
#required_defaults ⇒ Object
83 84 85 86 87 |
# File 'lib/action_dispatch/journey/route.rb', line 83 def required_defaults @required_defaults ||= @defaults.dup.delete_if do |k,_| parts.include?(k) || !required_default?(k) end end |
#required_keys ⇒ Object
48 49 50 |
# File 'lib/action_dispatch/journey/route.rb', line 48 def required_keys required_parts + required_defaults.keys end |
#required_parts ⇒ Object
75 76 77 |
# File 'lib/action_dispatch/journey/route.rb', line 75 def required_parts @required_parts ||= path.required_names.map { |n| n.to_sym } end |
#requirements ⇒ Object
:nodoc:
37 38 39 40 41 42 |
# File 'lib/action_dispatch/journey/route.rb', line 37 def requirements # :nodoc: # needed for rails `rake routes` @defaults.merge(path.requirements).delete_if { |_,v| /.+?/ == v } end |
#score(constraints) ⇒ Object
52 53 54 55 56 57 58 59 60 |
# File 'lib/action_dispatch/journey/route.rb', line 52 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
44 45 46 |
# File 'lib/action_dispatch/journey/route.rb', line 44 def segments path.names end |
#verb ⇒ Object
120 121 122 |
# File 'lib/action_dispatch/journey/route.rb', line 120 def verb constraints[:request_method] || // end |