Class: Rucola::Initializer

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

Overview

Rails-like Initializer responsible for processing configuration.

Constant Summary collapse

RUBYCOCOA_PLUGINS_ROOT =

Now is a good time to load the plugins, because this will give them the chance to alter the behaviour of Rucola before it starts.

RUBYCOCOA_ROOT + 'vendor/plugins'
@@required_plugins =

TODO: isn’t used yet

[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration) ⇒ Initializer

Create an initializer instance that references the given Configuration instance.



82
83
84
# File 'lib/rucola/initializer.rb', line 82

def initialize(configuration)
  @configuration = configuration
end

Instance Attribute Details

#configurationObject (readonly)

The Configuration instance used by this Initializer instance.



55
56
57
# File 'lib/rucola/initializer.rb', line 55

def configuration
  @configuration
end

Class Method Details

.bootObject

Load the config/boot.rb file.



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

def self.boot
  require RUBYCOCOA_ROOT + 'config/boot'
end

.run(command = :process, configuration = Configuration.new) {|configuration| ... } ⇒ Object

Run the initializer and start the application. The #process method is run by default which runs all the initialization routines. You can alternatively specify a command to run.

OSX::Initializer.run(:set_load_path)

Yields:



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

def self.run(command = :process, configuration = Configuration.new)
  yield configuration if block_given?
  initializer = new configuration
  initializer.send(command)
  start_app unless RUBYCOCOA_ENV == 'test'
end

.start_appObject

Starts the application.



76
77
78
# File 'lib/rucola/initializer.rb', line 76

def self.start_app
  OSX.NSApplicationMain(0, nil)
end

Instance Method Details

#copy_load_paths_for_releaseObject

Copy the default load paths to the resource directory for the application if we are building a release, otherwise we do nothing. When in debug mode, the files are loaded directly from your working directory.

TODO: Remove debug database from released app if it exists.



201
202
203
204
205
206
# File 'lib/rucola/initializer.rb', line 201

def copy_load_paths_for_release
  return if configuration.environment == 'debug'
  configuration.load_paths.each do |path|
    `cp -R #{path} #{RUBYCOCOA_ROOT}/#{File.basename(path)}` if File.directory?(path)
  end
end

#initialize_active_record_settingsObject

Initializes active_record settings. The available settings map to the accessors of the ActiveRecord::Base class.



162
163
164
165
166
# File 'lib/rucola/initializer.rb', line 162

def initialize_active_record_settings
  configuration.send('active_record').each do |setting, value|
    ActiveRecord::Base.send("#{setting}=", value)
  end
end

#initialize_databaseObject



152
153
154
155
156
157
158
# File 'lib/rucola/initializer.rb', line 152

def initialize_database
  ActiveRecord::Base.configurations = configuration.database_configuration
  ActiveRecord::Base.logger = Logger.new($stderr)
  ActiveRecord::Base.colorize_logging = false
  ActiveRecord::Base.establish_connection
  ActiveRecord::Base.connection.initialize_schema_information
end

#initialize_database_directoriesObject



147
148
149
150
# File 'lib/rucola/initializer.rb', line 147

def initialize_database_directories
  return if configuration.environment == 'debug'
  `mkdir -p '#{configuration.application_support_path}'` unless File.exists?(configuration.application_support_path)
end

#load_application_initializersObject



168
169
170
171
172
# File 'lib/rucola/initializer.rb', line 168

def load_application_initializers
  Dir["#{configuration.root_path}/config/initializers/**/*.rb"].sort.each do |initializer|
    load(initializer)
  end
end

#load_environmentObject

Loads the environment specified by Configuration#environment_path, which can be debug or release



176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/rucola/initializer.rb', line 176

def load_environment
  return if @environment_loaded
  @environment_loaded = true
  
  config = configuration
  constants = self.class.constants
  eval(IO.read(configuration.environment_path), binding, configuration.environment_path)

  (self.class.constants - constants).each do |const|
    Object.const_set(const, self.class.const_get(const))
  end
end

#processObject

Step through the initialization routines, skipping the active_record routines if active_record isnt’ being used.



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/rucola/initializer.rb', line 88

def process
  unless ENV['DYLD_LIBRARY_PATH'].nil?
    set_load_path
    copy_load_paths_for_release
  end
  
  require_rucola_support
  require_frameworks
  require_ruby_source_files
  load_environment
  
  if configuration.use_active_record?
    initialize_database_directories
    initialize_database
    initialize_active_record_settings
  end
end

#require_frameworksObject

Requires all frameworks specified by the Configuration#objc_frameworks list. This is also responsible for including osx/active_record_proxy if use_active_record? is true



109
110
111
112
113
114
115
116
117
# File 'lib/rucola/initializer.rb', line 109

def require_frameworks
  configuration.objc_frameworks.each { |framework| OSX.require_framework(framework) }
  if configuration.use_active_record?
    require 'active_support'
    configuration.active_record = OrderedOptions.new
    require 'active_record'        
    require 'osx/active_record_proxy'
  end
end

#require_ruby_source_filesObject

Loops through the subdirectories of the app/ directory. It requires any ruby file in any of the subdirectories and registers the required file in the hash @require_ruby_source_files with the name of the subdirectory as it’s key.

require_ruby_source_files # => :views=>[], :controllers=>



141
142
143
144
145
# File 'lib/rucola/initializer.rb', line 141

def require_ruby_source_files
  Dir[RUBYCOCOA_ROOT + 'app/**/*.rb'].each do |f|
    require f
  end
end

#require_ruby_source_files_in_dir_recursive(dir) ⇒ Object

Recursively requires any ruby source file that it finds.



125
126
127
128
129
130
131
132
133
# File 'lib/rucola/initializer.rb', line 125

def require_ruby_source_files_in_dir_recursive(dir)
  dir.children.each do |child|
    if child.directory?
      require_ruby_source_files_in_dir_recursive(child)
      next
    end
    require child if child.basename.to_s =~ /\.rb$/
  end
end

#require_rucola_supportObject

Loads the Rucola support library



120
121
122
# File 'lib/rucola/initializer.rb', line 120

def require_rucola_support
  require Pathname.new(__FILE__).dirname + 'rucola_support'
end

#set_load_pathObject

Set the paths from which your application will automatically load source files.



190
191
192
193
194
# File 'lib/rucola/initializer.rb', line 190

def set_load_path
  load_paths = configuration.load_paths
  load_paths.reverse_each { |dir| $LOAD_PATH.unshift(dir) if File.directory?(dir) } unless RUBYCOCOA_ENV == 'test' # FIXME: why??
  $LOAD_PATH.uniq!
end