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.
-
#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.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/journey/route.rb', line 11 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 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 |
#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
28 29 30 31 32 33 34 |
# File 'lib/journey/route.rb', line 28 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
66 67 68 69 70 71 72 73 74 75 |
# File 'lib/journey/route.rb', line 66 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
77 78 79 |
# File 'lib/journey/route.rb', line 77 def optional_parts path.optional_names.map { |n| n.to_sym } end |
#parts ⇒ Object Also known as: segment_keys
61 62 63 |
# File 'lib/journey/route.rb', line 61 def parts @parts ||= segments.map { |n| n.to_sym } end |
#required_defaults ⇒ Object
85 86 87 88 89 90 |
# File 'lib/journey/route.rb', line 85 def required_defaults @required_defaults ||= begin matches = parts @defaults.dup.delete_if { |k,_| matches.include? k } end end |
#required_keys ⇒ Object
47 48 49 |
# File 'lib/journey/route.rb', line 47 def required_keys path.required_names.map { |x| x.to_sym } + required_defaults.keys end |
#required_parts ⇒ Object
81 82 83 |
# File 'lib/journey/route.rb', line 81 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/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/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/journey/route.rb', line 43 def segments @path.names end |