Class: Pendragon::Router
- Inherits:
-
Object
- Object
- Pendragon::Router
- Defined in:
- lib/pendragon/router.rb
Overview
A class for the router
Direct Known Subclasses
Constant Summary collapse
- @@mutex =
Mutex.new
Instance Attribute Summary collapse
-
#current ⇒ Object
The accessors are useful to access from Pendragon::Route.
-
#routes ⇒ Object
The accessors are useful to access from Pendragon::Route.
Instance Method Summary collapse
-
#add(verb, path, options = {}, &block) ⇒ Pendragon::Route
Adds a new route to router.
-
#call(env) ⇒ Object
Finds the routes if request method is valid.
-
#configuration ⇒ Pendragon::Configuration
Returns Pendragon configuration.
- #delete(path, options = {}, &block) ⇒ Object
-
#get(path, options = {}, &block) ⇒ Object
Provides some methods intuitive than #add Basic usage is the same as #add.
- #head(path, options = {}, &block) ⇒ Object
-
#increment_order! ⇒ Object
Increments for the integrity of priorities.
-
#initialize(&block) ⇒ Router
constructor
Constructs a new instance of Pendragon::Router Possible to pass the block.
-
#invoke(route, params) ⇒ Array<Fixnum, Hash, Array>
Calls a route, and build return value of the router.
-
#path(name, *args) ⇒ String, Regexp
Returns an expanded path matched with the conditions as arguments.
- #post(path, options = {}, &block) ⇒ Object
-
#prepare! ⇒ Object
Prepares the router for route’s priority This method is executed only once in the initial load.
-
#prepared? ⇒ Boolean
The router is already prepared?.
- #put(path, options = {}, &block) ⇒ Object
-
#recognize(request) ⇒ Array
Recognizes the route by request.
-
#recognize_path(path_info) ⇒ Array
Recognizes a given path.
-
#reset! ⇒ Object
Resets the router’s instance variables.
Constructor Details
#initialize(&block) ⇒ Router
Constructs a new instance of Pendragon::Router Possible to pass the block
40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/pendragon/router.rb', line 40 def initialize(&block) reset! if block_given? if block.arity.zero? instance_eval(&block) else @configuration = Configuration.new block.call(configuration) end end end |
Instance Attribute Details
#current ⇒ Object
The accessors are useful to access from Pendragon::Route
22 23 24 |
# File 'lib/pendragon/router.rb', line 22 def current @current end |
#routes ⇒ Object
The accessors are useful to access from Pendragon::Route
22 23 24 |
# File 'lib/pendragon/router.rb', line 22 def routes @routes end |
Instance Method Details
#add(verb, path, options = {}, &block) ⇒ Pendragon::Route
Adds a new route to router
86 87 88 89 90 |
# File 'lib/pendragon/router.rb', line 86 def add(verb, path, = {}, &block) routes << (route = Route.new(path, verb, , &block)) route.router = self route end |
#call(env) ⇒ Object
Finds the routes if request method is valid
54 55 56 57 58 59 60 61 |
# File 'lib/pendragon/router.rb', line 54 def call(env) request = Rack::Request.new(env) recognize(request).each do |route, params| catch(:pass){ return invoke(route, params) } end rescue BadRequest, NotFound, MethodNotAllowed $!.call end |
#configuration ⇒ Pendragon::Configuration
Returns Pendragon configuration
148 149 150 |
# File 'lib/pendragon/router.rb', line 148 def configuration @configuration || Pendragon.configuration end |
#delete(path, options = {}, &block) ⇒ Object
80 |
# File 'lib/pendragon/router.rb', line 80 def delete(path, = {}, &block); add :delete, path, , &block end |
#get(path, options = {}, &block) ⇒ Object
Provides some methods intuitive than #add Basic usage is the same as #add
78 |
# File 'lib/pendragon/router.rb', line 78 def get(path, = {}, &block); add :get, path, , &block end |
#head(path, options = {}, &block) ⇒ Object
82 |
# File 'lib/pendragon/router.rb', line 82 def head(path, = {}, &block); add :head, path, , &block end |
#increment_order! ⇒ Object
Increments for the integrity of priorities
113 114 115 |
# File 'lib/pendragon/router.rb', line 113 def increment_order! @current += 1 end |
#invoke(route, params) ⇒ Array<Fixnum, Hash, Array>
Calls a route, and build return value of the router
67 68 69 70 71 72 73 |
# File 'lib/pendragon/router.rb', line 67 def invoke(route, params) response = route.arity != 0 ? route.call(params) : route.call return response unless configuration.auto_rack_format? status = route.[:status] || 200 header = {'Content-Type' => 'text/html;charset=utf-8'}.merge(route.[:header] || {}) [status, header, Array(response)] end |
#path(name, *args) ⇒ String, Regexp
Returns an expanded path matched with the conditions as arguments
140 141 142 143 144 |
# File 'lib/pendragon/router.rb', line 140 def path(name, *args) extract_with(name, *args) do |route, params, matcher| matcher.mustermann? ? matcher.(params) : route.path end end |
#post(path, options = {}, &block) ⇒ Object
79 |
# File 'lib/pendragon/router.rb', line 79 def post(path, = {}, &block); add :post, path, , &block end |
#prepare! ⇒ Object
Prepares the router for route’s priority This method is executed only once in the initial load
101 102 103 104 105 |
# File 'lib/pendragon/router.rb', line 101 def prepare! @routes.sort_by!(&:order) @engine = (configuration.enable_compiler? ? Compiler : Recognizer).new(routes) @prepared = true end |
#prepared? ⇒ Boolean
Returns the router is already prepared?.
108 109 110 |
# File 'lib/pendragon/router.rb', line 108 def prepared? !!@prepared end |
#put(path, options = {}, &block) ⇒ Object
81 |
# File 'lib/pendragon/router.rb', line 81 def put(path, = {}, &block); add :put, path, , &block end |
#recognize(request) ⇒ Array
Recognizes the route by request
120 121 122 123 |
# File 'lib/pendragon/router.rb', line 120 def recognize(request) prepare! unless prepared? synchronize { @engine.call(request) } end |
#recognize_path(path_info) ⇒ Array
Recognizes a given path
128 129 130 131 |
# File 'lib/pendragon/router.rb', line 128 def recognize_path(path_info) route, params = recognize(Rack::MockRequest.env_for(path_info)).first [route.name, params.inject({}){|hash, (key, value)| hash[key] = value; hash }] end |
#reset! ⇒ Object
Resets the router’s instance variables
93 94 95 96 97 |
# File 'lib/pendragon/router.rb', line 93 def reset! @routes = [] @current = 0 @prepared = nil end |