Class: Rucola::Initializer
- Inherits:
-
Object
- Object
- Rucola::Initializer
- 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
-
#configuration ⇒ Object
readonly
The Configuration instance used by this Initializer instance.
Class Method Summary collapse
-
.boot ⇒ Object
Load the config/boot.rb file.
-
.run(command = :process, configuration = Configuration.new) {|configuration| ... } ⇒ Object
Run the initializer and start the application.
-
.start_app ⇒ Object
Starts the application.
Instance Method Summary collapse
-
#copy_load_paths_for_release ⇒ Object
Copy the default load paths to the resource directory for the application if we are building a release, otherwise we do nothing.
-
#initialize(configuration) ⇒ Initializer
constructor
Create an initializer instance that references the given Configuration instance.
-
#initialize_active_record_settings ⇒ Object
Initializes active_record settings.
- #initialize_database ⇒ Object
- #initialize_database_directories ⇒ Object
- #load_application_initializers ⇒ Object
-
#load_environment ⇒ Object
Loads the environment specified by Configuration#environment_path, which can be debug or release.
-
#process ⇒ Object
Step through the initialization routines, skipping the active_record routines if active_record isnt’ being used.
-
#require_frameworks ⇒ Object
Requires all frameworks specified by the Configuration#objc_frameworks list.
-
#require_ruby_source_files ⇒ Object
Loops through the subdirectories of the app/ directory.
-
#require_ruby_source_files_in_dir_recursive(dir) ⇒ Object
Recursively requires any ruby source file that it finds.
-
#require_rucola_support ⇒ Object
Loads the Rucola support library.
-
#set_load_path ⇒ Object
Set the paths from which your application will automatically load source files.
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
#configuration ⇒ Object (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
.boot ⇒ Object
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)
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_app ⇒ Object
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_release ⇒ Object
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_settings ⇒ Object
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_database ⇒ Object
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_directories ⇒ Object
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_initializers ⇒ Object
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_environment ⇒ Object
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 |
#process ⇒ Object
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_frameworks ⇒ Object
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_files ⇒ Object
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_support ⇒ Object
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_path ⇒ Object
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 |