aboutsummaryrefslogtreecommitdiffstats
path: root/railties/lib/rails/initializable.rb
diff options
context:
space:
mode:
authorCarl Lerche <carllerche@mac.com>2009-10-07 15:21:19 -0700
committerCarl Lerche <carllerche@mac.com>2009-10-08 12:31:09 -0700
commit6d6ae0841c94d3c0ce0c91311028ff7396c44a4a (patch)
tree14030762454f1afffa747fc6202498353c0337d9 /railties/lib/rails/initializable.rb
parentd0965892456e0ec76f9cb95a151d3b8e11622e36 (diff)
downloadrails-6d6ae0841c94d3c0ce0c91311028ff7396c44a4a.tar.gz
rails-6d6ae0841c94d3c0ce0c91311028ff7396c44a4a.tar.bz2
rails-6d6ae0841c94d3c0ce0c91311028ff7396c44a4a.zip
Start moving the initializers into the application object
Diffstat (limited to 'railties/lib/rails/initializable.rb')
-rw-r--r--railties/lib/rails/initializable.rb70
1 files changed, 70 insertions, 0 deletions
diff --git a/railties/lib/rails/initializable.rb b/railties/lib/rails/initializable.rb
new file mode 100644
index 0000000000..61c98d4f99
--- /dev/null
+++ b/railties/lib/rails/initializable.rb
@@ -0,0 +1,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 \ No newline at end of file