class Pakyow::Application

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 {Application#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.

@see Support::Pipeline

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

@see Support::Configurable

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 Support::Hookable

Attributes

environment[R]

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

mount_path[R]

Application mount path.

Public Class Methods

_load(state) click to toggle source
# File lib/pakyow/application.rb, line 210
def self._load(state)
  Pakyow.app(Marshal.load(state)[:name])
end
inherited(subclass) click to toggle source
Calls superclass method
# 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
new(environment, mount_path: "/", &block) click to toggle source
# 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

Public Instance Methods

_dump(_) click to toggle source
# File lib/pakyow/application.rb, line 202
def _dump(_)
  Marshal.dump(
    {
      name: config.name
    }
  )
end
booted() click to toggle source

Called by the environment after it boots the app.

# 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) click to toggle source

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

Calls superclass method
# 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) click to toggle source

@api private

# File lib/pakyow/application.rb, line 220
def perform(app_connection)
  @__pipeline.call(app_connection)
end
shutdown() click to toggle source
# File lib/pakyow/application.rb, line 198
def shutdown
  performing :shutdown do; end
end
top() click to toggle source

@api private

# File lib/pakyow/application.rb, line 215
def top
  self
end