aboutsummaryrefslogtreecommitdiffstats
path: root/railties/lib/rails/initializable.rb
blob: 61c98d4f99bcc46b59cfb549dc22718df5d0bddc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
module Rails
  module Initializable

    # A collection of initializers
    class Collection < ActiveSupport::OrderedHash
      # def initialize_copy(other)
      #   super
      #   each do |key, value|
      #     self[key] = value.dup
      #   end
      # end

      def run
        each do |key, initializer|
          initializer.run
        end
        self
      end
    end

    class Initializer
      attr_reader :name, :options, :block

      def initialize(name, options = {}, &block)
        @name, @options, @block = name, options, block
      end

      def run
        return if @already_ran
        @block.call
        @already_ran = true
      end
    end

    def initializer(name, options = {}, &block)
      initializers[name] = Initializer.new(name, options, &block)
    end

    def initializers
      @initializers ||= Collection.new
    end

    def initializers=(initializers)
      @initializers = initializers
    end

  end

  extend Initializable

  # Check for valid Ruby version (1.8.2 or 1.8.4 or higher). This is done in an
  # external file, so we can use it from the `rails` program as well without duplication.
  initializer :check_ruby_version do
    require 'rails/ruby_version_check'
  end

  # For Ruby 1.8, this initialization sets $KCODE to 'u' to enable the
  # multibyte safe operations. Plugin authors supporting other encodings
  # should override this behaviour and set the relevant +default_charset+
  # on ActionController::Base.
  #
  # For Ruby 1.9, UTF-8 is the default internal and external encoding.
  initializer :initialize_encoding do
    if RUBY_VERSION < '1.9'
      $KCODE='u'
    else
      Encoding.default_external = Encoding::UTF_8
    end
  end
end