Class: Lennarb::App

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

Overview

Main application class with hooks and helpers support

Constant Summary collapse

AlreadyInitializedError =

Error for already initialized app

Class.new(StandardError)

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|self| ... } ⇒ App

Initialize a new app

Yields:

  • (self)

    Configuration block



103
104
105
106
107
108
109
# File 'lib/lennarb/app.rb', line 103

def initialize(&block)
  @initialized = false
  @root = Pathname.pwd
  @env = Environment.new(compute_env)

  instance_eval(&block) if block_given?
end

Class Attribute Details

.appString

Rack environment variable name

Returns:

  • (String)

    The environment variable name



9
10
11
# File 'lib/lennarb/app.rb', line 9

def app
  @app ||= self
end

Instance Attribute Details

#envObject

The current environment



112
113
114
# File 'lib/lennarb/app.rb', line 112

def env
  @env
end

#rootObject

The root directory



115
116
117
# File 'lib/lennarb/app.rb', line 115

def root
  @root
end

Class Method Details

.after {|req, res| ... } ⇒ Array

Define an after hook

Yields:

  • (req, res)

    Block to execute after route

Returns:

  • (Array)

    The after hooks array



33
34
35
# File 'lib/lennarb/app.rb', line 33

def after(&block)
  Hooks.add(self, :after, &block)
end

.before {|req, res| ... } ⇒ Array

Define a before hook

Yields:

  • (req, res)

    Block to execute before route

Returns:

  • (Array)

    The before hooks array



25
26
27
# File 'lib/lennarb/app.rb', line 25

def before(&block)
  Hooks.add(self, :before, &block)
end

.config(*envs) { ... } ⇒ Config

Define configuration

Parameters:

  • envs (Array<Symbol>)

    Environments

Yields:

  • Configuration block

Returns:

  • (Config)

    The config instance



74
75
76
77
78
79
80
81
82
83
# File 'lib/lennarb/app.rb', line 74

def config(*envs, &)
  @config ||= Config.new(self)

  if block_given?
    write = envs.empty? || envs.map(&:to_sym).include?(compute_env_name)
    @config.instance_eval(&) if write
  end

  @config
end

.helpers(mod_or_block = nil) { ... } ⇒ Module

Define helper methods for the application

Yields:

  • Block containing helper definitions

Returns:

  • (Module)

    The helpers module



17
18
19
# File 'lib/lennarb/app.rb', line 17

def helpers(mod_or_block = nil, &block)
  Helpers.define(self, mod_or_block, &block)
end

.inherited(subclass) ⇒ void

This method returns an undefined value.

Set up subclass

Parameters:

  • subclass (Class)

    The new subclass



48
49
50
51
52
# File 'lib/lennarb/app.rb', line 48

def inherited(subclass)
  super
  # Each subclass gets its own routes
  subclass.instance_variable_set(:@routes, Routes.new)
end

.root {|req, res, params| ... } ⇒ void

This method returns an undefined value.

Define root route (GET /)

Yields:

  • (req, res, params)

    Route block



65
66
67
# File 'lib/lennarb/app.rb', line 65

def root(&block)
  get("/", &block)
end

.routesRoutes

Get routes for this app class

Returns:

  • (Routes)

    The routes instance



40
41
42
# File 'lib/lennarb/app.rb', line 40

def routes
  @routes ||= Routes.new
end

Instance Method Details

#after {|req, res| ... } ⇒ Array

Define after hook (instance method)

Yields:

  • (req, res)

    After hook block

Returns:

  • (Array)

    The after hooks array



181
182
183
# File 'lib/lennarb/app.rb', line 181

def after(&block)
  self.class.after(&block)
end

#app#call

Get the Rack app with middleware

Returns:

  • (#call)

    The Rack app



130
131
132
133
134
135
136
137
138
139
140
# File 'lib/lennarb/app.rb', line 130

def app
  @app ||= begin
    handler = build_request_handler
    stack = middleware.to_a

    Rack::Builder.app do
      stack.each { |middleware, args, block| use(middleware, *args, &block) }
      run handler
    end
  end
end

#before {|req, res| ... } ⇒ Array

Define before hook (instance method)

Yields:

  • (req, res)

    Before hook block

Returns:

  • (Array)

    The before hooks array



173
174
175
# File 'lib/lennarb/app.rb', line 173

def before(&block)
  self.class.before(&block)
end

#call(env) ⇒ Array

Rack interface method

Parameters:

  • env (Hash)

    Rack environment

Returns:

  • (Array)

    Rack response



146
147
148
149
# File 'lib/lennarb/app.rb', line 146

def call(env)
  env[RACK_LENNA_APP] = self
  app.call(env)
end

#config(*envs) { ... } ⇒ Config

Get/define configuration

Parameters:

  • envs (Array<Symbol>)

    Environments

Yields:

  • Configuration block

Returns:

  • (Config)

    The config instance



204
205
206
207
208
209
210
211
212
213
# File 'lib/lennarb/app.rb', line 204

def config(*envs, &)
  @config ||= self.class.config

  if block_given?
    write = envs.empty? || envs.map(&:to_sym).include?(env.name)
    @config.instance_eval(&) if write
  end

  @config
end

#helpers { ... } ⇒ Module

Define helpers (instance method)

Yields:

  • Block with helper definitions

Returns:

  • (Module)

    The helpers module



165
166
167
# File 'lib/lennarb/app.rb', line 165

def helpers(&block)
  self.class.helpers(&block)
end

#initialize!self

Initialize the app

Returns:

  • (self)

    The initialized app

Raises:



219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/lennarb/app.rb', line 219

def initialize!
  raise AlreadyInitializedError if @initialized

  warn_about_defaulted_env

  # Snapshot the class routes into this instance and freeze only the copy,
  # so booting an app never freezes process-global class state.
  @routes = Routes.new
  @routes.merge!(self.class.routes)
  @routes.freeze

  @initialized = true
  self
end

#initialized?Boolean

Check if initialized

Returns:

  • (Boolean)

    true if initialized



237
238
239
# File 'lib/lennarb/app.rb', line 237

def initialized?
  @initialized
end

#middleware { ... } ⇒ MiddlewareStack

Define middleware

Yields:

  • Block to configure middleware

Returns:



155
156
157
158
159
# File 'lib/lennarb/app.rb', line 155

def middleware(&block)
  @middleware_stack ||= default_middleware_stack
  @middleware_stack.instance_eval(&block) if block_given?
  @middleware_stack
end

#routes { ... } ⇒ Routes

Get/define routes

Yields:

  • Block to define routes

Returns:

  • (Routes)

    The routes instance



189
190
191
192
193
194
195
196
197
# File 'lib/lennarb/app.rb', line 189

def routes(&block)
  if block_given?
    self.class.instance_exec(&block)
  end

  # Before boot, route definitions go to the class. After boot, this
  # instance serves from its own frozen snapshot.
  @routes || self.class.routes
end