Class: Adhearsion::Components::ComponentModule

Inherits:
Module show all
Defined in:
lib/adhearsion/component_manager.rb

Defined Under Namespace

Modules: ComponentModuleMethods Classes: MissingFile

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Module

#relationships

Constructor Details

#initialize(component) {|_self| ... } ⇒ ComponentModule

:yields: self

Yields:

  • (_self)

Yield Parameters:



180
181
182
183
184
185
186
187
188
189
# File 'lib/adhearsion/component_manager.rb', line 180

def initialize(component)   # :yields: self
  extend ComponentModuleMethods
  @component       = component        
  @loaded_features = {}

  const_set :Component, component
  const_set :Configuration, OpenStruct.new(:description => nil)

  yield self if block_given?
end

Instance Attribute Details

#componentObject (readonly)

Creates new Script, and loads main_file in the scope of the Script. If a block is given, the script is passed to it before loading from the file, and constants can be defined as inputs to the script.



179
180
181
# File 'lib/adhearsion/component_manager.rb', line 179

def component
  @component
end

#dirObject (readonly)

The directory in which main_file is located, and relative to which #load searches for files before falling back to Kernel#load.



165
166
167
# File 'lib/adhearsion/component_manager.rb', line 165

def dir
  @dir
end

#loaded_featuresObject (readonly)

A hash that maps filename=>true for each file that has been required locally by the script. This has the same semantics as $", alias $LOADED_FEATURES, except that it is local to this script.



170
171
172
# File 'lib/adhearsion/component_manager.rb', line 170

def loaded_features
  @loaded_features
end

#main_fileObject (readonly)

The file with which the Script was instantiated.



161
162
163
# File 'lib/adhearsion/component_manager.rb', line 161

def main_file
  @main_file
end

Instance Method Details

#load(file, wrap = false) ⇒ Object

Loads file into this Script. Searches relative to the local dir, that is, the dir of the file given in the original call to Script.load(file), loads the file, if found, into this Script’s scope, and returns true. If the file is not found, falls back to Kernel.load, which searches on $LOAD_PATH, loads the file, if found, into global scope, and returns true. Otherwise, raises LoadError.

The wrap argument is passed to Kernel.load in the fallback case, when the file is not found locally.

Typically called from within the main file to load additional sub files, or from those sub files.



209
210
211
212
213
214
# File 'lib/adhearsion/component_manager.rb', line 209

def load(file, wrap = false)
  load_in_module(File.join(component.path, file))
  true
rescue MissingFile
  super
end

#load_configuration_fileObject



191
192
193
# File 'lib/adhearsion/component_manager.rb', line 191

def load_configuration_file
  load_in_module(component.configuration_file)
end

#load_in_module(file) ⇒ Object

Loads file in this module’s context. Note that _\FILE\_ and _\LINE\_ work correctly in file. Called by #load and #require; not normally called directly.



244
245
246
247
248
249
250
251
252
# File 'lib/adhearsion/component_manager.rb', line 244

def load_in_module(file)
  module_eval(File.read(file), File.expand_path(file))
rescue Errno::ENOENT => e
  if /#{file}$/ =~ e.message
    raise MissingFile, e.message
  else
    raise
  end
end

#require(feature) ⇒ Object

Analogous to Kernel#require. First tries the local dir, then falls back to Kernel#require. Will load a given feature only once.

Note that extensions (*.so, *.dll) can be required in the global scope, as usual, but not in the local scope. (This is not much of a limitation in practice–you wouldn’t want to load an extension more than once.) This implementation falls back to Kernel#require when the argument is an extension or is not found locally.



225
226
227
228
229
230
231
232
233
234
235
# File 'lib/adhearsion/component_manager.rb', line 225

def require(feature)
  unless @loaded_features[feature]
    @loaded_features[feature] = true
    file = feature
    file += ".rb" unless /\.rb$/ =~ file
    load_in_module(File.join(component.path, file))
  end
rescue MissingFile
  @loaded_features[feature] = false
  super
end

#to_sObject



254
255
256
# File 'lib/adhearsion/component_manager.rb', line 254

def to_s
  "#<#{self.class}:#{File.basename(component.path)}>"
end