Class: Copland::Configuration::Loader

Inherits:
Object
  • Object
show all
Defined in:
lib/copland/configuration/loader.rb

Overview

This is the driver class for processing a series of directories, looking for configuration files. It relies on visitor objects to do the actual configuration processing–all this class does is recurse through the requested directories.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(search_paths = []) ⇒ Loader

Create a new Loader object that will search the given paths. Regardless, the ‘copland/impl’ directory will always be processed.



52
53
54
55
56
57
# File 'lib/copland/configuration/loader.rb', line 52

def initialize( search_paths=[] )
  @search_paths = []
  use_library "copland"
  add_search_path *search_paths
  @loaders = []
end

Instance Attribute Details

#loadersObject (readonly)

This is the array of visitor objects that will be employed by this driver.



48
49
50
# File 'lib/copland/configuration/loader.rb', line 48

def loaders
  @loaders
end

#search_pathsObject (readonly)

This is the array of search paths that the loader will descend.



44
45
46
# File 'lib/copland/configuration/loader.rb', line 44

def search_paths
  @search_paths
end

Instance Method Details

#add_loader(loader) ⇒ Object

Add a new visitor loader to be used while processing directories.



65
66
67
# File 'lib/copland/configuration/loader.rb', line 65

def add_loader( loader )
  @loaders.push loader
end

#add_search_path(*paths) ⇒ Object

Add new search paths to be searched.



60
61
62
# File 'lib/copland/configuration/loader.rb', line 60

def add_search_path( *paths )
  @search_paths.concat( paths ).uniq!
end

#load(options = {}) ⇒ Object

Processes each search path in turn, and finishes by calling #finalize! on each of the visitors. The options parameter is passed to each loader.



72
73
74
75
# File 'lib/copland/configuration/loader.rb', line 72

def load( options={} )
  @search_paths.each { |path| load_path path, options }
  @loaders.each { |loader| loader.finalize! }
end

#load_path(path, options) ⇒ Object

Processes a single path (recursively). Each loader gets a turn at the directory, and then any subdirectories are processed by recursively calling this method on that directory.

If the given path does not exist, this does nothing.



82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/copland/configuration/loader.rb', line 82

def load_path( path, options )
  return unless File.directory?( path ) 

  @loaders.each do |loader|
    options = loader.process_dir( path, options )
  end

  Dir.foreach( path ) do |entry|
    next if entry == "." || entry == ".."

    file_name = File.join( path, entry )
    load_path( file_name, options ) if File.directory?( file_name )
  end
end

#use_library(name) ⇒ Object

Adds the use of the given library to the registry that is being constructed. The name parameter must be the string that would be require‘d (i.e., “copland/lib”). That file must then (at least) add a value to the Copland::LIBRARIES hash: the key must be the library name (i.e., “copland/lib”), and the value must be an array of paths that should be searched for this library, for package descriptors.



104
105
106
107
108
109
# File 'lib/copland/configuration/loader.rb', line 104

def use_library( name )
  require( name )
  # add_search_path does a uniq! on the result, so we can always just
  # add the whole thing in and not worry about duplication.
  add_search_path *Copland::LIBRARIES[ name ]
end