class Confection::Project

Project configuration.

@todo Rename to ‘ProjectConfig` or ?

Constants

PATTERN

Configuration file pattern. The standard configuration file name is ‘Config.rb`, and that name should be used in most cases. However, `.config.rb` can also be use and will take precedence if found. Conversely, `config.rb` (lowercase form) can also be used but has the least precedence.

Config files looked for in the order or precedence:

* `.config.rb`
* `Config.rb`
* `config.rb`

Attributes

directory[R]

Project root directory.

@return [String] project’s root directory

root[R]

Project root directory.

@return [String] project’s root directory

Public Class Methods

cache() click to toggle source

Per library cache.

# File lib/confection/project.rb, line 29
def self.cache
  @cache ||= {}
end
load(lib=nil) click to toggle source

Get project configuration from another library.

This method uses the Finder gem.

@param [String] lib

Library name.

@return [Project,nil] Located project.

# File lib/confection/project.rb, line 43
def self.load(lib=nil)
  if lib
    lib = lib.to_s
    return cache[lib] if cache.key?(lib)
    cache[lib] ||= (
      config_path = Find.path(PATTERN, :from=>lib).first
      config_path ? new(File.dirname(config_path)) : nil
    )
  else
    lookup
  end
end
lookup(dir=nil) click to toggle source

Lookup configuation file.

@param dir [String]

Optional directory to begin search.

@return [String] file path

# File lib/confection/project.rb, line 64
def self.lookup(dir=nil)
  dir = dir || Dir.pwd
  home = File.expand_path('~')
  while dir != '/' && dir != home
    if file = Dir.glob(File.join(dir, PATTERN), File::FNM_CASEFOLD).first
      return new(File.dirname(file))
    end
    dir = File.dirname(dir)
  end
  return nil
end
new(root) click to toggle source

Initialize new ProjectConfig.

@param [String] root

Project root directory.
# File lib/confection/project.rb, line 82
def initialize(root)
  @root  = root
end

Public Instance Methods

controller(scope, tool, options={}) click to toggle source

Create a configuration controller.

@param [Object] scope

Context for which controller is being created.

@param [Symbol] tool

The tool of the configuration to select.
# File lib/confection/project.rb, line 147
def controller(scope, tool, options={})
  profile = options[:profile]
  configs = store.lookup(tool, profile)
  Controller.new(scope, *configs)
end
each(&block) click to toggle source

Iterate over each configurations.

# File lib/confection/project.rb, line 156
def each(&block)
  store.each(&block)
end
profiles(tool) click to toggle source

List of configuration profiles.

@return [Array] profile names

# File lib/confection/project.rb, line 119
def profiles(tool)
  store.profiles(tool)
end
properties() click to toggle source

Project properties.

@todo Use cascading class, e.g. Confstruct.

# File lib/confection/project.rb, line 128
def properties
  dotruby = File.join(directory,'.ruby')
  if File.exist?(dotruby)
    data = YAML.load_file(dotruby)
    OpenStruct.new(data)
  else
    OpenStruct.new
  end
end
size() click to toggle source

The number of configurations.

@return [Fixnum] config count

# File lib/confection/project.rb, line 165
def size
  store.size
end
source() click to toggle source

The file path of the project’s configuration file.

@return [String] path to configuration file

# File lib/confection/project.rb, line 110
def source
  Dir.glob(File.join(root, PATTERN), File::FNM_CASEFOLD)
end
store() click to toggle source

Configuration store tracks a project’s confirguration entries.

# File lib/confection/project.rb, line 101
def store
  @store ||= Store.new(*source)
end