lib/adhearsion/component_manager.rb in adhearsion-0.8.6 vs lib/adhearsion/component_manager.rb in adhearsion-1.0.0

- old
+ new

@@ -41,31 +41,51 @@ File.directory?(path) end components.map! { |path| File.basename path } components.each do |component| next if component == "disabled" + component_file = File.join(@path_to_container_directory, component, 'lib', component + ".rb") + if File.exists? component_file + load_file component_file + next + end + + # Try the old-style components/<component>/<component>.rb component_file = File.join(@path_to_container_directory, component, component + ".rb") if File.exists? component_file load_file component_file else ahn_log.warn "Component directory does not contain a matching .rb file! Was expecting #{component_file.inspect}" end end + # Load configured system- or gem-provided components + AHN_CONFIG.components_to_load.each do |component| + require component + end + end ## # Loads the configuration file for a given component name. # # @return [Hash] The loaded YAML for the given component name. An empty Hash if no YAML file exists. # def configuration_for_component_named(component_name) + # Look for configuration in #{AHN_ROOT}/config/components first + if File.exists?("#{AHN_ROOT}/config/components/#{component_name}.yml") + return YAML.load_file "#{AHN_ROOT}/config/components/#{component_name}.yml" + end + + # Next try the local app component directory component_dir = File.join(@path_to_container_directory, component_name) config_file = File.join component_dir, "#{component_name}.yml" if File.exists?(config_file) YAML.load_file config_file else + # Nothing found? Return an empty hash + ahn_log.warn "No configuration found for requested component #{component_name}" return {} end end def extend_object_with(object, *scopes) @@ -90,10 +110,14 @@ def load_file(filename) load_container ComponentDefinitionContainer.load_file(filename) end + def require(filename) + load_container ComponentDefinitionContainer.require(filename) + end + protected def load_container(container) container.constants.each do |constant_name| constant_value = container.const_get(constant_name) @@ -114,20 +138,41 @@ class ComponentDefinitionContainer < Module class << self def load_code(code) - returning(new) do |instance| + new.tap do |instance| instance.module_eval code end end def load_file(filename) - returning(new) do |instance| + new.tap do |instance| instance.module_eval File.read(filename), filename end end + + def require(filename) + filename = filename + ".rb" if !(filename =~ /\.rb$/) + begin + # Try loading the exact filename first + load_file(filename) + rescue LoadError, Errno::ENOENT + end + + # Next try Rubygems + filepath = get_gem_path_for(filename) + return load_file(filepath) if !filepath.nil? + + # Finally try the system search path + filepath = get_system_path_for(filename) + return load_file(filepath) if !filepath.nil? + + # Raise a LoadError exception if the file is still not found + raise LoadError, "File not found: #{filename}" + end + end def initialize(&block) # Hide our instance variables in the singleton class metadata = {} @@ -167,9 +212,29 @@ class << self def self.method_added(method_name) @methods ||= [] @methods << method_name + end + + def get_gem_path_for(filename) + # Look for component files provided by rubygems + spec = Gem.searcher.find(filename) + return nil if spec.nil? + File.join(spec.full_gem_path, spec.require_path, filename) + rescue NameError + # In case Rubygems are not available + nil + end + + def get_system_path_for(filename) + $:.each do |path| + filepath = File.join(path, filename) + return filepath if File.exists?(filepath) + end + + # Not found? Return nil + return nil end end end