Class Gem::ConfigFile
In: lib/rubygems/config_file.rb
Parent: Object

Store the gem command options specified in the configuration file. The config file object acts much like a hash.

Methods

Constants

DEFAULT_BACKTRACE = false
DEFAULT_BENCHMARK = false
DEFAULT_BULK_THRESHOLD = 1000
DEFAULT_VERBOSITY = true
DEFAULT_UPDATE_SOURCES = true

Attributes

args  [R]  List of arguments supplied to the config file object.
backtrace  [W]  True if we print backtraces on errors.
benchmark  [RW]  True if we are benchmarking this run.
bulk_threshold  [RW]  Bulk threshold value. If the number of missing gems are above this threshold value, then a bulk download technique is used.
hash  [R] 
update_sources  [RW]  True if we want to update the SourceInfoCache every time, false otherwise
verbose  [RW]  Verbose level of output:
  • false — No output
  • true — Normal output
  • :loud — Extra output

Public Class methods

Create the config file object. args is the list of arguments from the command line.

The following command line options are handled early here rather than later at the time most command options are processed.

  • —config-file and —config-file==NAME — Obviously these need to be handled by the ConfigFile object to ensure we get the right config file.
  • backtrace — Backtrace needs to be turned on early so that errors before normal option parsing can be properly handled.
  • —debug — Enable Ruby level debug messages. Handled early for the same reason as —backtrace.

[Source]

     # File lib/rubygems/config_file.rb, line 59
 59:   def initialize(arg_list)
 60:     @config_file_name = nil
 61:     need_config_file_name = false
 62: 
 63:     arg_list = arg_list.map do |arg|
 64:       if need_config_file_name then
 65:         @config_file_name = arg
 66:         nil
 67:       elsif arg =~ /^--config-file=(.*)/ then
 68:         @config_file_name = $1
 69:         nil
 70:       elsif arg =~ /^--config-file$/ then
 71:         need_config_file_name = true
 72:         nil
 73:       else
 74:         arg
 75:       end
 76:     end.compact
 77: 
 78:     @backtrace = DEFAULT_BACKTRACE
 79:     @benchmark = DEFAULT_BENCHMARK
 80:     @bulk_threshold = DEFAULT_BULK_THRESHOLD
 81:     @verbose = DEFAULT_VERBOSITY
 82:     @update_sources = DEFAULT_UPDATE_SOURCES
 83: 
 84:     begin
 85:       # HACK $SAFE ok?
 86:       @hash = open(config_file_name.dup.untaint) {|f| YAML.load(f) }
 87:     rescue ArgumentError
 88:       warn "Failed to load #{config_file_name}"
 89:     rescue Errno::ENOENT
 90:       # Ignore missing config file error.
 91:     rescue Errno::EACCES
 92:       warn "Failed to load #{config_file_name} due to permissions problem."
 93:     end
 94: 
 95:     @hash ||= {}
 96: 
 97:     # HACK these override command-line args, which is bad
 98:     @backtrace = @hash[:backtrace] if @hash.key? :backtrace
 99:     @benchmark = @hash[:benchmark] if @hash.key? :benchmark
100:     @bulk_threshold = @hash[:bulk_threshold] if @hash.key? :bulk_threshold
101:     Gem.sources.replace @hash[:sources] if @hash.key? :sources
102:     @verbose = @hash[:verbose] if @hash.key? :verbose
103:     @update_sources = @hash[:update_sources] if @hash.key? :update_sources
104: 
105:     handle_arguments arg_list
106:   end

Public Instance methods

Return the configuration information for key.

[Source]

     # File lib/rubygems/config_file.rb, line 200
200:   def [](key)
201:     @hash[key.to_s]
202:   end

Set configuration option key to value.

[Source]

     # File lib/rubygems/config_file.rb, line 205
205:   def []=(key, value)
206:     @hash[key.to_s] = value
207:   end

True if the backtrace option has been specified, or debug is on.

[Source]

     # File lib/rubygems/config_file.rb, line 109
109:   def backtrace
110:     @backtrace or $DEBUG
111:   end

The name of the configuration file.

[Source]

     # File lib/rubygems/config_file.rb, line 114
114:   def config_file_name
115:     @config_file_name || Gem.config_file
116:   end

Delegates to @hash

[Source]

     # File lib/rubygems/config_file.rb, line 119
119:   def each(&block)
120:     hash = @hash.dup
121:     hash.delete :update_sources
122:     hash.delete :verbose
123:     hash.delete :benchmark
124:     hash.delete :backtrace
125:     hash.delete :bulk_threshold
126: 
127:     yield :update_sources, @update_sources
128:     yield :verbose, @verbose
129:     yield :benchmark, @benchmark
130:     yield :backtrace, @backtrace
131:     yield :bulk_threshold, @bulk_threshold
132: 
133:     yield 'config_file_name', @config_file_name if @config_file_name
134: 
135:     hash.each(&block)
136:   end

Handle the command arguments.

[Source]

     # File lib/rubygems/config_file.rb, line 139
139:   def handle_arguments(arg_list)
140:     @args = []
141: 
142:     arg_list.each do |arg|
143:       case arg
144:       when /^--(backtrace|traceback)$/ then
145:         @backtrace = true
146:       when /^--bench(mark)?$/ then
147:         @benchmark = true
148:       when /^--debug$/ then
149:         $DEBUG = true
150:       else
151:         @args << arg
152:       end
153:     end
154:   end

Really verbose mode gives you extra output.

[Source]

     # File lib/rubygems/config_file.rb, line 157
157:   def really_verbose
158:     case verbose
159:     when true, false, nil then false
160:     else true
161:     end
162:   end

Writes out this config file, replacing its source.

[Source]

     # File lib/rubygems/config_file.rb, line 193
193:   def write
194:     File.open config_file_name, 'w' do |fp|
195:       fp.write self.to_yaml
196:     end
197:   end

[Validate]