Class: NYNY::App

Inherits:
Object
  • Object
show all
Defined in:
lib/nyny/app.rb

Constant Summary collapse

HTTP_VERBS =
[:delete, :get, :head, :options, :patch, :post, :put, :trace]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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_chainObject (readonly)

Returns the value of attribute middleware_chain.



5
6
7
# File 'lib/nyny/app.rb', line 5

def middleware_chain
  @middleware_chain
end

#routerObject (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_hooksObject



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_hooksObject



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

.middlewaresObject



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

.routesObject



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