class Racket::Application

Racket main application class.

Attributes

registry[R]

Public Class Methods

default() click to toggle source

Initializes a new Racket::Application object with default settings.

@return [Class]

# File lib/racket/application.rb, line 25
def self.default
  new
end
new(settings = {}) click to toggle source

Initializes the Racket application.

@param [Hash] settings @return [Class]

# File lib/racket/application.rb, line 43
def initialize(settings = {})
  @registry = Utils::Application::RegistryBuilder.new(settings).registry
  @registry.handler_stack # Makes sure all plugins and helpers are loaded before any controllers
  load_controllers
end
using(settings) click to toggle source

Initializes a new Racket::Application object with settings specified by settings.

@param [Hash] settings @return [Class]

# File lib/racket/application.rb, line 33
def self.using(settings)
  new(settings)
end

Public Instance Methods

call(env) click to toggle source

Called whenever Rack sends a request to the application.

@param [Hash] env Rack environment @return [Array] A Rack response array

# File lib/racket/application.rb, line 53
def call(env)
  @registry.handler_stack.call(env.dup)
end
dev_mode?() click to toggle source

Returns whether the application runs in dev mode.

@return [true|false]

# File lib/racket/application.rb, line 60
def dev_mode?
  @registry.application_settings.mode == :dev
end
inform_all(message, level = :info) click to toggle source

Sends a message to the logger.

@param [String] message @param [Symbol] level @return nil

# File lib/racket/application.rb, line 69
def inform_all(message, level = :info)
  @registry.application_logger.inform_all(message, level)
end
inform_dev(message, level = :debug) click to toggle source

Sends a message to the logger, but only if the application is running in dev mode.

@param [String] message @param [Symbol] level @return nil

# File lib/racket/application.rb, line 78
def inform_dev(message, level = :debug)
  @registry.application_logger.inform_dev(message, level)
end
kernel_require(*args)
Alias for: require
require(*args) click to toggle source

Requires a file using the current application directory as a base path.

@param [Object] args @return [nil]

# File lib/racket/application.rb, line 88
def require(*args)
  (kernel_require @registry.utils.build_path(*args)) && nil
end
Also aliased as: kernel_require

Private Instance Methods

calculate_url_path(file) click to toggle source

Calculates the url path for the specified (controller) file

@param [Pathname] file @return [String]

# File lib/racket/application.rb, line 98
def calculate_url_path(file)
  controller_dir = @registry.application_settings.controller_dir
  url_path = "/#{file.relative_path_from(controller_dir).dirname}"
  url_path = '' if url_path == '/.'
  url_path
end
controller_files() click to toggle source

Returns a list of relative file paths representing controllers, sorted by path length (longest first).

return [Array]

# File lib/racket/application.rb, line 109
def controller_files
  controller_dir = @registry.application_settings.controller_dir
  glob = File.join('**', '*.rb')
  Utils::FileSystem.matching_paths(controller_dir, glob).map do |path|
    Utils::FileSystem::SizedPath.new(path)
  end.sort.map(&:path)
end
load_controller_file(file) click to toggle source

Loads a controller file.

@param [String] file Relative path from controller dir @return nil

# File lib/racket/application.rb, line 131
def load_controller_file(file)
  kernel_require file
  klass = @registry.application_settings.fetch(:last_added_controller).pop
  # Helpers may do stuff based on route, make sure it is available before applying helpers.
  @registry.router.map(calculate_url_path(file), klass)
  @registry.utils.apply_helpers(klass) && nil
end
load_controller_files() click to toggle source
# File lib/racket/application.rb, line 139
def load_controller_files
  settings = @registry.application_settings
  settings.store(:last_added_controller, [])
  controller_files.each { |path| load_controller_file(path) }
  settings.delete(:last_added_controller)
end
load_controllers() click to toggle source

Loads controllers and associates each controller with a route.

@return [nil]

# File lib/racket/application.rb, line 120
def load_controllers
  inform_dev('Loading controllers.')
  Controller.context = @registry.controller_context
  load_controller_files
  inform_dev('Done loading controllers.') && nil
end