Class: Pendragon::Router
- Inherits:
-
Object
- Object
- Pendragon::Router
- Defined in:
- lib/pendragon/router.rb
Class Method Summary collapse
-
.on(event) {|env, method, routes| ... } ⇒ Object
Adds event listener in router class.
-
.register(name) ⇒ Object
Registers new router type onto global maps.
Instance Method Summary collapse
-
#call(env) ⇒ Array<Integer, Hash, #each>, Rack::Response
Calls by given env, returns a response conformed Rack style.
-
#delete(path, to: nil, **options, &block) ⇒ Object
Appends a route of DELETE method.
-
#flat_map ⇒ Array<Pendragon::Route>
Maps all routes.
-
#get(path, to: nil, **options, &block) ⇒ Object
Appends a route of GET method.
-
#head(path, to: nil, **options, &block) ⇒ Object
Appends a route of HEAD method.
-
#initialize { ... } ⇒ Pendragon::Router
constructor
Construcsts an instance of router class.
-
#map ⇒ Hash{String => Array}
Maps all routes for each request methods.
-
#namespace(name) { ... } ⇒ Object
Prefixes a namespace to route path inside given block.
-
#options(path, to: nil, **options, &block) ⇒ Object
Appends a route of OPTIONS method.
-
#post(path, to: nil, **options, &block) ⇒ Object
Appends a route of POST method.
-
#put(path, to: nil, **options, &block) ⇒ Object
Appends a route of PUT method.
-
#route(method, path, to: nil, **options, &block) ⇒ Object
Appends a new route to router.
Constructor Details
#initialize { ... } ⇒ Pendragon::Router
Construcsts an instance of router class.
70 71 72 73 |
# File 'lib/pendragon/router.rb', line 70 def initialize(&block) @compiled = false instance_eval(&block) if block_given? end |
Class Method Details
.on(event) {|env, method, routes| ... } ⇒ Object
Adds event listener in router class.
55 56 57 |
# File 'lib/pendragon/router.rb', line 55 def self.on(event, &listener) define_method('on_%s_listener' % event, &listener) end |
.register(name) ⇒ Object
Registers new router type onto global maps.
25 26 27 |
# File 'lib/pendragon/router.rb', line 25 def self.register(name) Pendragon.register(name, self) end |
Instance Method Details
#call(env) ⇒ Array<Integer, Hash, #each>, Rack::Response
Calls by given env, returns a response conformed Rack style.
109 110 111 |
# File 'lib/pendragon/router.rb', line 109 def call(env) catch(:halt) { with_optimization { invoke(env) } } end |
#delete(path, to: nil, **options, &block) ⇒ Object
Appends a route of DELETE method
217 218 219 |
# File 'lib/pendragon/router.rb', line 217 def delete(path, to: nil, **, &block) route Constants::Http::DELETE, path, to: to, **, &block end |
#flat_map ⇒ Array<Pendragon::Route>
Maps all routes.
253 254 255 |
# File 'lib/pendragon/router.rb', line 253 def flat_map @flat_map ||= [] end |
#get(path, to: nil, **options, &block) ⇒ Object
Appends a route of GET method
199 200 201 |
# File 'lib/pendragon/router.rb', line 199 def get(path, to: nil, **, &block) route Constants::Http::GET, path, to: to, **, &block end |
#head(path, to: nil, **options, &block) ⇒ Object
Appends a route of HEAD method
223 224 225 |
# File 'lib/pendragon/router.rb', line 223 def head(path, to: nil, **, &block) route Constants::Http::HEAD, path, to: to, **, &block end |
#map ⇒ Hash{String => Array}
Maps all routes for each request methods.
247 248 249 |
# File 'lib/pendragon/router.rb', line 247 def map @map ||= Hash.new { |hash, key| hash[key] = [] } end |
#namespace(name) { ... } ⇒ Object
Prefixes a namespace to route path inside given block.
88 89 90 91 92 93 94 |
# File 'lib/pendragon/router.rb', line 88 def namespace(name, &block) fail ArgumentError unless block_given? (self.prefix ||= []) << name.to_s instance_eval(&block) ensure prefix.pop end |
#options(path, to: nil, **options, &block) ⇒ Object
Appends a route of OPTIONS method
229 230 231 |
# File 'lib/pendragon/router.rb', line 229 def (path, to: nil, **, &block) route Constants::Http::OPTIONS, path, to: to, **, &block end |
#post(path, to: nil, **options, &block) ⇒ Object
Appends a route of POST method
205 206 207 |
# File 'lib/pendragon/router.rb', line 205 def post(path, to: nil, **, &block) route Constants::Http::POST, path, to: to, **, &block end |
#put(path, to: nil, **options, &block) ⇒ Object
Appends a route of PUT method
211 212 213 |
# File 'lib/pendragon/router.rb', line 211 def put(path, to: nil, **, &block) route Constants::Http::PUT, path, to: to, **, &block end |
#route(method, path, to: nil, **options, &block) ⇒ Object
Appends a new route to router.
238 239 240 241 242 243 |
# File 'lib/pendragon/router.rb', line 238 def route(method, path, to: nil, **, &block) app = block_given? ? block : to fail ArgumentError, 'Rack application could not be found' unless app path = ?/ + prefix.join(?/) + path if prefix && !prefix.empty? append Route.new(method: method, pattern: path, application: app, **) end |