class Racket::Application
Racket
main application class.
Attributes
Public Class Methods
Initializes a new Racket::Application
object with default settings.
@return [Class]
# File lib/racket/application.rb, line 25 def self.default new end
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
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
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
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
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
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
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
Private Instance Methods
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
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
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
# 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
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