Class: Lennarb::App

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

Overview

Lite implementation of app.

Direct Known Subclasses

Application

Constant Summary collapse

AlreadyInitializedError =

This error is raised whenever the app is initialized more than once.

Class.new(StandardError)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeApp

Returns a new instance of App.



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

def initialize(&)
  @initialized = false
  self.root = Pathname.pwd
  self.env = compute_env
  instance_eval(&) if block_given?
end

Instance Attribute Details

#envObject

The current environment. Defaults to “development”. It can be set using the following environment variables:

  • ‘LENNA_ENV`

  • ‘APP_ENV`

  • ‘RACK_ENV`

@return



23
24
25
# File 'lib/lennarb/app.rb', line 23

def env
  @env
end

#rootObject

The root app directory of the app.

@return



12
13
14
# File 'lib/lennarb/app.rb', line 12

def root
  @root
end

Instance Method Details

#appObject

The Rack app.



123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/lennarb/app.rb', line 123

def app
  @app ||= begin
    request_handler = RequestHandler.new(self)

    stack = middleware.to_a

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

      run request_handler
    end
  end
end

#call(env) ⇒ Object

Call the app.

@param env



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

def call(env)
  env[RACK_LENNA_APP] = self
  Dir.chdir(root) { return app.call(env) }
end

#config(*envs) ⇒ Object

Define the app’s configuration. See Config.

@return

Examples:

Run config on every environment

app.config do
  mandatory :database_url, string
end

Run config on every a specific environment

app.config :development do
  set :domain, "example.dev"
end

Run config on every a specific environment

app.config :development, :test do
  set :domain, "example.dev"
end


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

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

  write = block_given? &&
    (envs.map(&:to_sym).include?(env.to_sym) || envs.empty?)

  @config.instance_eval(&) if write

  @config
end

#controllersObject Also known as: mounted_apps

Store mounted app’s



139
140
141
# File 'lib/lennarb/app.rb', line 139

def controllers
  @controllers ||= []
end

#freeze!Object

Freeze the app.

@return



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

def freeze!
  app.freeze
  routes.freeze
end

#initialize!Object

Initialize the app.

@return



154
155
156
157
158
159
160
161
162
163
164
# File 'lib/lennarb/app.rb', line 154

def initialize!
  raise AlreadyInitializedError if initialized?

  if controllers.any?
    controllers.each do
      routes.store.merge!(it.routes.store)
    end
  end

  @initialized = true
end

#initialized?Boolean

Check if the app is initialized.

@return

Returns:

  • (Boolean)


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

def initialized? = @initialized

#middlewareObject

Define the app’s middleware stack. See Middleware::Stack for more details.

@return



75
76
77
78
79
# File 'lib/lennarb/app.rb', line 75

def middleware(&)
  @middleware ||= MiddlewareStack.new(self)
  @middleware.instance_eval(&) if block_given?
  @middleware
end

#mount(*controllers) ⇒ Object

Mount an app at a specific path.

@param The controller|app to mount.

@return

MyApp = Lennarb::App.new do
  routes do
    mount PostController
  end

Examples:


class PostController
  extend Lennarb::Routes::Mixin

  get "/post/:id" do |req, res|
    res.text("Post ##{req.params[:id]}")
  end
end


63
64
65
66
67
68
69
# File 'lib/lennarb/app.rb', line 63

def mount(*controllers)
  controllers.each do |controller|
    raise ArgumentError, "Controller must respond to :routes" unless controller.respond_to?(:routes)

    self.controllers << controller
  end
end

#routesObject

Define the app’s route. See RouteNode for more details.

@return



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

def routes(&)
  @routes ||= Routes.new
  @routes.instance_eval(&) if block_given?
  @routes
end