Class: Pakyow::Application

Inherits:
Object
  • Object
show all
Includes:
Behavior::Aspects, Behavior::Endpoints, Behavior::Frameworks, Behavior::Helpers, Behavior::Initializers, Behavior::Isolating, Behavior::Operations, Behavior::Pipeline, Behavior::Plugins, Behavior::Rescuing, Behavior::Restarting, Behavior::Sessions, Config, Support::Configurable, Support::Definable, Support::Hookable, Support::Inspectable, Support::Pipeline
Defined in:
lib/pakyow/application.rb,
lib/pakyow/application/config.rb,
lib/pakyow/application/connection.rb,
lib/pakyow/application/helpers/app.rb,
lib/pakyow/application/behavior/aspects.rb,
lib/pakyow/application/behavior/helpers.rb,
lib/pakyow/application/behavior/plugins.rb,
lib/pakyow/application/behavior/pipeline.rb,
lib/pakyow/application/behavior/rescuing.rb,
lib/pakyow/application/behavior/sessions.rb,
lib/pakyow/application/behavior/endpoints.rb,
lib/pakyow/application/behavior/isolating.rb,
lib/pakyow/application/helpers/connection.rb,
lib/pakyow/application/behavior/frameworks.rb,
lib/pakyow/application/behavior/operations.rb,
lib/pakyow/application/behavior/restarting.rb,
lib/pakyow/application/behavior/initializers.rb,
lib/pakyow/application/connection/session/base.rb,
lib/pakyow/application/connection/session/cookie.rb,
lib/pakyow/application/connection/behavior/values.rb,
lib/pakyow/application/connection/behavior/session.rb,
lib/pakyow/application/connection/behavior/verifier.rb

Overview

Pakyow’s main application object. Can be defined directly or subclassed to create multiple application objects, each containing its own state. These applications can then be mounted as an endpoint within the environment.

Pakyow::Application.define do
  # state shared between all apps goes here
end

class API < Pakyow::Application
  # state for this app goes here
end

Pakyow.configure do
  mount API, at: "/api"
end

Pipeline

Requests are received by #call, creating a Connection object that provides an interface to the underlying request state. The connection is pushed through a pipeline. Each pipeline action can modify the connection and then either 1) allow the connection to hit the next action 2) halt pipeline execution completely.

Once the connection exits the pipeline a response is returned. If an action halted, the connection is finalized and returned, otherwise app assumes that the connection was unhandled and returns a canned 404 response.

Application also catches any unhandled errors that occur in the pipeline by simply logging the error and returning a canned 500 response.

Configuration

Application objects can be configured.

Pakyow::Application.configure do
  config.name = "my-app"
end

It’s possible to configure for certain environments.

Pakyow::Application.configure :development do
  config.name = "my-dev-app"
end

The app config namespace can be extended with your own custom options.

Pakyow::Application.configure do
  config.foo = "bar"
end

Hooks

Hooks can be defined for the following events:

- initialize
- configure
- load
- finalize
- boot
- rescue
- shutdown

Here’s how to log a message after initialize:

Pakyow::Application.after "initialize" do
  logger.info "initialized #{self}"
end

See Also:

  • Support::Pipeline
  • Support::Configurable
  • Support::Hookable

Defined Under Namespace

Modules: Behavior, Config, Helpers Classes: Connection

Instance Attribute Summary collapse

Attributes included from Behavior::Plugins

#plugs

Attributes included from Behavior::Rescuing

#rescued

Attributes included from Behavior::Endpoints

#endpoints

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Behavior::Plugins

#plug

Methods included from Behavior::Isolating

#isolated

Methods included from Behavior::Restarting

#touch_restart

Methods included from Behavior::Rescuing

#rescued?

Constructor Details

#initialize(environment, mount_path: "/", &block) ⇒ Application

Returns a new instance of Application.



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/pakyow/application.rb', line 148

def initialize(environment, mount_path: "/", &block)
  super()

  @environment, @mount_path, @rescued = environment, mount_path, false

  performing :initialize do
    performing :configure do
      configure!(environment)
    end

    performing :load do
      $LOAD_PATH.unshift(config.lib)
    end

    config.version = Support::PathVersion.build(config.src)

    # Call the Pakyow::Definable initializer.
    #
    # This ensures that any state registered in the passed block
    # has the proper priority against instance and global state.
    #
    defined!(&block)
  end
rescue ScriptError, StandardError => error
  rescue!(error)
end

Instance Attribute Details

#environmentObject (readonly)

Environment the app is booted in, e.g. :development.



118
119
120
# File 'lib/pakyow/application.rb', line 118

def environment
  @environment
end

#mount_pathObject (readonly)

Application mount path.



122
123
124
# File 'lib/pakyow/application.rb', line 122

def mount_path
  @mount_path
end

Class Method Details

._load(state) ⇒ Object



210
211
212
# File 'lib/pakyow/application.rb', line 210

def self._load(state)
  Pakyow.app(Marshal.load(state)[:name])
end

.inherited(subclass) ⇒ Object



107
108
109
110
111
112
113
# File 'lib/pakyow/application.rb', line 107

def inherited(subclass)
  super

  # Creates a connection subclass that other frameworks can safely extend.
  #
  subclass.isolate Connection
end

Instance Method Details

#_dump(_) ⇒ Object



202
203
204
205
206
207
208
# File 'lib/pakyow/application.rb', line 202

def _dump(_)
  Marshal.dump(
    {
      name: config.name
    }
  )
end

#bootedObject

Called by the environment after it boots the app.



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

def booted
  unless rescued?
    call_hooks :after, :boot
  end
rescue ScriptError, StandardError => error
  rescue!(error)
end

#call(connection) ⇒ Object

Calls the app pipeline with a connection created from the rack env.



187
188
189
190
191
192
193
194
195
196
# File 'lib/pakyow/application.rb', line 187

def call(connection)
  app_connection = isolated(:Connection).new(self, connection)
  super(app_connection)
rescue => error
  if app_connection && respond_to?(:controller_for_connection) && controller = controller_for_connection(app_connection)
    controller.handle_error(error)
  else
    raise error
  end
end

#perform(app_connection) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



220
221
222
# File 'lib/pakyow/application.rb', line 220

def perform(app_connection)
  @__pipeline.call(app_connection)
end

#shutdownObject



198
199
200
# File 'lib/pakyow/application.rb', line 198

def shutdown
  performing :shutdown do; end
end

#topObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



215
216
217
# File 'lib/pakyow/application.rb', line 215

def top
  self
end