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



91
92
93
94
95
96
97
# File 'lib/lennarb/app.rb', line 91

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



100
101
102
# File 'lib/lennarb/app.rb', line 100

def env
  @env
end

#rootObject

The root directory



103
104
105
# File 'lib/lennarb/app.rb', line 103

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?(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



169
170
171
# File 'lib/lennarb/app.rb', line 169

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

#app#call

Get the Rack app with middleware

Returns:

  • (#call)

    The Rack app



118
119
120
121
122
123
124
125
126
127
128
# File 'lib/lennarb/app.rb', line 118

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



161
162
163
# File 'lib/lennarb/app.rb', line 161

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

#call(env) ⇒ Array

Rack interface method

Parameters:

  • env (Hash)

    Rack environment

Returns:

  • (Array)

    Rack response



134
135
136
137
# File 'lib/lennarb/app.rb', line 134

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



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

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



153
154
155
# File 'lib/lennarb/app.rb', line 153

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

#initialize!self

Initialize the app

Returns:

  • (self)

    The initialized app

Raises:



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

def initialize!
  raise AlreadyInitializedError if @initialized

  @initialized = true
  routes.freeze
  self
end

#initialized?Boolean

Check if initialized

Returns:

  • (Boolean)

    true if initialized



216
217
218
# File 'lib/lennarb/app.rb', line 216

def initialized?
  @initialized
end

#middleware { ... } ⇒ MiddlewareStack

Define middleware

Yields:

  • Block to configure middleware

Returns:



143
144
145
146
147
# File 'lib/lennarb/app.rb', line 143

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



177
178
179
180
181
182
183
# File 'lib/lennarb/app.rb', line 177

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

  self.class.routes
end