class Praxis::Bootloader

Attributes

application[R]
stages[R]

Public Class Methods

new(application) click to toggle source
# File lib/praxis/bootloader.rb, line 8
def initialize(application)
  @application = application
  @stages = Array.new

  setup_stages!
end

Public Instance Methods

after(*stage_path, &block) click to toggle source
# File lib/praxis/bootloader.rb, line 73
def after(*stage_path, &block)
  stage_name = stage_path.shift
  stages.find { |stage| stage.name == stage_name }.after(*stage_path, &block)
end
before(*stage_path, &block) click to toggle source
# File lib/praxis/bootloader.rb, line 68
def before(*stage_path, &block)
  stage_name = stage_path.shift
  stages.find { |stage| stage.name == stage_name }.before(*stage_path, &block)
end
config() click to toggle source
# File lib/praxis/bootloader.rb, line 15
def config
  @application.config
end
delete_stage(stage_name) click to toggle source
# File lib/praxis/bootloader.rb, line 57
def delete_stage(stage_name)
  if (stage = stages.find { |stage| stage.name == stage_name })
    stages.delete(stage)
  else
    raise Exceptions::StageNotFound.new(
      "Cannot remove stage with name #{stage_name}, stage does not exist."
    )
  end
end
root() click to toggle source
# File lib/praxis/bootloader.rb, line 19
def root
  @application.root
end
run() click to toggle source
# File lib/praxis/bootloader.rb, line 122
def run
  stages.each do |stage|
    stage.setup!
    stage.run
  end
end
setup!() click to toggle source
# File lib/praxis/bootloader.rb, line 114
def setup!
  # use the Stats and Notifications plugins by default
  use Praxis::Stats
  use Praxis::Notifications

  run
end
setup_stages!() click to toggle source
# File lib/praxis/bootloader.rb, line 23
def setup_stages!
  # Require environment first. they define constants and environment specific settings
  stages << BootloaderStages::Environment.new(:environment, application)

  # then setup plugins
  stages << BootloaderStages::PluginLoader.new(:plugins, application)
  
  # then the initializers. as it is their job to ensure monkey patches and other
  # config is in place first.
  stages << BootloaderStages::FileLoader.new(:initializers, application)

  # then require lib/ code.
  stages << BootloaderStages::FileLoader.new(:lib, application)

  # design-specific code.
  stages << BootloaderStages::SubgroupLoader.new(:design, application)

  # app-specific code.
  stages << BootloaderStages::SubgroupLoader.new(:app, application)

  # setup routing
  stages << BootloaderStages::Routing.new(:routing, application)

  # naggy warning about unloaded files
  stages << BootloaderStages::WarnUnloadedFiles.new(:warn_unloaded_files, application)

  after(:app) do
    Praxis::Mapper.finalize!
    Praxis::Blueprint.finalize!
    Praxis::ResourceDefinition.finalize!
  end

end
use(plugin,**options, &block) click to toggle source
# File lib/praxis/bootloader.rb, line 78
def use(plugin,**options, &block)
  if plugin.ancestors.include?(PluginConcern)
    plugin.setup!
    plugin = plugin::Plugin
  end

  instance = if plugin.ancestors.include?(Singleton)
    plugin.instance
  elsif plugin.kind_of?(Class)
    plugin.new
  else
    plugin
  end

  instance.application = application
  instance.options.merge!(options)
  instance.block = block if block_given?

  config_key = if instance.config_key.nil?
    raise "Cannot use plugin: #{plugin}. It does not have a config_key defined, and its class does not have a name" unless instance.class.name
    # Default the config key based on the full class name transformed to snake case (and joining modules with '_')
    instance.class.name.to_s.split('::').collect{|n| n.underscore }.join('_').to_sym
  else
    instance.config_key
  end

  if application.plugins.key?(instance.config_key)
    used_in = application.plugins[config_key].class
    raise "Can not use plugin: #{plugin}, another plugin (#{used_in}) is already registered with key: #{instance.config_key}"
  end

  application.plugins[config_key] = instance

  instance
end