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

Gem::ConfigFile RubyGems options and gem command options from ~/.gemrc.

~/.gemrc is a YAML file that uses strings to match gem command arguments and symbols to match RubyGems options.

Gem command arguments use a String key that matches the command name and allow you to specify default arguments:

  install: --no-rdoc --no-ri
  update: --no-rdoc --no-ri

You can use gem: to set default arguments for all commands.

RubyGems options use symbol keys. Valid options are:

+:backtrace+:See backtrace
+:benchmark+:See benchmark
+:sources+:Sets Gem::sources
+:verbose+:See verbose

Methods

Constants

DEFAULT_BACKTRACE = false
DEFAULT_BENCHMARK = false
DEFAULT_BULK_THRESHOLD = 1000
DEFAULT_VERBOSITY = true
DEFAULT_UPDATE_SOURCES = true
OPERATING_SYSTEM_DEFAULTS = {}   For Ruby packagers to set configuration defaults. Set in rubygems/defaults/operating_system.rb
PLATFORM_DEFAULTS = {}   For Ruby implementers to set configuration defaults. Set in rubygems/defaults/#{RUBY_ENGINE}.rb
CSIDL_COMMON_APPDATA = 0x0023
SHGetFolderPath = Win32API.new 'shell32', 'SHGetFolderPath', 'PLPLP', 'L', :stdcall
SHGetFolderPath = Win32API.new 'shell32', 'SHGetFolderPath', 'LLLLP', 'L'
SYSTEM_WIDE_CONFIG_FILE = File.join system_config_path, 'gemrc'

Attributes

api_keys  [R]  Hash of RubyGems.org and alternate API keys
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. (deprecated)
hash  [R] 
home  [RW]  Where to install gems (deprecated)
path  [RW]  Where to look for gems (deprecated)
rubygems_api_key  [R]  API key for RubyGems.org
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, —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 150
150:   def initialize(arg_list)
151:     @config_file_name = nil
152:     need_config_file_name = false
153: 
154:     arg_list = arg_list.map do |arg|
155:       if need_config_file_name then
156:         @config_file_name = arg
157:         need_config_file_name = false
158:         nil
159:       elsif arg =~ /^--config-file=(.*)/ then
160:         @config_file_name = $1
161:         nil
162:       elsif arg =~ /^--config-file$/ then
163:         need_config_file_name = true
164:         nil
165:       else
166:         arg
167:       end
168:     end.compact
169: 
170:     @backtrace = DEFAULT_BACKTRACE
171:     @benchmark = DEFAULT_BENCHMARK
172:     @bulk_threshold = DEFAULT_BULK_THRESHOLD
173:     @verbose = DEFAULT_VERBOSITY
174:     @update_sources = DEFAULT_UPDATE_SOURCES
175: 
176:     operating_system_config = Marshal.load Marshal.dump(OPERATING_SYSTEM_DEFAULTS)
177:     platform_config = Marshal.load Marshal.dump(PLATFORM_DEFAULTS)
178:     system_config = load_file SYSTEM_WIDE_CONFIG_FILE
179:     user_config = load_file config_file_name.dup.untaint
180: 
181:     @hash = operating_system_config.merge platform_config
182:     @hash = @hash.merge system_config
183:     @hash = @hash.merge user_config
184: 
185:     # HACK these override command-line args, which is bad
186:     @backtrace        = @hash[:backtrace]        if @hash.key? :backtrace
187:     @benchmark        = @hash[:benchmark]        if @hash.key? :benchmark
188:     @bulk_threshold   = @hash[:bulk_threshold]   if @hash.key? :bulk_threshold
189:     @home             = @hash[:gemhome]          if @hash.key? :gemhome
190:     @path             = @hash[:gempath]          if @hash.key? :gempath
191:     @update_sources   = @hash[:update_sources]   if @hash.key? :update_sources
192:     @verbose          = @hash[:verbose]          if @hash.key? :verbose
193: 
194:     load_api_keys
195: 
196:     Gem.sources = @hash[:sources] if @hash.key? :sources
197:     handle_arguments arg_list
198:   end

Public Instance methods

Return the configuration information for key.

[Source]

     # File lib/rubygems/config_file.rb, line 335
335:   def [](key)
336:     @hash[key.to_s]
337:   end

Set configuration option key to value.

[Source]

     # File lib/rubygems/config_file.rb, line 340
340:   def []=(key, value)
341:     @hash[key.to_s] = value
342:   end

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

[Source]

     # File lib/rubygems/config_file.rb, line 244
244:   def backtrace
245:     @backtrace or $DEBUG
246:   end

The name of the configuration file.

[Source]

     # File lib/rubygems/config_file.rb, line 249
249:   def config_file_name
250:     @config_file_name || Gem.config_file
251:   end

Location of RubyGems.org credentials

[Source]

     # File lib/rubygems/config_file.rb, line 203
203:   def credentials_path
204:     File.join(Gem.user_home, '.gem', 'credentials')
205:   end

Delegates to @hash

[Source]

     # File lib/rubygems/config_file.rb, line 254
254:   def each(&block)
255:     hash = @hash.dup
256:     hash.delete :update_sources
257:     hash.delete :verbose
258:     hash.delete :benchmark
259:     hash.delete :backtrace
260:     hash.delete :bulk_threshold
261: 
262:     yield :update_sources, @update_sources
263:     yield :verbose, @verbose
264:     yield :benchmark, @benchmark
265:     yield :backtrace, @backtrace
266:     yield :bulk_threshold, @bulk_threshold
267: 
268:     yield 'config_file_name', @config_file_name if @config_file_name
269: 
270:     hash.each(&block)
271:   end

Handle the command arguments.

[Source]

     # File lib/rubygems/config_file.rb, line 274
274:   def handle_arguments(arg_list)
275:     @args = []
276: 
277:     arg_list.each do |arg|
278:       case arg
279:       when /^--(backtrace|traceback)$/ then
280:         @backtrace = true
281:       when /^--bench(mark)?$/ then
282:         @benchmark = true
283:       when /^--debug$/ then
284:         $DEBUG = true
285:       else
286:         @args << arg
287:       end
288:     end
289:   end

[Source]

     # File lib/rubygems/config_file.rb, line 207
207:   def load_api_keys
208:     @api_keys = File.exists?(credentials_path) ? load_file(credentials_path) : @hash
209:     if @api_keys.key? :rubygems_api_key then
210:       @rubygems_api_key = @api_keys[:rubygems_api_key]
211:       @api_keys[:rubygems] = @api_keys.delete :rubygems_api_key unless @api_keys.key? :rubygems
212:     end
213:   end

[Source]

     # File lib/rubygems/config_file.rb, line 230
230:   def load_file(filename)
231:     Gem.load_yaml
232: 
233:     return {} unless filename and File.exists?(filename)
234:     begin
235:       YAML.load(File.read(filename))
236:     rescue ArgumentError
237:       warn "Failed to load #{config_file_name}"
238:     rescue Errno::EACCES
239:       warn "Failed to load #{config_file_name} due to permissions problem."
240:     end or {}
241:   end

Really verbose mode gives you extra output.

[Source]

     # File lib/rubygems/config_file.rb, line 292
292:   def really_verbose
293:     case verbose
294:     when true, false, nil then false
295:     else true
296:     end
297:   end

[Source]

     # File lib/rubygems/config_file.rb, line 215
215:   def rubygems_api_key=(api_key)
216:     config = load_file(credentials_path).merge(:rubygems_api_key => api_key)
217: 
218:     dirname = File.dirname(credentials_path)
219:     Dir.mkdir(dirname) unless File.exists?(dirname)
220: 
221:     Gem.load_yaml
222: 
223:     File.open(credentials_path, 'w') do |f|
224:       f.write config.to_yaml
225:     end
226: 
227:     @rubygems_api_key = api_key
228:   end

Writes out this config file, replacing its source.

[Source]

     # File lib/rubygems/config_file.rb, line 328
328:   def write
329:     open config_file_name, 'w' do |io|
330:       io.write to_yaml
331:     end
332:   end

[Validate]