Class: NYNY::App
- Inherits:
-
Object
- Object
- NYNY::App
- Defined in:
- lib/nyny/app.rb
Constant Summary collapse
- HTTP_VERBS =
[:delete, :get, :head, :options, :patch, :post, :put, :trace]
Instance Attribute Summary collapse
-
#middleware_chain ⇒ Object
readonly
Returns the value of attribute middleware_chain.
-
#router ⇒ Object
readonly
Returns the value of attribute router.
Class Method Summary collapse
- .after(&blk) ⇒ Object
- .after_hooks ⇒ Object
- .before(&blk) ⇒ Object
- .before_hooks ⇒ Object
- .helpers(*args) ⇒ Object
- .middlewares ⇒ Object
-
.register(*extensions) ⇒ Object
move middleware chain and runner to core-ext.
- .routes ⇒ Object
- .use(middleware, *args, &block) ⇒ Object
Instance Method Summary collapse
- #_call(env) ⇒ Object
- #call(env) ⇒ Object
-
#initialize(app = nil) ⇒ App
constructor
A new instance of App.
Constructor Details
#initialize(app = nil) ⇒ App
Returns a new instance of App.
6 7 8 9 10 11 12 13 14 15 |
# File 'lib/nyny/app.rb', line 6 def initialize app=nil @router = Router.new({ :routes => self.class.routes, :fallback => (app || lambda {|env| Response.new '', 404 }), :before_hooks => self.class.before_hooks, :after_hooks => self.class.after_hooks }) @middleware_chain = MiddlewareChain.new(self.class.middlewares, lambda {|env| _call(env)}) end |
Instance Attribute Details
#middleware_chain ⇒ Object (readonly)
Returns the value of attribute middleware_chain.
5 6 7 |
# File 'lib/nyny/app.rb', line 5 def middleware_chain @middleware_chain end |
#router ⇒ Object (readonly)
Returns the value of attribute router.
5 6 7 |
# File 'lib/nyny/app.rb', line 5 def router @router end |
Class Method Details
.after(&blk) ⇒ Object
50 51 52 |
# File 'lib/nyny/app.rb', line 50 def after &blk after_hooks << Proc.new(&blk) end |
.after_hooks ⇒ Object
36 |
# File 'lib/nyny/app.rb', line 36 def after_hooks; @after_hooks ||= [] end |
.before(&blk) ⇒ Object
46 47 48 |
# File 'lib/nyny/app.rb', line 46 def before &blk before_hooks << Proc.new(&blk) end |
.before_hooks ⇒ Object
35 |
# File 'lib/nyny/app.rb', line 35 def before_hooks; @before_hooks ||= [] end |
.helpers(*args) ⇒ Object
58 59 60 |
# File 'lib/nyny/app.rb', line 58 def helpers *args args.each {|m| RequestScope.add_helper_module m } end |
.middlewares ⇒ Object
33 |
# File 'lib/nyny/app.rb', line 33 def middlewares; @middlewares ||= [] end |
.register(*extensions) ⇒ Object
move middleware chain and runner to core-ext
39 40 41 42 43 44 |
# File 'lib/nyny/app.rb', line 39 def register *extensions extensions.each do |ext| extend ext ext.registered(self) if ext.respond_to?(:registered) end end |
.routes ⇒ Object
34 |
# File 'lib/nyny/app.rb', line 34 def routes; @routes ||= {} end |
.use(middleware, *args, &block) ⇒ Object
54 55 56 |
# File 'lib/nyny/app.rb', line 54 def use middleware, *args, &block middlewares << [middleware, args, block] end |
Instance Method Details
#_call(env) ⇒ Object
17 18 19 |
# File 'lib/nyny/app.rb', line 17 def _call env router.call env end |
#call(env) ⇒ Object
21 22 23 |
# File 'lib/nyny/app.rb', line 21 def call env middleware_chain.call env end |