Class: Usher
- Inherits:
-
Object
- Object
- Usher
- Defined in:
- lib/usher.rb,
lib/usher/node.rb,
lib/usher/util.rb,
lib/usher/route.rb,
lib/usher/grapher.rb,
lib/usher/splitter.rb,
lib/usher/interface.rb,
lib/usher/node/root.rb,
lib/usher/delimiters.rb,
lib/usher/exceptions.rb,
lib/usher/route/path.rb,
lib/usher/route/util.rb,
lib/usher/util/rails.rb,
lib/usher/util/parser.rb,
lib/usher/route/static.rb,
lib/usher/node/response.rb,
lib/usher/util/generate.rb,
lib/usher/interface/rack.rb,
lib/usher/interface/text.rb,
lib/usher/route/variable.rb,
lib/usher/interface/rails3.rb,
lib/usher/interface/rails20.rb,
lib/usher/interface/rails22.rb,
lib/usher/interface/rails23.rb,
lib/usher/interface/sinatra.rb,
lib/usher/interface/rack/route.rb,
lib/usher/node/failed_response.rb,
lib/usher/route/request_method.rb,
lib/usher/interface/rack/builder.rb,
lib/usher/interface/rails22/mapper.rb,
lib/usher/interface/rails23/mapper.rb,
lib/usher/interface/rack/middleware.rb,
lib/usher/node/root_ignoring_trailing_delimiters.rb
Overview
Main class for routing. If you’re going to be routing for a specific context, like rails or rack, you probably want to use an interface. Otherwise, this is the main class that actually does all the work.
Defined Under Namespace
Modules: Interface, Util Classes: Delimiters, DoubleRegexpException, Grapher, MissingParameterException, MultipleParameterException, Node, Route, Splitter, UnrecognizedException, ValidationException
Instance Attribute Summary collapse
-
#delimiters ⇒ Object
readonly
Returns the value of attribute delimiters.
-
#delimiters_regex ⇒ Object
readonly
Returns the value of attribute delimiters_regex.
-
#generator ⇒ Object
readonly
Returns the value of attribute generator.
-
#grapher ⇒ Object
readonly
Returns the value of attribute grapher.
-
#named_routes ⇒ Object
readonly
Returns the value of attribute named_routes.
-
#parent_route ⇒ Object
Returns the value of attribute parent_route.
-
#parser ⇒ Object
readonly
Returns the value of attribute parser.
-
#root ⇒ Object
readonly
Returns the value of attribute root.
-
#route_class ⇒ Object
Returns the value of attribute route_class.
-
#routes ⇒ Object
readonly
Returns the value of attribute routes.
-
#splitter ⇒ Object
readonly
Returns the value of attribute splitter.
Instance Method Summary collapse
- #add_meta(meta, path, options = nil) ⇒ Object
-
#add_named_route(name, path, options = nil) ⇒ Route
Adds a route referencable by ‘name`.
-
#add_route(path, options = nil) ⇒ Route
Creates a route from ‘path` and `options`.
-
#allow_identical_variable_names? ⇒ Boolean
State of allow_identical_variable_names feature.
-
#can_generate? ⇒ Boolean
Able to generate.
-
#consider_destination_keys? ⇒ Boolean
State of consider_destination_keys feature.
-
#delete_named_route(name, path, options = nil) ⇒ Route
Deletes a route referencable by ‘name`.
-
#delete_route(path, options = nil) ⇒ Route
Deletes a route.
-
#detailed_failure? ⇒ Boolean
State of detailed_failure feature.
-
#dup ⇒ Usher
Duplicates the router.
-
#empty? ⇒ Boolean
Whether the route set is empty.
-
#ignore_trailing_delimiters? ⇒ Boolean
State of ignore_trailing_delimiters feature.
-
#initialize(options = nil) ⇒ Usher
constructor
Creates a route set, with options.
- #inspect ⇒ Object
-
#name(name, route) ⇒ Route
Attaches a ‘route` to a `name`.
-
#path_for_options(options) ⇒ nil, Route::Path
Recognizes a set of ‘parameters` and gets the closest matching Usher::Route::Path or `nil` if no route exists.
-
#priority_lookups? ⇒ Boolean
State of priority_lookups feature.
-
#recognize(request, path = request.path) ⇒ nil, Node::Response
Recognizes a ‘request`.
-
#recognize_path(path) ⇒ nil, Node::Response
Recognizes a ‘path`.
-
#reset! ⇒ Object
Resets the route set back to its initial state.
-
#route_count ⇒ Number
The number of routes currently mapped.
- #to_s ⇒ Object
Constructor Details
#initialize(options = nil) ⇒ Usher
Creates a route set, with options
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/usher.rb', line 74 def initialize( = nil) self.route_class = Usher::Route self.generator = && .delete(:generator) self.delimiters = Delimiters.new( && .delete(:delimiters) || ['/', '.']) self.valid_regex = && .delete(:valid_regex) || '[0-9A-Za-z\$\-_\+!\*\',]+' self.request_methods = && .delete(:request_methods) self.ignore_trailing_delimiters = && .key?(:ignore_trailing_delimiters) ? .delete(:ignore_trailing_delimiters) : false self.consider_destination_keys = && .key?(:consider_destination_keys) ? .delete(:consider_destination_keys) : false self.allow_identical_variable_names = && .key?(:allow_identical_variable_names) ? .delete(:allow_identical_variable_names) : true self.detailed_failure = && .key?(:detailed_failure) ? .delete(:detailed_failure) : false unless .nil? || .empty? raise "unrecognized options -- #{.keys.join(', ')}" end reset! end |
Instance Attribute Details
#delimiters ⇒ Object
Returns the value of attribute delimiters.
26 27 28 |
# File 'lib/usher.rb', line 26 def delimiters @delimiters end |
#delimiters_regex ⇒ Object (readonly)
Returns the value of attribute delimiters_regex.
26 27 28 |
# File 'lib/usher.rb', line 26 def delimiters_regex @delimiters_regex end |
#generator ⇒ Object
Returns the value of attribute generator.
26 27 28 |
# File 'lib/usher.rb', line 26 def generator @generator end |
#grapher ⇒ Object (readonly)
Returns the value of attribute grapher.
26 27 28 |
# File 'lib/usher.rb', line 26 def grapher @grapher end |
#named_routes ⇒ Object (readonly)
Returns the value of attribute named_routes.
26 27 28 |
# File 'lib/usher.rb', line 26 def named_routes @named_routes end |
#parent_route ⇒ Object
Returns the value of attribute parent_route.
26 27 28 |
# File 'lib/usher.rb', line 26 def parent_route @parent_route end |
#parser ⇒ Object
Returns the value of attribute parser.
26 27 28 |
# File 'lib/usher.rb', line 26 def parser @parser end |
#root ⇒ Object (readonly)
Returns the value of attribute root.
26 27 28 |
# File 'lib/usher.rb', line 26 def root @root end |
#route_class ⇒ Object
Returns the value of attribute route_class.
28 29 30 |
# File 'lib/usher.rb', line 28 def route_class @route_class end |
#routes ⇒ Object (readonly)
Returns the value of attribute routes.
26 27 28 |
# File 'lib/usher.rb', line 26 def routes @routes end |
#splitter ⇒ Object (readonly)
Returns the value of attribute splitter.
26 27 28 |
# File 'lib/usher.rb', line 26 def splitter @splitter end |
Instance Method Details
#add_meta(meta, path, options = nil) ⇒ Object
255 256 257 258 |
# File 'lib/usher.rb', line 255 def (, path, = nil) route = get_route(path, ) root.(route, ) end |
#add_named_route(name, path, options = nil) ⇒ Route
Adds a route referencable by ‘name`. See #add_route for format `path` and `options`.
129 130 131 |
# File 'lib/usher.rb', line 129 def add_named_route(name, path, = nil) add_route(path, ).name(name) end |
#add_route(path, options = nil) ⇒ Route
Creates a route from ‘path` and `options`
232 233 234 235 236 237 238 239 |
# File 'lib/usher.rb', line 232 def add_route(path, = nil) route = get_route(path, ) root.add(route) routes << route grapher.add_route(route) route.parent_route = parent_route if parent_route route end |
#allow_identical_variable_names? ⇒ Boolean
Returns State of allow_identical_variable_names feature.
92 93 94 |
# File 'lib/usher.rb', line 92 def allow_identical_variable_names? @allow_identical_variable_names end |
#can_generate? ⇒ Boolean
Returns Able to generate.
117 118 119 |
# File 'lib/usher.rb', line 117 def can_generate? !generator.nil? end |
#consider_destination_keys? ⇒ Boolean
Returns State of consider_destination_keys feature.
107 108 109 |
# File 'lib/usher.rb', line 107 def consider_destination_keys? @consider_destination_keys end |
#delete_named_route(name, path, options = nil) ⇒ Route
Deletes a route referencable by ‘name`. At least the path and conditions have to match the route you intend to delete.
141 142 143 144 |
# File 'lib/usher.rb', line 141 def delete_named_route(name, path, = nil) delete_route(path, ) named_routes.delete(name) end |
#delete_route(path, options = nil) ⇒ Route
Deletes a route. At least the path and conditions have to match the route you intend to delete.
247 248 249 250 251 252 253 |
# File 'lib/usher.rb', line 247 def delete_route(path, = nil) route = get_route(path, ) root.delete(route) routes.replace(root.unique_routes) build_grapher! route end |
#detailed_failure? ⇒ Boolean
Returns State of detailed_failure feature.
97 98 99 |
# File 'lib/usher.rb', line 97 def detailed_failure? @detailed_failure end |
#dup ⇒ Usher
Duplicates the router.
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 |
# File 'lib/usher.rb', line 305 def dup replacement = super original = self inverted_named_routes = original.named_routes.invert replacement.instance_eval do reset! original.routes.each do |route| new_route = route.dup new_route.router = self root.add(new_route) routes << new_route if name = inverted_named_routes[route] named_routes[name] = new_route end end send(:generator=, original.generator.class.new) if original.can_generate? build_grapher! end replacement end |
#empty? ⇒ Boolean
Returns Whether the route set is empty.
36 37 38 |
# File 'lib/usher.rb', line 36 def empty? routes.empty? end |
#ignore_trailing_delimiters? ⇒ Boolean
Returns State of ignore_trailing_delimiters feature.
102 103 104 |
# File 'lib/usher.rb', line 102 def ignore_trailing_delimiters? @ignore_trailing_delimiters end |
#inspect ⇒ Object
326 327 328 |
# File 'lib/usher.rb', line 326 def inspect "#<Usher:0x%x route_count=%d delimiters=%s request_methods=%s ignore_trailing_delimiters? %s consider_destination_keys? %s can_generate? %s priority_lookups? %s>" % [self.object_id, route_count, self.delimiters.inspect, request_methods.inspect, ignore_trailing_delimiters?.inspect, consider_destination_keys?.inspect, can_generate?.inspect, priority_lookups?.inspect] end |
#name(name, route) ⇒ Route
Attaches a ‘route` to a `name`
154 155 156 157 |
# File 'lib/usher.rb', line 154 def name(name, route) named_routes[name.to_sym] = route route end |
#path_for_options(options) ⇒ nil, Route::Path
Recognizes a set of ‘parameters` and gets the closest matching Usher::Route::Path or `nil` if no route exists.
292 293 294 |
# File 'lib/usher.rb', line 292 def () grapher.find_matching_path() end |
#priority_lookups? ⇒ Boolean
Returns State of priority_lookups feature.
112 113 114 |
# File 'lib/usher.rb', line 112 def priority_lookups? @priority_lookups end |
#recognize(request, path = request.path) ⇒ nil, Node::Response
Recognizes a ‘request`
269 270 271 |
# File 'lib/usher.rb', line 269 def recognize(request, path = request.path) root.lookup(request, path) end |
#recognize_path(path) ⇒ nil, Node::Response
Recognizes a ‘path`
281 282 283 |
# File 'lib/usher.rb', line 281 def recognize_path(path) recognize(nil, path) end |
#reset! ⇒ Object
Resets the route set back to its initial state
53 54 55 56 57 58 59 60 |
# File 'lib/usher.rb', line 53 def reset! @root = class_for_root.new(self, request_methods) @named_routes = {} @routes = [] @grapher = Grapher.new(self) @priority_lookups = false @parser = Util::Parser.new(self, valid_regex) end |
#route_count ⇒ Number
Returns The number of routes currently mapped.
42 43 44 |
# File 'lib/usher.rb', line 42 def route_count routes.size end |
#to_s ⇒ Object
330 331 332 |
# File 'lib/usher.rb', line 330 def to_s inspect end |