Class: Journey::Route
- Inherits:
-
Object
- Object
- Journey::Route
- Defined in:
- lib/journey/route.rb
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.
-
#ip ⇒ Object
readonly
Returns the value of attribute ip.
-
#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.
-
#verb ⇒ Object
readonly
Returns the value of attribute verb.
Instance Method Summary collapse
- #ast ⇒ Object
- #format(path_options) ⇒ Object
-
#initialize(name, app, path, constraints, defaults = {}) ⇒ Route
constructor
path
is a path constraint. - #optional_parts ⇒ Object
- #parts ⇒ Object (also: #segment_keys)
- #required_defaults ⇒ Object
- #required_keys ⇒ Object
- #required_parts ⇒ Object
-
#requirements ⇒ Object
:nodoc:.
- #score(constraints) ⇒ Object
- #segments ⇒ 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.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/journey/route.rb', line 13 def initialize name, app, path, constraints, defaults = {} constraints = constraints.dup @name = name @app = app @path = path @verb = constraints[:request_method] || // @ip = constraints.delete(:ip) || // @constraints = constraints @constraints.keep_if { |_,v| Regexp === v || String === v } @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.
3 4 5 |
# File 'lib/journey/route.rb', line 3 def app @app end |
#constraints ⇒ Object (readonly) Also known as: conditions
Returns the value of attribute constraints.
5 6 7 |
# File 'lib/journey/route.rb', line 5 def constraints @constraints end |
#defaults ⇒ Object (readonly)
Returns the value of attribute defaults.
3 4 5 |
# File 'lib/journey/route.rb', line 3 def defaults @defaults end |
#ip ⇒ Object (readonly)
Returns the value of attribute ip.
3 4 5 |
# File 'lib/journey/route.rb', line 3 def ip @ip end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/journey/route.rb', line 3 def name @name end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
3 4 5 |
# File 'lib/journey/route.rb', line 3 def path @path end |
#precedence ⇒ Object
Returns the value of attribute precedence.
8 9 10 |
# File 'lib/journey/route.rb', line 8 def precedence @precedence end |
#verb ⇒ Object (readonly)
Returns the value of attribute verb.
3 4 5 |
# File 'lib/journey/route.rb', line 3 def verb @verb end |
Instance Method Details
#ast ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/journey/route.rb', line 31 def ast return @decorated_ast if @decorated_ast @decorated_ast = path.ast @decorated_ast.grep(Nodes::Terminal).each { |n| n.memo = self } @decorated_ast end |
#format(path_options) ⇒ Object
69 70 71 72 73 74 75 76 77 78 |
# File 'lib/journey/route.rb', line 69 def format (defaults.keys - required_parts).each do |key| .delete key if defaults[key].to_s == [key].to_s end formatter = Visitors::Formatter.new() formatted_path = formatter.accept(path.spec) formatted_path.gsub(/\/\x00/, '') end |
#optional_parts ⇒ Object
80 81 82 |
# File 'lib/journey/route.rb', line 80 def optional_parts path.optional_names.map { |n| n.to_sym } end |
#parts ⇒ Object Also known as: segment_keys
64 65 66 |
# File 'lib/journey/route.rb', line 64 def parts @parts ||= segments.map { |n| n.to_sym } end |
#required_defaults ⇒ Object
88 89 90 91 92 93 |
# File 'lib/journey/route.rb', line 88 def required_defaults @required_defaults ||= begin matches = parts @defaults.dup.delete_if { |k,_| matches.include? k } end end |
#required_keys ⇒ Object
50 51 52 |
# File 'lib/journey/route.rb', line 50 def required_keys path.required_names.map { |x| x.to_sym } + required_defaults.keys end |
#required_parts ⇒ Object
84 85 86 |
# File 'lib/journey/route.rb', line 84 def required_parts @required_parts ||= path.required_names.map { |n| n.to_sym } end |
#requirements ⇒ Object
:nodoc:
39 40 41 42 43 44 |
# File 'lib/journey/route.rb', line 39 def requirements # :nodoc: # needed for rails `rake routes` path.requirements.merge(@defaults).delete_if { |_,v| /.+?/ == v } end |
#score(constraints) ⇒ Object
54 55 56 57 58 59 60 61 62 |
# File 'lib/journey/route.rb', line 54 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
46 47 48 |
# File 'lib/journey/route.rb', line 46 def segments @path.names end |