Class: RC::Configuration
- Inherits:
-
Module
- Object
- Module
- RC::Configuration
- Includes:
- Enumerable
- Defined in:
- lib/rc/configuration.rb
Overview
The Configuration class encapsulates a project/library’s tool configuration.
Constant Summary collapse
- CONFIG_FILE =
Runtime configuration file glob pattern.
'.ruby{rc,}'
Class Method Summary collapse
-
.load(options = {}) ⇒ Object
Load configuration file from local project or other gem.
-
.load_from(gem) ⇒ Object
Load configuration from another gem.
-
.load_local ⇒ Object
Load configuration for current project.
-
.lookup(glob, flags = 0) ⇒ Object
private
Search upward from working directory.
Instance Method Summary collapse
- #[](feature) ⇒ Object
-
#config(target, options = {}, &block) ⇒ Object
Configure a commandline tool or feature.
- #configs_from(options) ⇒ Object private
-
#each(&block) ⇒ Object
Iterate over each feature config.
-
#evaluate(*args, &cfg) ⇒ Object
Evaluate configuration code.
-
#import(glob, opts = {}) ⇒ Object
Import other runtime configuration files.
-
#initialize(*files) ⇒ Configuration
constructor
Initialize new Configuration object.
-
#load_files(*files) ⇒ Object
(also: #load_file)
Load configuration files.
-
#lookup_root ⇒ Object
private
Search upward from project root directory.
-
#profile_names(command = nil) ⇒ Object
Get a list of defined profiles names for the given
command
. -
#size ⇒ Object
The number of feature configs.
-
#to_a ⇒ Array
(also: #configurations)
Get a list of the defined configurations.
Constructor Details
#initialize(*files) ⇒ Configuration
Initialize new Configuration object.
94 95 96 97 98 99 100 101 |
# File 'lib/rc/configuration.rb', line 94 def initialize(*files) @files = files @_config = Hash.new{ |h,k| h[k]=[] } #@_onload = Hash.new{ |h,k| h[k]=[] } load_files(*files) end |
Class Method Details
.load(options = {}) ⇒ Object
Load configuration file from local project or other gem.
27 28 29 30 31 32 33 |
# File 'lib/rc/configuration.rb', line 27 def load(={}) if [:from] load_from([:from]) else load_local() end end |
.load_from(gem) ⇒ Object
Load configuration from another gem.
38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/rc/configuration.rb', line 38 def load_from(gem) files = Find.path(CONFIG_FILE, :from=>gem) file = files.find{ |f| File.file?(f) } new(*file) #if file # paths = [file] #else # #paths = Find.path(CONFIG_DIR + '/**/*', :from=>gem) #end #files = paths.select{ |path| File.file?(path) } #new(*files) end |
.load_local ⇒ Object
Load configuration for current project.
55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/rc/configuration.rb', line 55 def load_local files = lookup(CONFIG_FILE) file = files.find{ |f| File.file?(f) } new(*file) #if file # paths = [file] #else # dir = lookup(CONFIG_DIR).find{ |f| File.directory?(f) } # paths = dir ? Dir.glob(File.join(dir, '**/*')) : [] #end #files = paths.select{ |path| File.file?(path) } end |
.lookup(glob, flags = 0) ⇒ Object (private)
Search upward from working directory.
74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/rc/configuration.rb', line 74 def lookup(glob, flags=0) pwd = File.(Dir.pwd) home = File.('~') while pwd != '/' && pwd != home paths = Dir.glob(File.join(pwd, glob), flags) return paths unless paths.empty? break if ROOT_INDICATORS.any?{ |r| File.exist?(File.join(pwd, r)) } pwd = File.dirname(pwd) end return [] end |
Instance Method Details
#[](feature) ⇒ Object
242 243 244 |
# File 'lib/rc/configuration.rb', line 242 def [](feature) @_config[feature.to_s] end |
#config(target, options = {}, &block) ⇒ Object
Configure a commandline tool or feature.
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/rc/configuration.rb', line 199 def config(target, ={}, &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().each do |c| @_config[target.to_s] << c.copy() end return unless block @_config[target.to_s] << Config.new(target, , &block) end |
#configs_from(options) ⇒ Object (private)
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 |
# File 'lib/rc/configuration.rb', line 314 def configs_from() from = [:from] list = [] return list unless from if Array === from from_name, from_opts = *from else from_name, from_opts = from, {} end from_config = RC.configuration(from_name) from_opts[:feature] = [:feature] unless from_opts.key?(:feature) if [:feature] from_opts[:command] = [:command] unless from_opts.key?(:command) if [:command] from_opts[:profile] = [:profile] unless from_opts.key?(:profile) if [: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() end end end list end |
#each(&block) ⇒ Object
Iterate over each feature config.
256 257 258 |
# File 'lib/rc/configuration.rb', line 256 def each(&block) @_config.each(&block) end |
#evaluate(*args, &cfg) ⇒ Object
Evaluate configuration code.
171 172 173 174 |
# File 'lib/rc/configuration.rb', line 171 def evaluate(*args, &cfg) dsl = DSL.new(self) dsl.instance_eval(*args, &cfg) end |
#import(glob, opts = {}) ⇒ Object
Import other runtime configuration files.
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/rc/configuration.rb', line 117 def import(glob, opts={}) paths = [] glob = glob + '**/*' if glob.end_with?('/') if from = opts[:from] paths = Find.path(glob, :from=>from) else if glob.start_with?('/') if root = lookup_root glob = File.join(root, glob) else raise "no project root for #{glob}" unless root end end paths = Dir.glob(glob) end paths = paths.select{ |path| File.file?(path) } paths = paths[0..0] if opts[:first] load_files(*paths) paths.empty? ? nil : paths end |
#load_files(*files) ⇒ Object Also known as: load_file
Load configuration files.
TODO: begin/rescue around instance_eval?
TODO: Does each file need it’s own DSL instance?
150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/rc/configuration.rb', line 150 def load_files(*files) dsl = DSL.new(self) files.each do |file| next unless File.file?(file) # TODO: warn ? #begin dsl.instance_eval(File.read(file), file) #rescue => e # raise e if $DEBUG # warn e.message #end end end |
#lookup_root ⇒ Object (private)
Search upward from project root directory.
350 351 352 353 354 355 356 357 358 |
# File 'lib/rc/configuration.rb', line 350 def lookup_root pwd = File.(Dir.pwd) home = File.('~') while pwd != '/' && pwd != home return pwd if ROOT_INDICATORS.any?{ |r| File.exist?(File.join(pwd, r)) } pwd = File.dirname(pwd) end return nil 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.
289 290 291 292 293 294 295 296 297 298 299 300 301 |
# File 'lib/rc/configuration.rb', line 289 def profile_names(command=nil) command = command || RC.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 ⇒ Object
The number of feature configs.
263 264 265 |
# File 'lib/rc/configuration.rb', line 263 def size @_config.size end |
#to_a ⇒ Array Also known as: configurations
Get a list of the defined configurations.
272 273 274 275 276 277 278 |
# File 'lib/rc/configuration.rb', line 272 def to_a list = [] @_config.each do |feature, configs| list.concat(configs) end list end |