Created
March 17, 2009 07:03
-
-
Save deepthawtz/80375 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Merb::BootLoader::Dependencies < Merb::BootLoader | |
| # ==== Returns | |
| # Array[Gem::Dependency]:: The dependencies registered in init.rb. | |
| # | |
| # :api: plugin | |
| cattr_accessor :dependencies | |
| self.dependencies = [] | |
| # 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, which register the list of necessary dependencies and any | |
| # after_app_loads hooks. | |
| # | |
| # 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 | |
| def self.run | |
| set_encoding | |
| # this is crucial: load init file with all the preferences | |
| # then environment init file, then start enabling specific | |
| # components, load dependencies and update logger. | |
| unless Merb::disabled?(:initfile) | |
| load_initfile | |
| load_env_config | |
| end | |
| expand_ruby_path | |
| enable_json_gem unless Merb::disabled?(:json) | |
| load_dependencies | |
| update_logger | |
| nil | |
| end | |
| # Load each dependency that has been declared so far. | |
| # | |
| # ==== Returns | |
| # nil | |
| # | |
| # :api: private | |
| def self.load_dependencies | |
| dependencies.each { |dependency| Kernel.load_dependency(dependency, nil) } | |
| nil | |
| end | |
| # Loads json or json_pure and requires it. | |
| # | |
| # ==== Returns | |
| # nil | |
| # | |
| # :api: private | |
| def self.enable_json_gem | |
| gem "json" | |
| require "json/ext" | |
| rescue LoadError | |
| gem "json_pure" | |
| require "json/pure" | |
| end | |
| # Resets the logger and sets the log_stream to Merb::Config[:log_file] | |
| # if one is specified, falling back to STDOUT. | |
| # | |
| # ==== Returns | |
| # nil | |
| # | |
| # :api: private | |
| 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] | |
| raise "log file should be a string, got: #{Merb::Config[:log_file].inspect}" unless Merb::Config[:log_file].is_a?(String) | |
| STDOUT.puts "Logging to file at #{Merb::Config[:log_file]}" unless Merb.testing? | |
| Merb::Config[:log_stream] = File.open(Merb::Config[:log_file], "a") | |
| # but if it's not given, fallback to log stream or stdout | |
| else | |
| Merb::Config[:log_stream] ||= STDOUT | |
| end | |
| nil | |
| end | |
| # Default encoding to UTF8 if it has not already been set to something else. | |
| # | |
| # ==== Returns | |
| # nil | |
| # | |
| # :api: private | |
| def self.set_encoding | |
| unless Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("1.9") | |
| $KCODE = 'UTF8' if $KCODE == 'NONE' || $KCODE.blank? | |
| end | |
| nil | |
| end | |
| private | |
| # Determines the path for the environment configuration file | |
| # | |
| # ==== Returns | |
| # String:: The path to the config file for the environment | |
| # | |
| # :api: private | |
| def self.env_config | |
| Merb.dir_for(:config) / "environments" / (Merb.environment + ".rb") | |
| end | |
| # Checks to see whether or not an environment configuration exists | |
| # | |
| # ==== Returns | |
| # Boolean:: Whether or not the environment configuration file exists. | |
| # | |
| # :api: private | |
| def self.env_config? | |
| Merb.environment && File.exist?(env_config) | |
| end | |
| # Loads the environment configuration file, if it is present | |
| # | |
| # ==== Returns | |
| # nil | |
| # | |
| # :api: private | |
| def self.load_env_config | |
| if env_config? | |
| STDOUT.puts "Loading #{env_config}" unless Merb.testing? | |
| load(env_config) | |
| end | |
| nil | |
| end | |
| # Determines the init file to use, if any. | |
| # By default Merb uses init.rb from application config directory. | |
| # | |
| # ==== Returns | |
| # nil | |
| # | |
| # :api: private | |
| def self.initfile | |
| if Merb::Config[:init_file] | |
| Merb::Config[:init_file].chomp(".rb") + ".rb" | |
| else | |
| Merb.dir_for(:config) / "init.rb" | |
| end | |
| end | |
| # Loads the init file, should one exist | |
| # | |
| # ==== Returns | |
| # nil | |
| # | |
| # :api: private | |
| def self.load_initfile | |
| return nil if Merb.const_defined?("INIT_RB_LOADED") | |
| if File.exists?(initfile) | |
| STDOUT.puts "Loading init file from #{initfile}" 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 | |
| # Expands Ruby path with framework directories (for models, lib, etc). Only run once. | |
| # | |
| # ==== Returns | |
| # nil | |
| # | |
| # :api: private | |
| 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 | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment