Class: Bowline::Initializer

Inherits:
Object show all
Defined in:
lib/bowline/initializer.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration) ⇒ Initializer

Create a new Initializer instance that references the given Configuration instance.



77
78
79
80
# File 'lib/bowline/initializer.rb', line 77

def initialize(configuration)
  @configuration = configuration
  @loaded_plugins = []
end

Instance Attribute Details

#configurationObject (readonly)

The Configuration instance used by this Initializer instance.



58
59
60
# File 'lib/bowline/initializer.rb', line 58

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.

Yields:



68
69
70
71
72
73
# File 'lib/bowline/initializer.rb', line 68

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_pathsObject



123
124
125
126
127
128
129
130
131
132
# File 'lib/bowline/initializer.rb', line 123

def add_plugin_load_paths
  Dir.glob(File.join(configuration.plugin_glob, 'lib')).sort.each do |path|
    $LOAD_PATH << path
    ActiveSupport::Dependencies.autoload_paths << path
    unless configuration.reload_plugins?
      ActiveSupport::Dependencies.autoload_once_paths << path
    end
  end
  $LOAD_PATH.uniq!
end

#after_initializeObject



247
248
249
250
251
# File 'lib/bowline/initializer.rb', line 247

def after_initialize
  configuration.after_initialize_blocks.each do |block|
    block.call
  end
end

#disable_dependency_loadingObject



134
135
136
137
138
# File 'lib/bowline/initializer.rb', line 134

def disable_dependency_loading
  if configuration.cache_classes && !configuration.dependency_loading
    ActiveSupport::Dependencies.unhook!
  end
end

#initialize_databaseObject

Initializes ActiveRecord databases



145
146
147
148
149
# File 'lib/bowline/initializer.rb', line 145

def initialize_database
  if defined?(ActiveRecord)
    ActiveRecord::Base.establish_connection(configuration.database_configuration)
  end
end

#initialize_dependency_mechanismObject



140
141
142
# File 'lib/bowline/initializer.rb', line 140

def initialize_dependency_mechanism
  ActiveSupport::Dependencies.mechanism = configuration.cache_classes ? :require : :load
end

#initialize_desktopObject



283
284
285
286
287
# File 'lib/bowline/initializer.rb', line 283

def initialize_desktop
  return unless Desktop.enabled?
  Desktop::Runtime.setup!
  Desktop::JS.setup!
end

#initialize_framework_loggingObject



172
173
174
175
# File 'lib/bowline/initializer.rb', line 172

def initialize_framework_logging
  ActiveRecord::Base.logger ||= Bowline.logger if defined?(ActiveRecord)
  ActiveSupport::Dependencies.logger ||= Bowline.logger
end

#initialize_framework_settingsObject



204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/bowline/initializer.rb', line 204

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_loggerObject



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/bowline/initializer.rb', line 151

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_marshalObject



308
309
310
311
312
313
314
315
316
317
# File 'lib/bowline/initializer.rb', line 308

def initialize_marshal
  return unless defined?(SuperModel)
  path = configuration.marshal_path
  path = path.call if path.is_a?(Proc)
  SuperModel::Marshal.path = path
  SuperModel::Marshal.load
  at_exit {
    SuperModel::Marshal.dump
  }
end

#initialize_nameObject



270
271
272
273
274
275
# File 'lib/bowline/initializer.rb', line 270

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_pathObject



302
303
304
305
306
# File 'lib/bowline/initializer.rb', line 302

def initialize_path
  # Dir::tmpdir/Tempfile uses this
  ENV["TMPDIR"] = Desktop::Path.temp if Desktop.enabled?
  FileUtils.mkdir_p(Desktop::Path.user_data)
end

#initialize_time_zoneObject

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.



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/bowline/initializer.rb', line 185

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_trapObject



295
296
297
298
299
300
# File 'lib/bowline/initializer.rb', line 295

def initialize_trap
  return unless Desktop.enabled?
  trap("INT") {
    Desktop::App.exit
  }
end

#initialize_whiny_nilsObject

Loads support for “whiny nil” (noisy warnings when methods are invoked on nil values) if Configuration#whiny_nils is true.



179
180
181
# File 'lib/bowline/initializer.rb', line 179

def initialize_whiny_nils
  require("active_support/whiny_nil") if configuration.whiny_nils
end

#initialize_windowsObject



289
290
291
292
293
# File 'lib/bowline/initializer.rb', line 289

def initialize_windows
  return unless Desktop.enabled?
  MainWindow.setup!
  MainWindow.title = configuration.name
end

#load_app_configObject

Creates a class called AppConfig from configuration variables found in config/application.yml



279
280
281
# File 'lib/bowline/initializer.rb', line 279

def load_app_config
  Object.const_set("AppConfig", AppConfig.load!(configuration.app_config_file))
end

#load_application_classesObject



259
260
261
262
263
264
265
266
267
268
# File 'lib/bowline/initializer.rb', line 259

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_environmentObject



225
226
227
228
229
# File 'lib/bowline/initializer.rb', line 225

def load_application_environment
  config = configuration
  path   = Bowline.root.join("config", "environments", Bowline.env + ".rb").to_s
  eval(IO.read(path), binding, path)
end

#load_application_first_runObject



237
238
239
240
241
242
243
244
245
# File 'lib/bowline/initializer.rb', line 237

def load_application_first_run
  first_run = Bowline.root.join("app_first_run")
  if first_run.exist?
    first_run.unlink if Bowline.root.writable_real?
    Dir.glob(configuration.first_run_glob).sort.each do |initializer|
      load(initializer)
    end
  end
end

#load_application_helpersObject



253
254
255
256
257
# File 'lib/bowline/initializer.rb', line 253

def load_application_helpers
  helpers = configuration.helpers
  helpers = helpers.map(&:constantize)
  helpers.each {|h| Helpers.module_eval { extend h } }
end

#load_application_initializersObject



231
232
233
234
235
# File 'lib/bowline/initializer.rb', line 231

def load_application_initializers
  Dir.glob(configuration.initializer_glob).sort.each do |initializer|
    load(initializer)
  end
end

#load_gemsObject



99
100
101
102
103
104
105
106
107
108
# File 'lib/bowline/initializer.rb', line 99

def load_gems
  if defined?(Bundler)
    # API changed between 0.8 and 0.9
    if defined?(Bundler.require_env)
      Bundler.require_env
    else
      Bundler.require
    end
  end
end

#load_pluginsObject



218
219
220
221
222
223
# File 'lib/bowline/initializer.rb', line 218

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

#processObject



319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
# File 'lib/bowline/initializer.rb', line 319

def process
  Bowline.configuration = configuration
  
  set_environment
  
  set_load_path
  load_gems
  
  load_application_environment
  
  require_frameworks
  set_autoload_paths
  add_plugin_load_paths
  initialize_dependency_mechanism
  disable_dependency_loading
  
  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_classes
  load_application_helpers
  
  initialize_desktop
  initialize_marshal
  initialize_windows
  initialize_trap
  initialize_path
  
  load_application_initializers
  load_application_first_run
  
  after_initialize
  
  Bowline.initialized = true
end

#require_frameworksObject



89
90
91
# File 'lib/bowline/initializer.rb', line 89

def require_frameworks
  configuration.frameworks.each { |framework| require(framework.to_s) }
end

#set_autoload_pathsObject

Set the paths from which Bowline will automatically load source files, and the load_once paths.



112
113
114
115
116
117
118
119
120
121
# File 'lib/bowline/initializer.rb', line 112

def set_autoload_paths
  # Rails 3 master support
  if ActiveSupport::Dependencies.respond_to?(:autoload_paths)
    ActiveSupport::Dependencies.autoload_paths      = configuration.autoload_paths.uniq
    ActiveSupport::Dependencies.autoload_once_paths = configuration.autoload_once_paths.uniq
  else
    ActiveSupport::Dependencies.load_paths      = configuration.autoload_paths.uniq
    ActiveSupport::Dependencies.load_once_paths = configuration.autoload_once_paths.uniq        
  end
end

#set_environmentObject



82
83
84
85
86
87
# File 'lib/bowline/initializer.rb', line 82

def set_environment
  production_path = Bowline.root.join("app_production")
  ENV["APP_ENV"] ||= begin
    production_path.exist? ? "production" : "development"
  end
end

#set_load_pathObject



93
94
95
96
97
# File 'lib/bowline/initializer.rb', line 93

def set_load_path
  load_paths = configuration.autoload_paths
  load_paths.reverse_each { |dir| $LOAD_PATH.unshift(dir) if File.directory?(dir) }
  $LOAD_PATH.uniq!
end