Class: Courtier::Configuration

Inherits:
Module
  • Object
show all
Includes:
Enumerable
Defined in:
lib/courtier/configuration.rb

Overview

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

Defined Under Namespace

Classes: DSL

Constant Summary collapse

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.

'{.c,C,c}onfi{g.rb,le.rb,le}'
ROOT_INDICATORS =

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

%w{.git .hg _darcs .ruby}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file = nil) ⇒ Configuration

Initialize new Configuration object.

Parameters:

  • file (String) (defaults to: nil)

    Configuration file (optional).



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# 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

Class Method Details

.load(options = {}) ⇒ Object

Load configuration file from local project or other gem.

Parameters:

  • options (Hash) (defaults to: {})

    Load options.

Options Hash (options):

  • :from (String)

    Name of gem or library.



50
51
52
53
54
55
56
57
# 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

.lookup(glob, flags = 0) ⇒ Object (private)

Search upward from working directory.



223
224
225
226
227
228
229
230
231
232
233
234
# 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

Instance Method Details

#[](feature) ⇒ Object



153
154
155
# File 'lib/courtier/configuration.rb', line 153

def [](feature)
  @_config[feature.to_s]
end

#config(target, options = {}, &block) ⇒ Object

Configure a tool.

Examples:

profile :coverage do
  config :qed, :from=>'qed'
end

Parameters:

  • tool (Symbol)

    The name of the command or feature to configure.

  • opts (Hash)

    Configuration options.



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# 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

#configs_from(options) ⇒ Object (private)



241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# 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

#each(&block) ⇒ Object

Iterate over each feature config.

Examples:

confgiuration.each do |feature, configs|
  configs.each do |config|
    ...
  end
end


167
168
169
# File 'lib/courtier/configuration.rb', line 167

def each(&block)
  @_config.each(&block)
end

#evaluate(*args, &block) ⇒ Object



82
83
84
85
# 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) ⇒ Object

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



200
201
202
203
204
205
206
207
208
209
210
211
212
# 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

#sizeObject

The number of feature configs.



174
175
176
# File 'lib/courtier/configuration.rb', line 174

def size
  @_config.size
end

#to_aArray Also known as: configurations

Get a list of the defined configurations.

Returns:

  • (Array)

    List of all defined configurations.



183
184
185
186
187
188
189
# File 'lib/courtier/configuration.rb', line 183

def to_a
  list = []
  @_config.each do |feature, configs|
    list.concat(configs)
  end
  list
end