Class: RuGUI::Initializer

Inherits:
Object show all
Includes:
LogSupport
Defined in:
lib/rugui/initializer.rb

Constant Summary collapse

@@processed =
false

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from LogSupport

included, #logger

Constructor Details

#initialize(configuration) ⇒ Initializer

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



33
34
35
# File 'lib/rugui/initializer.rb', line 33

def initialize(configuration)
  @configuration = configuration
end

Instance Attribute Details

#configurationObject (readonly)

The configuration for this application.



9
10
11
# File 'lib/rugui/initializer.rb', line 9

def configuration
  @configuration
end

#gems_dependencies_loadedObject (readonly)

Whether or not all the gem dependencies have been met



12
13
14
# File 'lib/rugui/initializer.rb', line 12

def gems_dependencies_loaded
  @gems_dependencies_loaded
end

Class Method Details

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

Runs the initializer.

It runs the process procedure by default, by this can be changed by specifying a command when calling this method. A block can be given in order to change the application configuration.

Yields:



22
23
24
25
26
27
28
29
# File 'lib/rugui/initializer.rb', line 22

def self.run(command = :process, configuration = Configuration.new)
  yield configuration if block_given?
  initializer = new configuration
  RuGUI.configuration = configuration
  initializer.send(command)
  @@processed = (command == :process) ? true : false
  initializer
end

Instance Method Details

#add_gem_load_pathsObject



101
102
103
104
105
106
107
# File 'lib/rugui/initializer.rb', line 101

def add_gem_load_paths
  RuGUI::GemDependency.add_frozen_gem_path
  unless @configuration.gems.empty?
    require "rubygems"
    @configuration.gems.each { |gem| gem.add_load_paths }
  end
end

#check_gem_dependenciesObject



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/rugui/initializer.rb', line 115

def check_gem_dependencies
  unloaded_gems = @configuration.gems.reject { |g| g.loaded? }
  if unloaded_gems.size > 0
    @gems_dependencies_loaded = false
    # don't print if the gems rake tasks are being run
    unless $gems_rake_task
      abort <<-end_error
Missing these required gems:
  #{unloaded_gems.map { |gem| "#{gem.name}  #{gem.requirement}" } * "\n  "}

You're running:
  ruby #{Gem.ruby_version} at #{Gem.ruby}
  rubygems #{Gem::RubyGemsVersion} at #{Gem.path * ', '}

Run `rake gems:install` to install the missing gems.
      end_error
    end
  else
    @gems_dependencies_loaded = true
  end
end

#finish_initialization_process_logObject



145
146
147
# File 'lib/rugui/initializer.rb', line 145

def finish_initialization_process_log
  logger.info "RuGUI application configurations loaded." unless silence_logs?
end

#load_environmentObject

Loads the environment specified by Configuration#environment_path, which is typically one of development, or production.



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/rugui/initializer.rb', line 81

def load_environment
  return if @environment_loaded
  @environment_loaded = true

  if File.exist?(configuration.environment_path)
    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
end

#load_framework_adapterObject



153
154
155
# File 'lib/rugui/initializer.rb', line 153

def load_framework_adapter
  require "rugui/framework_adapters/#{RuGUI.configuration.framework_adapter}"
end

#load_gemsObject



109
110
111
112
113
# File 'lib/rugui/initializer.rb', line 109

def load_gems
  unless $gems_build_rake_task
    @configuration.gems.each { |gem| gem.load }
  end
end

#load_loggerObject



137
138
139
# File 'lib/rugui/initializer.rb', line 137

def load_logger
  RuGUILogger.logger
end

#load_pluginsObject



97
98
99
# File 'lib/rugui/initializer.rb', line 97

def load_plugins
  plugin_loader.load_plugins
end

#plugin_loaderObject



149
150
151
# File 'lib/rugui/initializer.rb', line 149

def plugin_loader
  @plugin_loader || RuGUI::Plugin::Loader.new(self, configuration)
end

#processObject

Sequentially step through all of the available initialization routines, in order (view execution order in source).



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/rugui/initializer.rb', line 39

def process
 load_environment
 load_logger

 start_initialization_process_log

 set_load_path
 add_gem_load_paths

 set_autoload_paths
 load_framework_adapter

 load_gems
 load_plugins

 # pick up any gems that plugins depend on
 add_gem_load_paths
 load_gems
 check_gem_dependencies

 # bail out if gems are missing - note that check_gem_dependencies will have
 # already called abort() unless $gems_rake_task is set
 return unless gems_dependencies_loaded

 finish_initialization_process_log
end

#set_autoload_pathsObject

Set the paths from which RuGUI will automatically load source files.



75
76
77
# File 'lib/rugui/initializer.rb', line 75

def set_autoload_paths
  ActiveSupport::Dependencies.autoload_paths = configuration.load_paths.uniq
end

#set_load_pathObject

Set the $LOAD_PATH based on the value of Configuration#load_paths. Duplicates are removed.



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

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

#start_initialization_process_logObject



141
142
143
# File 'lib/rugui/initializer.rb', line 141

def start_initialization_process_log
  logger.info "Starting RuGUI application with #{configuration.environment} environment..." unless silence_logs?
end