class Courtier::Configuration

The Configuration class encapsulates a project/library’s tool configuration.

Constants

CONFIG_FILE

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` or `.confile.rb`
* `Config.rb`  or `Confile.rb`
* `config.rb`  or `confile.rb`

The ‘.rb` suffix is optional for `confile` variations, but recommended. It is not optional for `config` b/c very old version of setup.rb script still in use by some projects use `.config` for it’s own purposes.

TODO: Yes, there are really too many choices for config file name, but we haven’t been able to settle on a smaller list just yet. Please come argue with us about what’s best.

ROOT_INDICATORS

When looking up config file, it one of these is found then there is no point to looking further.

Public Class Methods

load(options={}) click to toggle source

Load configuration file from local project or other gem.

@param options [Hash] Load options.

@option options [String] :from

Name of gem or library.
# File lib/courtier/configuration.rb, line 50
def self.load(options={})
  if from = options[:from]
    file = Find.path(CONFIG_FILE, :from=>from).first
  else
    file = lookup(CONFIG_FILE)
  end
  new(file)
end
new(file=nil) click to toggle source

Initialize new Configuration object.

@param [String] file

Configuration file (optional).
# File lib/courtier/configuration.rb, line 65
def initialize(file=nil)
  @file = file

  @_config = Hash.new{ |h,k| h[k]=[] }
  #@_onload = Hash.new{ |h,k| h[k]=[] }

  # TODO: does this rescue make sense here?
  begin
    dsl = DSL.new(self)
    dsl.instance_eval(File.read(file), file) if file      
  rescue => e
    raise e if $DEBUG
    warn e.message
  end
end

Private Class Methods

lookup(glob, flags=0) click to toggle source

Search upward from working directory.

# File lib/courtier/configuration.rb, line 223
def self.lookup(glob, flags=0)
  pwd  = File.expand_path(Dir.pwd)
  home = File.expand_path('~')
  while pwd != '/' && pwd != home
    if file = Dir.glob(File.join(pwd, glob), flags).first
      return file
    end
    break if ROOT_INDICATORS.any?{ |r| File.exist?(File.join(pwd, r)) }
    pwd = File.dirname(pwd)
  end
  return nil   
end

Public Instance Methods

[](feature) click to toggle source
# File lib/courtier/configuration.rb, line 153
def [](feature)
  @_config[feature.to_s]
end
config(target, options={}, &block) click to toggle source

Configure a tool.

@param [Symbol] tool

The name of the command or feature to configure.

@param [Hash] opts

Configuration options.

@options opts [String] :command

Name of command, or false if not a command configuration.

@options opts [String] :feature

Alternate require if differnt than command name.

@options opts [String] :from

Library from which to import configuration.

@example

profile :coverage do
  config :qed, :from=>'qed'
end
# File lib/courtier/configuration.rb, line 110
def config(target, options={}, &block)
  #options[:profile] = (options[:profile] || 'default').to_s
  #options[:command] = command.to_s unless options.key?(:command)
  #options[:feature] = command.to_s unless options.key?(:feature)
  #command = options[:command].to_s

  # IDEA: other import options such as local file?

  configs_from(options).each do |c|
    @_config[target.to_s] << c.copy(options)
  end

  return unless block

  @_config[target.to_s] << Config.new(target, options, &block)
end
configurations()

@deprecated

Alias for: to_a
each(&block) click to toggle source

Iterate over each feature config.

@example

confgiuration.each do |feature, configs|
  configs.each do |config|
    ...
  end
end
# File lib/courtier/configuration.rb, line 167
def each(&block)
  @_config.each(&block)
end
evaluate(*args, &block) click to toggle source
# File lib/courtier/configuration.rb, line 82
def evaluate(*args, &block)
  dsl = DSL.new(self)
  dsl.instance_eval(*args, &block)
end
profile_names(command=nil) click to toggle source

Get a list of defined profiles names for the given command. use the current command if no command is given.

# File lib/courtier/configuration.rb, line 200
def profile_names(command=nil)
  command = command || Courtier.current_command

  list = []
  @_config.each do |feature, configs|
    configs.each do |c|
      if c.command?(command)
        list << c.profile
      end
    end
  end
  list.uniq
end
size() click to toggle source

The number of feature configs.

# File lib/courtier/configuration.rb, line 174
def size
  @_config.size
end
to_a() click to toggle source

Get a list of the defined configurations.

@return [Array] List of all defined configurations.

# File lib/courtier/configuration.rb, line 183
def to_a
  list = []
  @_config.each do |feature, configs|
    list.concat(configs)
  end
  list
end
Also aliased as: configurations

Private Instance Methods

configs_from(options) click to toggle source
# File lib/courtier/configuration.rb, line 241
def configs_from(options)
  from = options[:from]
  list = []

  return list unless from

  if Array === from
    from_name, from_opts = *from
  else
    from_name, from_opts = from, {}
  end

  from_config = Courtier.configuration(from_name)

  from_opts[:feature] = options[:feature] unless from_opts.key?(:feature) if options[:feature]
  from_opts[:command] = options[:command] unless from_opts.key?(:command) if options[:command]
  from_opts[:profile] = options[:profile] unless from_opts.key?(:profile) if options[:profile]

  from_opts[:feature] = from_opts[:feature].to_s if from_opts[:feature]
  from_opts[:command] = from_opts[:command].to_s if from_opts[:command]
  from_opts[:profile] = from_opts[:profile].to_s if from_opts[:profile]

  from_config.each do |ftr, confs|
    confs.each_with_index do |c, i|
      if c.match?(from_opts)
        list << c.copy(options)
      end
    end
  end

  list
end