Class: ServiceWorker::Router
- Inherits:
-
Object
- Object
- ServiceWorker::Router
- Defined in:
- lib/serviceworker/router.rb
Constant Summary collapse
- PATH_INFO =
"PATH_INFO".freeze
Instance Attribute Summary collapse
-
#routes ⇒ Object
readonly
Returns the value of attribute routes.
Class Method Summary collapse
Instance Method Summary collapse
- #any? ⇒ Boolean
- #draw(&block) ⇒ Object
- #draw_default ⇒ Object
-
#initialize ⇒ Router
constructor
A new instance of Router.
- #match(path, *args) ⇒ Object (also: #get)
- #match_route(env) ⇒ Object
Constructor Details
#initialize ⇒ Router
Returns a new instance of Router.
12 13 14 15 16 |
# File 'lib/serviceworker/router.rb', line 12 def initialize @routes = [] draw(&Proc.new) if block_given? end |
Instance Attribute Details
#routes ⇒ Object (readonly)
Returns the value of attribute routes.
10 11 12 |
# File 'lib/serviceworker/router.rb', line 10 def routes @routes end |
Class Method Details
.default ⇒ Object
6 7 8 |
# File 'lib/serviceworker/router.rb', line 6 def self.default new.draw_default end |
Instance Method Details
#any? ⇒ Boolean
47 48 49 |
# File 'lib/serviceworker/router.rb', line 47 def any? @routes.any? end |
#draw(&block) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/serviceworker/router.rb', line 18 def draw(&block) return self unless block_given? if block.arity == 1 yield(self) else instance_eval(&block) end self end |
#draw_default ⇒ Object
30 31 32 |
# File 'lib/serviceworker/router.rb', line 30 def draw_default draw { get "/serviceworker.js" } end |
#match(path, *args) ⇒ Object Also known as: get
34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/serviceworker/router.rb', line 34 def match(path, *args) if path.is_a?(Hash) opts = path.to_a path, asset = opts.shift args = [asset, opts.to_h] end Route.new(path, *args).tap do |route| @routes << route end end |
#match_route(env) ⇒ Object
51 52 53 54 55 56 57 |
# File 'lib/serviceworker/router.rb', line 51 def match_route(env) path = env[PATH_INFO] @routes.each do |route| match = route.match(path) and return match end nil end |