Class: Rucola::Initializer

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

Overview

Rails-like Initializer responsible for processing configuration.

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.



107
108
109
# File 'lib/rucola/initializer.rb', line 107

def initialize(configuration)
  @configuration = configuration
end

Instance Attribute Details

#configurationObject (readonly)

The Configuration instance used by this Initializer instance.



53
54
55
# File 'lib/rucola/initializer.rb', line 53

def configuration
  @configuration
end

Class Method Details

.bootObject

Load the config/boot.rb file.



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

def boot
  Rucola::Plugin.before_boot
  do_boot
  Rucola::Plugin.after_boot
end

.do_bootObject

Override this method from your Plugin.before_boot method if you need to alter behaviour before any of the application’s files are required and the app is started.



66
67
68
# File 'lib/rucola/initializer.rb', line 66

def do_boot
  require RUBYCOCOA_ROOT + 'config/boot'
end

.load_pluginsObject

Loads all the plugins that are found in plugins_root.



76
77
78
79
80
81
82
83
84
# File 'lib/rucola/initializer.rb', line 76

def load_plugins
  root = plugins_root
  if root.exist?
    root.children.each do |plugin|
      next unless plugin.directory?
      Kernel.require plugin + 'init.rb'
    end
  end
end

.plugins_rootObject

Returns the path to the plugins root directory. Eg /MyApp/vendor/plugins.



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

def plugins_root
  RUBYCOCOA_ROOT + 'vendor/plugins'
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:



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

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

.start_appObject

Starts the application’s run loop.



100
101
102
# File 'lib/rucola/initializer.rb', line 100

def start_app
  OSX.NSApplicationMain(0, nil) unless RUBYCOCOA_ENV == 'test' || ENV['DONT_START_RUBYCOCOA_APP']
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 or test mode, the files are loaded directly from your working directory.

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



195
196
197
198
199
200
# File 'lib/rucola/initializer.rb', line 195

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

#load_application_initializersObject



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

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



170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/rucola/initializer.rb', line 170

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.



113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/rucola/initializer.rb', line 113

def process
  Rucola::Plugin.before_process(self)
  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
  Rucola::Plugin.after_process(self)
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



130
131
132
# File 'lib/rucola/initializer.rb', line 130

def require_frameworks
  configuration.objc_frameworks.each { |framework| OSX.require_framework(framework) }
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=>



156
157
158
159
160
# File 'lib/rucola/initializer.rb', line 156

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.



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

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



135
136
137
# File 'lib/rucola/initializer.rb', line 135

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.



184
185
186
187
188
# File 'lib/rucola/initializer.rb', line 184

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