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 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
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
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
# File lib/courtier/configuration.rb, line 153 def [](feature) @_config[feature.to_s] end
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
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
# File lib/courtier/configuration.rb, line 82 def evaluate(*args, &block) dsl = DSL.new(self) dsl.instance_eval(*args, &block) end
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
The number of feature configs.
# File lib/courtier/configuration.rb, line 174 def size @_config.size end
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
Private Instance Methods
# 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