Class: Bowline::Initializer
Instance Attribute Summary collapse
-
#configuration ⇒ Object
readonly
The Configuration instance used by this Initializer instance.
Class Method Summary collapse
-
.run(command = :process, configuration = Configuration.new) {|configuration| ... } ⇒ Object
Runs the initializer.
Instance Method Summary collapse
- #add_plugin_load_paths ⇒ Object
- #after_initialize ⇒ Object
-
#initialize(configuration) ⇒ Initializer
constructor
Create a new Initializer instance that references the given Configuration instance.
- #initialize_database ⇒ Object
-
#initialize_encoding ⇒ Object
For Ruby 1.8, this initialization sets $KCODE to ‘u’ to enable the multibyte safe operations.
- #initialize_framework_logging ⇒ Object
- #initialize_framework_settings ⇒ Object
- #initialize_gems ⇒ Object
- #initialize_js ⇒ Object
- #initialize_logger ⇒ Object
- #initialize_name ⇒ Object
-
#initialize_time_zone ⇒ Object
Sets the default value for Time.zone, and turns on ActiveRecord::Base#time_zone_aware_attributes.
-
#initialize_whiny_nils ⇒ Object
Loads support for “whiny nil” (noisy warnings when methods are invoked on
nil
values) if Configuration#whiny_nils is true. - #load_app_config ⇒ Object
- #load_application_classes ⇒ Object
- #load_application_helpers ⇒ Object
- #load_application_initializers ⇒ Object
- #load_gems ⇒ Object
- #load_plugins ⇒ Object
- #process ⇒ Object
- #require_frameworks ⇒ Object
-
#set_autoload_paths ⇒ Object
Set the paths from which Bowline will automatically load source files, and the load_once paths.
- #set_load_path ⇒ Object
Constructor Details
#initialize(configuration) ⇒ Initializer
Create a new Initializer instance that references the given Configuration instance.
58 59 60 61 |
# File 'lib/bowline/initializer.rb', line 58 def initialize(configuration) @configuration = configuration @loaded_plugins = [] end |
Instance Attribute Details
#configuration ⇒ Object (readonly)
The Configuration instance used by this Initializer instance.
39 40 41 |
# File 'lib/bowline/initializer.rb', line 39 def configuration @configuration end |
Class Method Details
.run(command = :process, configuration = Configuration.new) {|configuration| ... } ⇒ Object
Runs the initializer. By default, this will invoke the #process method, which simply executes all of the initialization routines. Alternately, you can specify explicitly which initialization routine you want:
Bowline::Initializer.run(:set_load_path)
This is useful if you only want the load path initialized, without incurring the overhead of completely loading the entire environment.
49 50 51 52 53 54 |
# File 'lib/bowline/initializer.rb', line 49 def self.run(command = :process, configuration = Configuration.new) yield configuration if block_given? initializer = new configuration initializer.send(command) initializer end |
Instance Method Details
#add_plugin_load_paths ⇒ Object
91 92 93 94 95 96 97 98 99 100 |
# File 'lib/bowline/initializer.rb', line 91 def add_plugin_load_paths Dir.glob(File.join(configuration.plugin_glob, 'lib')).sort.each do |path| $LOAD_PATH << path ActiveSupport::Dependencies.load_paths << path unless configuration.reload_plugins? ActiveSupport::Dependencies.load_once_paths << path end end $LOAD_PATH.uniq! end |
#after_initialize ⇒ Object
211 212 213 214 215 |
# File 'lib/bowline/initializer.rb', line 211 def after_initialize configuration.after_initialize_blocks.each do |block| block.call end end |
#initialize_database ⇒ Object
102 103 104 105 106 |
# File 'lib/bowline/initializer.rb', line 102 def initialize_database if defined?(ActiveRecord) ActiveRecord::Base.establish_connection(configuration.database_configuration) end end |
#initialize_encoding ⇒ Object
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, this does nothing. Specify the default encoding in the Ruby shebang line if you don’t want UTF-8.
242 243 244 |
# File 'lib/bowline/initializer.rb', line 242 def initialize_encoding $KCODE='u' if RUBY_VERSION < '1.9' end |
#initialize_framework_logging ⇒ Object
129 130 131 132 |
# File 'lib/bowline/initializer.rb', line 129 def initialize_framework_logging ActiveRecord::Base.logger ||= Bowline.logger if defined?(ActiveRecord) ActiveSupport::Dependencies.logger ||= Bowline.logger end |
#initialize_framework_settings ⇒ Object
161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/bowline/initializer.rb', line 161 def initialize_framework_settings (configuration.frameworks - [:active_support]).each do |framework| base_class = framework.to_s.camelize.constantize.const_get("Base") settings = configuration.send(framework) next if !settings settings.each do |setting, value| base_class.send("#{setting}=", value) end end configuration.active_support.each do |setting, value| ActiveSupport.send("#{setting}=", value) end end |
#initialize_gems ⇒ Object
175 176 177 178 179 |
# File 'lib/bowline/initializer.rb', line 175 def initialize_gems require 'rubygems' Gem.clear_paths Gem.path.unshift(configuration.gem_path) end |
#initialize_js ⇒ Object
264 265 266 |
# File 'lib/bowline/initializer.rb', line 264 def initialize_js Bowline.js.bowline_loaded end |
#initialize_logger ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/bowline/initializer.rb', line 108 def initialize_logger # if the environment has explicitly defined a logger, use it return if Bowline.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) rescue StandardError => e logger = ActiveSupport::BufferedLogger.new(STDERR) logger.level = ActiveSupport::BufferedLogger::WARN logger.warn( "Bowline 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 "BOWLINE_LOGGER", logger } end |
#initialize_name ⇒ Object
246 247 248 249 250 251 |
# File 'lib/bowline/initializer.rb', line 246 def initialize_name unless configuration.name raise "You must provide an application name in environment.rb" end silence_warnings { Object.const_set "APP_NAME", configuration.name } end |
#initialize_time_zone ⇒ Object
Sets the default value for Time.zone, and turns on ActiveRecord::Base#time_zone_aware_attributes. If assigned value cannot be matched to a TimeZone, an exception will be raised.
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/bowline/initializer.rb', line 142 def initialize_time_zone if configuration.time_zone zone_default = Time.__send__(:get_zone, configuration.time_zone) unless zone_default raise \ 'Value assigned to config.time_zone not recognized.' + 'Run "rake -D time" for a list of tasks for finding appropriate time zone names.' end Time.zone_default = zone_default if defined?(ActiveRecord) ActiveRecord::Base.time_zone_aware_attributes = true ActiveRecord::Base.default_timezone = :utc end end end |
#initialize_whiny_nils ⇒ Object
Loads support for “whiny nil” (noisy warnings when methods are invoked on nil
values) if Configuration#whiny_nils is true.
136 137 138 |
# File 'lib/bowline/initializer.rb', line 136 def initialize_whiny_nils require('active_support/whiny_nil') if configuration.whiny_nils end |
#load_app_config ⇒ Object
253 254 255 256 257 258 259 260 261 262 |
# File 'lib/bowline/initializer.rb', line 253 def load_app_config app_config = configuration.app_config return unless app_config Object.const_set("AppConfig", Class.new { app_config.keys.each do |key| cattr_accessor key send("#{key}=", app_config[key]) end }) end |
#load_application_classes ⇒ Object
224 225 226 227 228 229 230 231 232 233 |
# File 'lib/bowline/initializer.rb', line 224 def load_application_classes if configuration.cache_classes configuration.eager_load_paths.each do |load_path| matcher = /\A#{Regexp.escape(load_path)}(.*)\.rb\Z/ Dir.glob("#{load_path}/**/*.rb").sort.each do |file| require_dependency file.sub(matcher, '\1') end end end end |
#load_application_helpers ⇒ Object
217 218 219 220 221 222 |
# File 'lib/bowline/initializer.rb', line 217 def load_application_helpers helpers = configuration.helpers helpers = helpers.map(&:constantize) helpers.each {|h| Helpers.module_eval { extend h } } Helpers.init end |
#load_application_initializers ⇒ Object
205 206 207 208 209 |
# File 'lib/bowline/initializer.rb', line 205 def load_application_initializers Dir.glob(configuration.initializer_glob).sort.each do |initializer| load(initializer) end end |
#load_gems ⇒ Object
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/bowline/initializer.rb', line 181 def load_gems configuration.gems.each do |dep| = { :lib => dep.name }.merge(dep.) next unless [:lib] begin gem(dep.name, *dep.versions) require([:lib]) rescue LoadError => e puts "was unable to require #{dep.name} as '#{[:lib]}' Reason: #{e.class.name} error raised with message: #{e.}" end end end |
#load_plugins ⇒ Object
198 199 200 201 202 203 |
# File 'lib/bowline/initializer.rb', line 198 def load_plugins Dir.glob(File.join(configuration.plugin_glob, 'init.rb')).sort.each do |path| config = configuration # Need local config variable eval(IO.read(path), binding, path) end end |
#process ⇒ Object
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 |
# File 'lib/bowline/initializer.rb', line 268 def process Bowline.configuration = configuration set_load_path initialize_gems load_gems require_frameworks set_autoload_paths add_plugin_load_paths initialize_encoding initialize_database initialize_logger initialize_framework_logging initialize_whiny_nils initialize_time_zone initialize_framework_settings initialize_name load_app_config load_plugins load_application_initializers after_initialize load_application_classes load_application_helpers initialize_js Bowline.initialized = true end |
#require_frameworks ⇒ Object
63 64 65 |
# File 'lib/bowline/initializer.rb', line 63 def require_frameworks configuration.frameworks.each { |framework| require(framework.to_s) } end |
#set_autoload_paths ⇒ Object
Set the paths from which Bowline will automatically load source files, and the load_once paths.
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/bowline/initializer.rb', line 75 def set_autoload_paths ActiveSupport::Dependencies.load_paths = configuration.load_paths.uniq ActiveSupport::Dependencies.load_once_paths = configuration.load_once_paths.uniq extra = ActiveSupport::Dependencies.load_once_paths - ActiveSupport::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_path ⇒ Object
67 68 69 70 71 |
# File 'lib/bowline/initializer.rb', line 67 def set_load_path load_paths = configuration.load_paths load_paths.reverse_each { |dir| $LOAD_PATH.unshift(dir) if File.directory?(dir) } $LOAD_PATH.uniq! end |