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
- #format(path_options) ⇒ Object
-
#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 |
# 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 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
28 29 30 31 32 33 34 |
# File 'lib/action_dispatch/journey/route.rb', line 28 def ast @decorated_ast ||= begin decorated_ast = path.ast decorated_ast.grep(Nodes::Terminal).each { |n| n.memo = self } decorated_ast end end |
#format(path_options) ⇒ Object
66 67 68 69 70 71 72 |
# File 'lib/action_dispatch/journey/route.rb', line 66 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 |
#ip ⇒ Object
115 116 117 |
# File 'lib/action_dispatch/journey/route.rb', line 115 def ip constraints[:ip] || // end |
#matches?(request) ⇒ Boolean
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/action_dispatch/journey/route.rb', line 96 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
74 75 76 |
# File 'lib/action_dispatch/journey/route.rb', line 74 def optimized_path Visitors::OptimizedPath.new.accept(path.spec) end |
#optional_parts ⇒ Object
78 79 80 |
# File 'lib/action_dispatch/journey/route.rb', line 78 def optional_parts path.optional_names.map { |n| n.to_sym } end |
#parts ⇒ Object Also known as: segment_keys
61 62 63 |
# File 'lib/action_dispatch/journey/route.rb', line 61 def parts @parts ||= segments.map { |n| n.to_sym } end |
#required_default?(key) ⇒ Boolean
86 87 88 |
# File 'lib/action_dispatch/journey/route.rb', line 86 def required_default?(key) (constraints[:required_defaults] || []).include?(key) end |
#required_defaults ⇒ Object
90 91 92 93 94 |
# File 'lib/action_dispatch/journey/route.rb', line 90 def required_defaults @required_defaults ||= @defaults.dup.delete_if do |k,_| parts.include?(k) || !required_default?(k) end end |
#required_keys ⇒ Object
47 48 49 |
# File 'lib/action_dispatch/journey/route.rb', line 47 def required_keys required_parts + required_defaults.keys end |
#required_parts ⇒ Object
82 83 84 |
# File 'lib/action_dispatch/journey/route.rb', line 82 def required_parts @required_parts ||= path.required_names.map { |n| n.to_sym } end |
#requirements ⇒ Object
:nodoc:
36 37 38 39 40 41 |
# File 'lib/action_dispatch/journey/route.rb', line 36 def requirements # :nodoc: # needed for rails `rake routes` path.requirements.merge(@defaults).delete_if { |_,v| /.+?/ == v } end |
#score(constraints) ⇒ Object
51 52 53 54 55 56 57 58 59 |
# File 'lib/action_dispatch/journey/route.rb', line 51 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
43 44 45 |
# File 'lib/action_dispatch/journey/route.rb', line 43 def segments path.names end |
#verb ⇒ Object
119 120 121 |
# File 'lib/action_dispatch/journey/route.rb', line 119 def verb constraints[:request_method] || // end |