Class: Dao::Route
- Inherits:
-
String
- Object
- String
- Dao::Route
- Defined in:
- lib/dao/route.rb
Defined Under Namespace
Classes: List
Constant Summary collapse
- Default =
'/index'.freeze
Instance Attribute Summary collapse
-
#keys ⇒ Object
Returns the value of attribute keys.
-
#pattern ⇒ Object
Returns the value of attribute pattern.
Class Method Summary collapse
- .default ⇒ Object
- .keys_for(route) ⇒ Object
- .like?(route) ⇒ Boolean
- .path_for(route, params = {}) ⇒ Object
- .pattern_for(route) ⇒ Object
Instance Method Summary collapse
-
#initialize(path) ⇒ Route
constructor
A new instance of Route.
- #match(path) ⇒ Object
- #params_for(path) ⇒ Object
- #path ⇒ Object
- #path_for(params) ⇒ Object
Constructor Details
#initialize(path) ⇒ Route
Returns a new instance of Route.
40 41 42 43 44 45 |
# File 'lib/dao/route.rb', line 40 def initialize(path) replace(path.to_s) @keys = Route.keys_for(self).freeze @pattern = Route.pattern_for(self).freeze freeze end |
Instance Attribute Details
#keys ⇒ Object
Returns the value of attribute keys.
37 38 39 |
# File 'lib/dao/route.rb', line 37 def keys @keys end |
#pattern ⇒ Object
Returns the value of attribute pattern.
38 39 40 |
# File 'lib/dao/route.rb', line 38 def pattern @pattern end |
Class Method Details
.default ⇒ Object
6 7 8 |
# File 'lib/dao/route.rb', line 6 def default Default end |
.keys_for(route) ⇒ Object
14 15 16 17 |
# File 'lib/dao/route.rb', line 14 def keys_for(route) route = Path.absolute_path_for(route.to_s) route.scan(%r{/:[^/]+}).map{|key| key.sub(%r{^/:}, '')} end |
.like?(route) ⇒ Boolean
10 11 12 |
# File 'lib/dao/route.rb', line 10 def like?(route) route.to_s =~ %r{/:[^/]+} end |
.path_for(route, params = {}) ⇒ Object
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/dao/route.rb', line 25 def path_for(route, params = {}) path = Path.absolute_path_for(route.to_s) params = Map.new(params) params.each do |key, val| re = %r{/:#{ Regexp.escape(key.to_s) }(\Z|/)} repl = "/#{ val.to_s }\\1" path.gsub!(re, repl) end path end |
.pattern_for(route) ⇒ Object
19 20 21 22 23 |
# File 'lib/dao/route.rb', line 19 def pattern_for(route) route = Path.absolute_path_for(route.to_s) re = route.gsub(%r{/:[^/]+}, '/([^/]+)') /#{ re }/ioux end |
Instance Method Details
#match(path) ⇒ Object
55 56 57 |
# File 'lib/dao/route.rb', line 55 def match(path) match = pattern.match(path).to_a end |
#params_for(path) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/dao/route.rb', line 59 def params_for(path) match = pattern.match(path).to_a if match map = Map.new ignored = match.shift @keys.each_with_index do |key, index| map[key] = match[index] end map end end |
#path ⇒ Object
47 48 49 |
# File 'lib/dao/route.rb', line 47 def path self end |