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

Public Instance methods

Return the configuration information for key.

[Source]

     # File lib/rubygems/config_file.rb, line 322
322:   def [](key)
323:     @hash[key.to_s]
324:   end

Set configuration option key to value.

[Source]

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

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

[Source]

     # File lib/rubygems/config_file.rb, line 230
230:   def backtrace
231:     @backtrace or $DEBUG
232:   end

The name of the configuration file.

[Source]

     # File lib/rubygems/config_file.rb, line 235
235:   def config_file_name
236:     @config_file_name || Gem.config_file
237:   end

Location of RubyGems.org credentials

[Source]

     # File lib/rubygems/config_file.rb, line 192
192:   def credentials_path
193:     File.join(Gem.user_home, '.gem', 'credentials')
194:   end

Delegates to @hash

[Source]

     # File lib/rubygems/config_file.rb, line 240
240:   def each(&block)
241:     hash = @hash.dup
242:     hash.delete :update_sources
243:     hash.delete :verbose
244:     hash.delete :benchmark
245:     hash.delete :backtrace
246:     hash.delete :bulk_threshold
247: 
248:     yield :update_sources, @update_sources
249:     yield :verbose, @verbose
250:     yield :benchmark, @benchmark
251:     yield :backtrace, @backtrace
252:     yield :bulk_threshold, @bulk_threshold
253: 
254:     yield 'config_file_name', @config_file_name if @config_file_name
255: 
256:     hash.each(&block)
257:   end

Handle the command arguments.

[Source]

     # File lib/rubygems/config_file.rb, line 260
260:   def handle_arguments(arg_list)
261:     @args = []
262: 
263:     arg_list.each do |arg|
264:       case arg
265:       when /^--(backtrace|traceback)$/ then
266:         @backtrace = true
267:       when /^--bench(mark)?$/ then
268:         @benchmark = true
269:       when /^--debug$/ then
270:         $DEBUG = true
271:       else
272:         @args << arg
273:       end
274:     end
275:   end

[Source]

     # File lib/rubygems/config_file.rb, line 217
217:   def load_file(filename)
218:     return {} unless filename and File.exists?(filename)
219:     begin
220:       require 'yaml'
221:       YAML.load(File.read(filename))
222:     rescue ArgumentError
223:       warn "Failed to load #{config_file_name}"
224:     rescue Errno::EACCES
225:       warn "Failed to load #{config_file_name} due to permissions problem."
226:     end or {}
227:   end

[Source]

     # File lib/rubygems/config_file.rb, line 196
196:   def load_rubygems_api_key
197:     api_key_hash = File.exists?(credentials_path) ? load_file(credentials_path) : @hash
198: 
199:     @rubygems_api_key = api_key_hash[:rubygems_api_key] if api_key_hash.key? :rubygems_api_key
200:   end

Really verbose mode gives you extra output.

[Source]

     # File lib/rubygems/config_file.rb, line 278
278:   def really_verbose
279:     case verbose
280:     when true, false, nil then false
281:     else true
282:     end
283:   end

[Source]

     # File lib/rubygems/config_file.rb, line 202
202:   def rubygems_api_key=(api_key)
203:     config = load_file(credentials_path).merge(:rubygems_api_key => api_key)
204: 
205:     dirname = File.dirname(credentials_path)
206:     Dir.mkdir(dirname) unless File.exists?(dirname)
207: 
208:     require 'yaml'
209: 
210:     File.open(credentials_path, 'w') do |f|
211:       f.write config.to_yaml
212:     end
213: 
214:     @rubygems_api_key = api_key
215:   end

Writes out this config file, replacing its source.

[Source]

     # File lib/rubygems/config_file.rb, line 314
314:   def write
315:     require 'yaml'
316:     open config_file_name, 'w' do |io|
317:       io.write to_yaml
318:     end
319:   end

[Validate]