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 339
339:   def [](key)
340:     @hash[key.to_s]
341:   end

Set configuration option key to value.

[Source]

     # File lib/rubygems/config_file.rb, line 344
344:   def []=(key, value)
345:     @hash[key.to_s] = value
346:   end

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

[Source]

     # File lib/rubygems/config_file.rb, line 248
248:   def backtrace
249:     @backtrace or $DEBUG
250:   end

The name of the configuration file.

[Source]

     # File lib/rubygems/config_file.rb, line 253
253:   def config_file_name
254:     @config_file_name || Gem.config_file
255:   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 258
258:   def each(&block)
259:     hash = @hash.dup
260:     hash.delete :update_sources
261:     hash.delete :verbose
262:     hash.delete :benchmark
263:     hash.delete :backtrace
264:     hash.delete :bulk_threshold
265: 
266:     yield :update_sources, @update_sources
267:     yield :verbose, @verbose
268:     yield :benchmark, @benchmark
269:     yield :backtrace, @backtrace
270:     yield :bulk_threshold, @bulk_threshold
271: 
272:     yield 'config_file_name', @config_file_name if @config_file_name
273: 
274:     hash.each(&block)
275:   end

Handle the command arguments.

[Source]

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

[Source]

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

[Source]

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

Really verbose mode gives you extra output.

[Source]

     # File lib/rubygems/config_file.rb, line 296
296:   def really_verbose
297:     case verbose
298:     when true, false, nil then false
299:     else true
300:     end
301:   end

[Source]

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

Writes out this config file, replacing its source.

[Source]

     # File lib/rubygems/config_file.rb, line 332
332:   def write
333:     open config_file_name, 'w' do |io|
334:       io.write to_yaml
335:     end
336:   end

[Validate]