Class: NYNY::Base

Inherits:
Object
  • Object
show all
Includes:
Inheritable
Defined in:
lib/nyny/base.rb

Direct Known Subclasses

App

Constant Summary collapse

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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Inheritable

included

Constructor Details

#initialize(app = nil) ⇒ Base

Returns a new instance of Base.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/nyny/base.rb', line 21

def initialize app=nil
  self.class.before_init_hooks.each {|h| h.call(self)}

  builder = initialize_builder
  builder.run Router.new({
    :scope_class    => self.class.scope_class,
    :route_defs     => self.class.route_defs,
    :before_hooks   => self.class.before_hooks,
    :after_hooks    => self.class.after_hooks,
    :fallback       => app
  })

  @app = builder.to_app
  self.class.after_init_hooks.each {|h| h.call(self, @app)}
end

Class Method Details

.after(&blk) ⇒ Object



88
89
90
# File 'lib/nyny/base.rb', line 88

def after &blk
  after_hooks << Proc.new(&blk)
end

.after_initialize(&blk) ⇒ Object



96
97
98
# File 'lib/nyny/base.rb', line 96

def after_initialize &blk
  after_init_hooks << Proc.new(&blk)
end

.before(&blk) ⇒ Object



84
85
86
# File 'lib/nyny/base.rb', line 84

def before &blk
  before_hooks << Proc.new(&blk)
end

.before_initialize(&blk) ⇒ Object



92
93
94
# File 'lib/nyny/base.rb', line 92

def before_initialize &blk
  before_init_hooks << Proc.new(&blk)
end

.constraints(args, &block) ⇒ Object



77
78
79
80
81
82
# File 'lib/nyny/base.rb', line 77

def constraints args, &block
  current = self.default_constraints.dup
  self.default_constraints = args
  instance_eval &block
  self.default_constraints = current
end

.define_route(path, options, &block) ⇒ Object



73
74
75
# File 'lib/nyny/base.rb', line 73

def define_route path, options, &block
  self.route_defs << [path, options, Proc.new(&block)]
end

.helpers(*args, &block) ⇒ Object



111
112
113
114
# File 'lib/nyny/base.rb', line 111

def helpers *args, &block
  args << Module.new(&block) if block_given?
  args.each {|m| scope_class.send :include, m }
end

.map(url, &block) ⇒ Object



58
59
60
# File 'lib/nyny/base.rb', line 58

def map url, &block
  url_map[url] = block
end

.namespace(url, app = nil, &block) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'lib/nyny/base.rb', line 62

def namespace url, app=nil, &block
  scope  = self.scope_class

  app ||= Class.new self.superclass do
    self.scope_class = scope
    class_eval(&block)
  end

  map (url) { use app }
end

.register(*extensions) ⇒ Object



104
105
106
107
108
109
# File 'lib/nyny/base.rb', line 104

def register *extensions
  extensions.each do |ext|
    extend ext
    ext.registered(self) if ext.respond_to?(:registered)
  end
end

.use(middleware, *args, &block) ⇒ Object



100
101
102
# File 'lib/nyny/base.rb', line 100

def use middleware, *args, &block
  middlewares << [middleware, args, block]
end

Instance Method Details

#call(env) ⇒ Object



44
45
46
# File 'lib/nyny/base.rb', line 44

def call env
  @app.call env
end

#initialize_builderObject



37
38
39
40
41
42
# File 'lib/nyny/base.rb', line 37

def initialize_builder
  builder = Rack::Builder.new
  self.class.middlewares.each { |m, args, blk| builder.use m, *args, &blk }
  self.class.url_map.each {|url, block| builder.map(url, &block) }
  builder
end