rails.rb 2.6 KB
Newer Older
1 2
require 'rails/ruby_version_check'

3
require 'pathname'
4 5 6

require 'active_support'
require 'active_support/core_ext/kernel/reporting'
J
José Valim 已提交
7
require 'active_support/core_ext/array/extract_options'
8

9
require 'rails/application'
10 11
require 'rails/version'

12
require 'active_support/railtie'
13 14
require 'action_dispatch/railtie'

15
# For Ruby 1.9, UTF-8 is the default internal and external encoding.
J
José Valim 已提交
16 17 18
silence_warnings do
  Encoding.default_external = Encoding::UTF_8
  Encoding.default_internal = Encoding::UTF_8
19 20 21
end

module Rails
J
Joshua Peek 已提交
22 23 24
  autoload :Info, 'rails/info'
  autoload :InfoController, 'rails/info_controller'

25 26 27 28 29 30 31 32 33 34 35
  class << self
    def application
      @@application ||= nil
    end

    def application=(application)
      @@application = application
    end

    # The Configuration instance used to configure the Rails environment
    def configuration
36
      application.config
37 38 39 40 41 42 43
    end

    def initialize!
      application.initialize!
    end

    def initialized?
44
      @@initialized || false
45 46 47
    end

    def initialized=(initialized)
48
      @@initialized ||= initialized
49 50 51
    end

    def logger
52 53 54 55 56
      @@logger ||= nil
    end

    def logger=(logger)
      @@logger = logger
57 58 59 60
    end

    def backtrace_cleaner
      @@backtrace_cleaner ||= begin
61
        # Relies on Active Support, so we have to lazy load to postpone definition until AS has been loaded
62 63 64 65 66 67 68 69 70 71
        require 'rails/backtrace_cleaner'
        Rails::BacktraceCleaner.new
      end
    end

    def root
      application && application.config.root
    end

    def env
72
      @_env ||= ActiveSupport::StringInquirer.new(ENV["RAILS_ENV"] || ENV["RACK_ENV"] || "development")
73 74
    end

75 76 77 78
    def env=(environment)
      @_env = ActiveSupport::StringInquirer.new(environment)
    end

79 80 81 82
    def cache
      RAILS_CACHE
    end

83 84 85 86
    # Returns all rails groups for loading based on:
    #
    # * The Rails environment;
    # * The environment variable RAILS_GROUPS;
J
José Valim 已提交
87
    # * The optional envs given as argument and the hash with group dependencies;
88 89 90 91 92 93 94 95 96
    #
    # == Examples
    #
    #   groups :assets => [:development, :test]
    #
    #   # Returns
    #   # => [:default, :development, :assets] for Rails.env == "development"
    #   # => [:default, :production]           for Rails.env == "production"
    #
J
José Valim 已提交
97 98
    def groups(*groups)
      hash = groups.extract_options!
99
      env = Rails.env
J
José Valim 已提交
100
      groups.unshift(:default, env)
101 102 103
      groups.concat ENV["RAILS_GROUPS"].to_s.split(",")
      groups.concat hash.map { |k,v| k if v.map(&:to_s).include?(env) }
      groups.compact!
J
José Valim 已提交
104
      groups.uniq!
105 106 107
      groups
    end

108 109 110 111 112
    def version
      VERSION::STRING
    end

    def public_path
113
      application && application.paths["public"].first
114
    end
115
  end
116
end