Class: Merb::BootLoader::Dependencies

Inherits:
Merb::BootLoader show all
Defined in:
lib/merb-core/bootloader.rb

Class Method Summary collapse

Methods inherited from Merb::BootLoader

after, after_app_loads, before, before_app_loads, before_master_shutdown, before_worker_shutdown, default_framework, finished?, inherited, move_klass

Class Method Details

.enable_json_gemObject

Requires json or json_pure.

Returns

nil

:api: private



414
415
416
417
418
419
420
421
# 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_dependenciesObject

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



398
399
400
401
402
403
404
405
406
# 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

.runObject

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



373
374
375
376
377
378
379
380
381
382
383
384
# 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_encodingObject

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

Returns

nil

:api: private



458
459
460
461
462
463
464
# 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_loggerObject

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

Returns

nil

:api: private



430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
# 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