Class: Bowline::Initializer

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

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.



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

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

Instance Attribute Details

#configurationObject (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.

Yields:



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_pathsObject



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_initializeObject



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_databaseObject



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_encodingObject

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_loggingObject



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_settingsObject



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_gemsObject



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_jsObject



264
265
266
# File 'lib/bowline/initializer.rb', line 264

def initialize_js
  Bowline.js.bowline_loaded
end

#initialize_loggerObject



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_nameObject



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_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.



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_nilsObject

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_configObject



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_classesObject



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_helpersObject



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_initializersObject



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_gemsObject



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| 
    options = {
      :lib => dep.name
    }.merge(dep.options)
    
    next unless options[:lib]
    begin
      gem(dep.name, *dep.versions)
      require(options[:lib])
    rescue LoadError => e
      puts "was unable to require #{dep.name} as '#{options[:lib]}'
      Reason: #{e.class.name} error raised with message: #{e.message}"
    end
  end
end

#load_pluginsObject



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

#processObject



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_frameworksObject



63
64
65
# File 'lib/bowline/initializer.rb', line 63

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.



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_pathObject



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