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

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

Public Instance methods

Return the configuration information for key.

[Source]

     # File lib/rubygems/config_file.rb, line 328
328:   def [](key)
329:     @hash[key.to_s]
330:   end

Set configuration option key to value.

[Source]

     # File lib/rubygems/config_file.rb, line 333
333:   def []=(key, value)
334:     @hash[key.to_s] = value
335:   end

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

[Source]

     # File lib/rubygems/config_file.rb, line 237
237:   def backtrace
238:     @backtrace or $DEBUG
239:   end

The name of the configuration file.

[Source]

     # File lib/rubygems/config_file.rb, line 242
242:   def config_file_name
243:     @config_file_name || Gem.config_file
244:   end

Location of RubyGems.org credentials

[Source]

     # File lib/rubygems/config_file.rb, line 198
198:   def credentials_path
199:     File.join(Gem.user_home, '.gem', 'credentials')
200:   end

Delegates to @hash

[Source]

     # File lib/rubygems/config_file.rb, line 247
247:   def each(&block)
248:     hash = @hash.dup
249:     hash.delete :update_sources
250:     hash.delete :verbose
251:     hash.delete :benchmark
252:     hash.delete :backtrace
253:     hash.delete :bulk_threshold
254: 
255:     yield :update_sources, @update_sources
256:     yield :verbose, @verbose
257:     yield :benchmark, @benchmark
258:     yield :backtrace, @backtrace
259:     yield :bulk_threshold, @bulk_threshold
260: 
261:     yield 'config_file_name', @config_file_name if @config_file_name
262: 
263:     hash.each(&block)
264:   end

Handle the command arguments.

[Source]

     # File lib/rubygems/config_file.rb, line 267
267:   def handle_arguments(arg_list)
268:     @args = []
269: 
270:     arg_list.each do |arg|
271:       case arg
272:       when /^--(backtrace|traceback)$/ then
273:         @backtrace = true
274:       when /^--bench(mark)?$/ then
275:         @benchmark = true
276:       when /^--debug$/ then
277:         $DEBUG = true
278:       else
279:         @args << arg
280:       end
281:     end
282:   end

[Source]

     # File lib/rubygems/config_file.rb, line 223
223:   def load_file(filename)
224:     Gem.load_yaml
225: 
226:     return {} unless filename and File.exists?(filename)
227:     begin
228:       YAML.load(File.read(filename))
229:     rescue ArgumentError
230:       warn "Failed to load #{config_file_name}"
231:     rescue Errno::EACCES
232:       warn "Failed to load #{config_file_name} due to permissions problem."
233:     end or {}
234:   end

[Source]

     # File lib/rubygems/config_file.rb, line 202
202:   def load_rubygems_api_key
203:     api_key_hash = File.exists?(credentials_path) ? load_file(credentials_path) : @hash
204: 
205:     @rubygems_api_key = api_key_hash[:rubygems_api_key] if api_key_hash.key? :rubygems_api_key
206:   end

Really verbose mode gives you extra output.

[Source]

     # File lib/rubygems/config_file.rb, line 285
285:   def really_verbose
286:     case verbose
287:     when true, false, nil then false
288:     else true
289:     end
290:   end

[Source]

     # File lib/rubygems/config_file.rb, line 208
208:   def rubygems_api_key=(api_key)
209:     config = load_file(credentials_path).merge(:rubygems_api_key => api_key)
210: 
211:     dirname = File.dirname(credentials_path)
212:     Dir.mkdir(dirname) unless File.exists?(dirname)
213: 
214:     Gem.load_yaml
215: 
216:     File.open(credentials_path, 'w') do |f|
217:       f.write config.to_yaml
218:     end
219: 
220:     @rubygems_api_key = api_key
221:   end

Writes out this config file, replacing its source.

[Source]

     # File lib/rubygems/config_file.rb, line 321
321:   def write
322:     open config_file_name, 'w' do |io|
323:       io.write to_yaml
324:     end
325:   end

[Validate]