Class: Reactive::Initializer
- Inherits:
-
Object
- Object
- Reactive::Initializer
- Defined in:
- lib/initializer.rb
Instance Attribute Summary collapse
-
#configuration ⇒ Object
readonly
Returns the value of attribute configuration.
Class Method Summary collapse
Instance Method Summary collapse
-
#after_initialize ⇒ Object
Fires the user-supplied after_initialize block (Configuration#after_initialize).
-
#initialize(config) ⇒ Initializer
constructor
A new instance of Initializer.
-
#initialize_database ⇒ Object
This initialization routine does nothing unless
:active_record
is one of the frameworks to load (Configuration#frameworks). -
#initialize_dependency_mechanism ⇒ Object
Sets the dependency loading mechanism based on the value of Configuration#cache_classes.
-
#initialize_framework_logging ⇒ Object
Sets the logger for ActiveRecord and ActionMailer (but only for those frameworks that are to be loaded).
-
#initialize_framework_settings(frameworks = configuration.frameworks) ⇒ Object
Initializes framework-specific settings for each of the loaded frameworks (Configuration#frameworks).
-
#initialize_framework_views ⇒ Object
Sets Reactive::View::Base#view_paths and ActionMailer::Base#template_root (but only for those frameworks that are to be loaded).
-
#initialize_logger ⇒ Object
If the
REACTIVE_DEFAULT_LOGGER
constant is already set, this initialization routine does nothing. - #load_application_initializers ⇒ Object
-
#load_environment ⇒ Object
Loads the environment specified by Configuration#environment_path, which is typically one of development, test, or production.
- #load_observers ⇒ Object
- #process ⇒ Object
-
#require_frameworks ⇒ Object
Requires all frameworks specified by the Configuration#frameworks list By default, Controller, View, Dispatcher and ActiveRecord are loaded.
- #require_view_provider ⇒ Object
-
#set_autoload_paths ⇒ Object
Set the paths from which Reactive will automatically load source files, and the load_once paths.
-
#set_load_paths ⇒ Object
Set the
$LOAD_PATH
based on the value of Configuration#load_paths.
Constructor Details
#initialize(config) ⇒ Initializer
Returns a new instance of Initializer.
18 19 20 |
# File 'lib/initializer.rb', line 18 def initialize(config) @configuration = config end |
Instance Attribute Details
#configuration ⇒ Object (readonly)
Returns the value of attribute configuration.
9 10 11 |
# File 'lib/initializer.rb', line 9 def configuration @configuration end |
Class Method Details
.run(command = :process, configuration = Configuration.new) {|configuration| ... } ⇒ Object
11 12 13 14 15 16 |
# File 'lib/initializer.rb', line 11 def self.run(command = :process, configuration = Configuration.new) yield configuration if block_given? initializer = new(configuration) initializer.send(command) initializer end |
Instance Method Details
#after_initialize ⇒ Object
Fires the user-supplied after_initialize block (Configuration#after_initialize)
207 208 209 210 211 |
# File 'lib/initializer.rb', line 207 def after_initialize configuration.after_initialize_blocks.each do |block| block.call end end |
#initialize_database ⇒ Object
This initialization routine does nothing unless :active_record
is one of the frameworks to load (Configuration#frameworks). If it is, this sets the database configuration from Configuration#database_configuration and then establishes the connection.
124 125 126 127 128 129 |
# File 'lib/initializer.rb', line 124 def initialize_database if configuration.frameworks.include?(:active_record) ActiveRecord::Base.configurations = configuration.database_configuration ActiveRecord::Base.establish_connection(configuration.environment) end end |
#initialize_dependency_mechanism ⇒ Object
Sets the dependency loading mechanism based on the value of Configuration#cache_classes.
189 190 191 |
# File 'lib/initializer.rb', line 189 def initialize_dependency_mechanism Dependencies.mechanism = configuration.cache_classes ? :require : :load end |
#initialize_framework_logging ⇒ Object
Sets the logger for ActiveRecord and ActionMailer (but only for those frameworks that are to be loaded). If the framework’s logger is already set, it is not changed, otherwise it is set to use REACTIVE_DEFAULT_LOGGER
.
165 166 167 168 169 |
# File 'lib/initializer.rb', line 165 def initialize_framework_logging for framework in ([:controller, :active_record, :action_mailer ] & configuration.frameworks) framework_constantize(framework).const_get("Base").logger ||= REACTIVE_DEFAULT_LOGGER end end |
#initialize_framework_settings(frameworks = configuration.frameworks) ⇒ Object
Initializes framework-specific settings for each of the loaded frameworks (Configuration#frameworks). The available settings map to the accessors on each of the corresponding Base classes.
196 197 198 199 200 201 202 203 204 |
# File 'lib/initializer.rb', line 196 def initialize_framework_settings(frameworks = configuration.frameworks) (frameworks - [:dispatcher]).each do |framework| base_class = (framework.is_a?(Module) ? framework : framework_constantize(framework)).const_get("Base") configuration.send(framework).each do |setting, value| base_class.send("#{setting}=", value) end end end |
#initialize_framework_views ⇒ Object
Sets Reactive::View::Base#view_paths and ActionMailer::Base#template_root (but only for those frameworks that are to be loaded). If the framework’s paths have already been set, it is not changed, otherwise it is set to use Configuration#view_path.
175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/initializer.rb', line 175 def initialize_framework_views ActionMailer::Base.template_root ||= configuration.view_path if configuration.frameworks.include?(:action_mailer) raise NoViewProvider, "You need to setup a view provider. This is usually done in config/environment.rb with config.view_provider = :your_view_provider" unless @view_provider # initialize_framework_settings([@view_provider]) Controller::Base.template_class = @view_provider::Base.template_class Controller::Base.view_paths = View::Base.view_paths = [configuration.view_path] if View::Base.view_paths.empty? Controller::Base.class_eval { helper :application } View::Base.asset_paths = [configuration.asset_path] if View::Base.asset_paths.empty? end |
#initialize_logger ⇒ Object
If the REACTIVE_DEFAULT_LOGGER
constant is already set, this initialization routine does nothing. If the constant is not set, and Configuration#logger is not nil
, this also does nothing. Otherwise, a new logger instance is created at Configuration#log_path, with a default log level of Configuration#log_level.
If the log could not be created, the log will be set to output to STDERR
, with a log level of WARN
.
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/initializer.rb', line 139 def initialize_logger # if the environment has explicitly defined a logger, use it return if defined?(REACTIVE_DEFAULT_LOGGER) unless logger = configuration.logger begin logger = ActiveSupport::BufferedLogger.new(configuration.log_path) logger.level = ActiveSupport::BufferedLogger.const_get(configuration.log_level.to_s.upcase) logger.auto_flushing = false if configuration.environment == "production" rescue StandardError =>e logger = ActiveSupport::BufferedLogger.new(STDERR) logger.level = ActiveSupport::BufferedLogger::WARN logger.warn( "Reactive Error: Unable to access log file. Please ensure that #{configuration.log_path} exists and is chmod 0666. " + "The log level has been raised to WARN and the output directed to STDERR until the problem is fixed." ) end end silence_warnings { Object.const_set "REACTIVE_DEFAULT_LOGGER", logger } end |
#load_application_initializers ⇒ Object
213 214 215 216 217 |
# File 'lib/initializer.rb', line 213 def load_application_initializers Dir["#{configuration.root_path}/config/initializers/**/*.rb"].sort.each do |initializer| load(initializer) end end |
#load_environment ⇒ Object
Loads the environment specified by Configuration#environment_path, which is typically one of development, test, or production.
97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/initializer.rb', line 97 def load_environment return if @environment_loaded @environment_loaded = true config = configuration constants = self.class.constants eval(IO.read(configuration.global_environment_path), binding, configuration.global_environment_path) if File.exists?(configuration.global_environment_path) eval(IO.read(configuration.environment_path), binding, configuration.environment_path) if File.exists?(configuration.environment_path) (self.class.constants - constants).each do |const| Object.const_set(const, self.class.const_get(const)) end end |
#load_observers ⇒ Object
112 113 114 115 116 |
# File 'lib/initializer.rb', line 112 def load_observers if configuration.frameworks.include?(:active_record) ActiveRecord::Base.instantiate_observers end end |
#process ⇒ Object
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 |
# File 'lib/initializer.rb', line 22 def process set_load_paths set_autoload_paths load_environment require_frameworks require_view_provider #load_or_require_plugins #initialize_encoding initialize_database initialize_logger initialize_framework_logging initialize_framework_views initialize_dependency_mechanism #initialize_whiny_nils initialize_framework_settings after_initialize load_application_initializers # Observers are loaded after plugins in case Observers or observed models are modified by plugins. load_observers end |
#require_frameworks ⇒ Object
Requires all frameworks specified by the Configuration#frameworks list By default, Controller, View, Dispatcher and ActiveRecord are loaded.
76 77 78 |
# File 'lib/initializer.rb', line 76 def require_frameworks configuration.frameworks.each { |framework| require(framework.to_s) } end |
#require_view_provider ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/initializer.rb', line 80 def require_view_provider provider = configuration.view_provider raise NoViewProvider, "You need to setup a view provider. This is usually done in config/environment.rb with config.view_provider = :your_view_provider" unless provider if provider.is_a?(Class) @view_provider = provider else provider_name = provider.to_s long_name = (provider_name =~ /^reactive_view_/i ? provider_name : "reactive_view_#{provider_name}") short_name = /^reactive_view_(.+)/i.match(long_name)[1] require long_name @view_provider = "Reactive::View::#{short_name.camelize}".constantize end end |
#set_autoload_paths ⇒ Object
Set the paths from which Reactive will automatically load source files, and the load_once paths.
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/initializer.rb', line 58 def set_autoload_paths Dependencies.load_paths = configuration.load_paths.uniq Dependencies.load_once_paths = configuration.load_once_paths.uniq extra = Dependencies.load_once_paths - Dependencies.load_paths unless extra.empty? abort <<-end_error load_once_paths must be a subset of the load_paths. Extra items in load_once_paths: #{extra * ','} end_error end # Freeze the arrays so future modifications will fail rather than do nothing mysteriously configuration.load_once_paths.freeze end |
#set_load_paths ⇒ Object
Set the $LOAD_PATH
based on the value of Configuration#load_paths. Duplicates are removed.
50 51 52 53 54 |
# File 'lib/initializer.rb', line 50 def set_load_paths load_paths = configuration.load_paths + configuration.framework_paths load_paths.reverse_each { |dir| $LOAD_PATH.unshift(dir) if File.directory?(dir) } $LOAD_PATH.uniq! end |