class Merb::BootLoader::Dependencies

Public Class Methods

enable_json_gem() click to toggle source

Requires json or json_pure.

Returns

nil

:api: private

# File lib/merb-core/bootloader.rb, line 414
def self.enable_json_gem
  require "json"
  rescue LoadError
      Merb.logger.error! "You have enabled JSON but don't have json "                             "installed or don't have dependency in the Gemfile. "                             "Add \"gem 'json', '>= 1.1.7'\" or "                             "\"gem 'json_pure', '>= 1.1.7'\" to your Gemfile."
end
load_dependencies() click to toggle source

Try to load the gem environment file (set via Merb::Config) defaults to ./gems/environment

Load each the dependencies defined in the Merb::Config using the bundler gem's Bundler::require_env

Falls back to rubygems if no bundler environment exists

Returns

nil

:api: private

# File lib/merb-core/bootloader.rb, line 398
def self.load_dependencies
  begin
    Bundler.require(:default, Merb.environment.to_sym)
  rescue Bundler::GemfileNotFound
    Merb.logger.error! "No Gemfile found! If you're generating new app with merb-gen "                           "this is fine, otherwise run: bundle init to create Gemfile"
  end
  nil
end
run() click to toggle source

Load the init_file specified in Merb::Config or if not specified, the init.rb file from the Merb configuration directory, and any environment files and any after_app_loads hooks.

Dependencies are loaded via Bunlder and managed in the Gemfile manifest. By default manifest for Bundler is in the root directory of the app and is called Gemfile. All dependencies MUST be definied there because all dependency hangling was removed from Merb.

Deprecated (1.0.x)

Dependencies can hook into the bootloader process itself by using before or after insertion methods. Since these are loaded from this bootloader (Dependencies), they can only adapt the bootloaders that haven't been loaded up until this point.

Returns

nil

:api: plugin

# File lib/merb-core/bootloader.rb, line 373
def self.run
  set_encoding
  load_dependencies
  unless Merb::disabled?(:initfile)
    load_initfile
    load_env_config
  end
  expand_ruby_path
  enable_json_gem unless Merb::disabled?(:json)
  update_logger
  nil
end
set_encoding() click to toggle source

Default encoding to UTF8 if it has not already been set to something else.

Returns

nil

:api: private

# File lib/merb-core/bootloader.rb, line 458
def self.set_encoding
  unless RUBY_VERSION >= '1.9'
    $KCODE = 'UTF8' if $KCODE == 'NONE' || $KCODE.blank?
  end
  
  nil
end
update_logger() click to toggle source

Resets the logger and sets the log_stream to Merb::Config if one is specified, falling back to STDOUT.

Returns

nil

:api: private

# File lib/merb-core/bootloader.rb, line 430
def self.update_logger
  Merb.reset_logger!

  # If log file is given, use it and not log stream we have.
  if Merb::Config[:log_file]
    log_file = Merb::Config[:log_file]
    raise "log file should be a string, got: #{log_file.inspect}" unless log_file.is_a?(String)
    STDOUT.puts "Logging to file at #{log_file}" unless Merb.testing?
    
    # try to create log directory (if it doesnt exist)
    log_directory = File.dirname(log_file)
    FileUtils.mkdir_p(log_directory) unless File.exists?(log_directory)

    Merb::Config[:log_stream] = File.open(log_file, "a")
  # but if it's not given, fallback to log stream or stdout
  else
    Merb::Config[:log_stream] ||= STDOUT
  end

  nil
end

Private Class Methods

env_config() click to toggle source

Determines the path for the environment configuration file

Returns

String

The path to the config file for the environment

:api: private

# File lib/merb-core/bootloader.rb, line 474
def self.env_config
  Merb.dir_for(:config) / "environments" / (Merb.environment + ".rb")
end
env_config?() click to toggle source

Checks to see whether or not an environment configuration exists

Returns

Boolean

Whether or not the environment configuration file exists.

:api: private

# File lib/merb-core/bootloader.rb, line 484
def self.env_config?
  Merb.environment && File.exist?(env_config)
end
expand_ruby_path() click to toggle source

Expands Ruby path with framework directories (for models, lib, etc). Only run once.

Returns

nil

:api: private

# File lib/merb-core/bootloader.rb, line 545
def self.expand_ruby_path
  # Add models, controllers, helpers and lib to the load path
  unless @ran
    Merb.logger.info "Expanding RUBY_PATH..." if Merb::Config[:verbose]

    $LOAD_PATH.unshift Merb.dir_for(:model)
    $LOAD_PATH.unshift Merb.dir_for(:controller)
    $LOAD_PATH.unshift Merb.dir_for(:lib)
    $LOAD_PATH.unshift Merb.dir_for(:helper)
  end

  @ran = true
  nil
end
initfile() click to toggle source

Determines the init file to use, if any. By default Merb uses init.rb from application config directory.

Returns

nil

:api: private

# File lib/merb-core/bootloader.rb, line 510
def self.initfile
  if Merb::Config[:init_file]
    Merb::Config[:init_file].chomp(".rb") + ".rb"
  else
    Merb.dir_for(:config) / "init.rb"
  end
end
load_env_config() click to toggle source

Loads the environment configuration file, if it is present

Returns

nil

:api: private

# File lib/merb-core/bootloader.rb, line 494
def self.load_env_config
  if env_config?
    env_config_path = relative_to_merb_path(env_config)
    STDOUT.puts "Loading #{env_config_path}" unless Merb.testing?
    load(env_config)
  end
  nil
end
load_initfile() click to toggle source

Loads the init file, should one exist

Returns

nil

:api: private

# File lib/merb-core/bootloader.rb, line 524
def self.load_initfile
  return nil if Merb.const_defined?("INIT_RB_LOADED")
  if File.exists?(initfile)
    initfile_path = relative_to_merb_path(initfile)
    STDOUT.puts "Loading init file from #{initfile_path}" unless Merb.testing?
    load(initfile)
    Merb.const_set("INIT_RB_LOADED", true)
  elsif !Merb.testing?
    Merb.fatal! "You are not in a Merb application, or you are in "            "a flat application and have not specified the init file. If you "            "are trying to create a new merb application, use merb-gen app."
  end
  nil
end
relative_to_merb_path(path) click to toggle source

Converts an absolute path to an path relative to Merbs root, if the path is in the Merb root dir. Otherwise it will return the absolute path.

Returns

String

Relative path or absolute

:api: private

# File lib/merb-core/bootloader.rb, line 568
def self.relative_to_merb_path(path)
  absolute_path = File.expand_path(path)
  merb_root = File.expand_path(Merb.root)
  if absolute_path.slice(0, merb_root.length) == merb_root
    '.' + absolute_path.slice(merb_root.length..-1)
  else
    absolute_path
  end
end