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

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
ssl_ca_cert  [R]  Path name of directory or file of openssl CA certificate, used for remote https connection
ssl_verify_mode  [R]  openssl verify mode value, used for remote https connection
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 162
162:   def initialize(arg_list)
163:     @config_file_name = nil
164:     need_config_file_name = false
165: 
166:     arg_list = arg_list.map do |arg|
167:       if need_config_file_name then
168:         @config_file_name = arg
169:         need_config_file_name = false
170:         nil
171:       elsif arg =~ /^--config-file=(.*)/ then
172:         @config_file_name = $1
173:         nil
174:       elsif arg =~ /^--config-file$/ then
175:         need_config_file_name = true
176:         nil
177:       else
178:         arg
179:       end
180:     end.compact
181: 
182:     @backtrace = DEFAULT_BACKTRACE
183:     @benchmark = DEFAULT_BENCHMARK
184:     @bulk_threshold = DEFAULT_BULK_THRESHOLD
185:     @verbose = DEFAULT_VERBOSITY
186:     @update_sources = DEFAULT_UPDATE_SOURCES
187: 
188:     operating_system_config = Marshal.load Marshal.dump(OPERATING_SYSTEM_DEFAULTS)
189:     platform_config = Marshal.load Marshal.dump(PLATFORM_DEFAULTS)
190:     system_config = load_file SYSTEM_WIDE_CONFIG_FILE
191:     user_config = load_file config_file_name.dup.untaint
192: 
193:     @hash = operating_system_config.merge platform_config
194:     @hash = @hash.merge system_config
195:     @hash = @hash.merge user_config
196: 
197:     # HACK these override command-line args, which is bad
198:     @backtrace        = @hash[:backtrace]        if @hash.key? :backtrace
199:     @benchmark        = @hash[:benchmark]        if @hash.key? :benchmark
200:     @bulk_threshold   = @hash[:bulk_threshold]   if @hash.key? :bulk_threshold
201:     @home             = @hash[:gemhome]          if @hash.key? :gemhome
202:     @path             = @hash[:gempath]          if @hash.key? :gempath
203:     @update_sources   = @hash[:update_sources]   if @hash.key? :update_sources
204:     @verbose          = @hash[:verbose]          if @hash.key? :verbose
205:     @ssl_verify_mode  = @hash[:ssl_verify_mode]  if @hash.key? :ssl_verify_mode
206:     @ssl_ca_cert      = @hash[:ssl_ca_cert]      if @hash.key? :ssl_ca_cert
207: 
208:     load_api_keys
209: 
210:     Gem.sources = @hash[:sources] if @hash.key? :sources
211:     handle_arguments arg_list
212:   end

Public Instance methods

Return the configuration information for key.

[Source]

     # File lib/rubygems/config_file.rb, line 353
353:   def [](key)
354:     @hash[key.to_s]
355:   end

Set configuration option key to value.

[Source]

     # File lib/rubygems/config_file.rb, line 358
358:   def []=(key, value)
359:     @hash[key.to_s] = value
360:   end

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

[Source]

     # File lib/rubygems/config_file.rb, line 262
262:   def backtrace
263:     @backtrace or $DEBUG
264:   end

The name of the configuration file.

[Source]

     # File lib/rubygems/config_file.rb, line 267
267:   def config_file_name
268:     @config_file_name || Gem.config_file
269:   end

Location of RubyGems.org credentials

[Source]

     # File lib/rubygems/config_file.rb, line 217
217:   def credentials_path
218:     File.join Gem.user_home, '.gem', 'credentials'
219:   end

Delegates to @hash

[Source]

     # File lib/rubygems/config_file.rb, line 272
272:   def each(&block)
273:     hash = @hash.dup
274:     hash.delete :update_sources
275:     hash.delete :verbose
276:     hash.delete :benchmark
277:     hash.delete :backtrace
278:     hash.delete :bulk_threshold
279: 
280:     yield :update_sources, @update_sources
281:     yield :verbose, @verbose
282:     yield :benchmark, @benchmark
283:     yield :backtrace, @backtrace
284:     yield :bulk_threshold, @bulk_threshold
285: 
286:     yield 'config_file_name', @config_file_name if @config_file_name
287: 
288:     hash.each(&block)
289:   end

Handle the command arguments.

[Source]

     # File lib/rubygems/config_file.rb, line 292
292:   def handle_arguments(arg_list)
293:     @args = []
294: 
295:     arg_list.each do |arg|
296:       case arg
297:       when /^--(backtrace|traceback)$/ then
298:         @backtrace = true
299:       when /^--bench(mark)?$/ then
300:         @benchmark = true
301:       when /^--debug$/ then
302:         $DEBUG = true
303:       else
304:         @args << arg
305:       end
306:     end
307:   end

[Source]

     # File lib/rubygems/config_file.rb, line 221
221:   def load_api_keys
222:     @api_keys = if File.exist? credentials_path then
223:                   load_file(credentials_path)
224:                 else
225:                   @hash
226:                 end
227:     if @api_keys.key? :rubygems_api_key then
228:       @rubygems_api_key = @api_keys[:rubygems_api_key]
229:       @api_keys[:rubygems] = @api_keys.delete :rubygems_api_key unless @api_keys.key? :rubygems
230:     end
231:   end

[Source]

     # File lib/rubygems/config_file.rb, line 248
248:   def load_file(filename)
249:     Gem.load_yaml
250: 
251:     return {} unless filename and File.exist? filename
252:     begin
253:       YAML.load(File.read(filename))
254:     rescue ArgumentError
255:       warn "Failed to load #{config_file_name}"
256:     rescue Errno::EACCES
257:       warn "Failed to load #{config_file_name} due to permissions problem."
258:     end or {}
259:   end

Really verbose mode gives you extra output.

[Source]

     # File lib/rubygems/config_file.rb, line 310
310:   def really_verbose
311:     case verbose
312:     when true, false, nil then false
313:     else true
314:     end
315:   end

[Source]

     # File lib/rubygems/config_file.rb, line 233
233:   def rubygems_api_key=(api_key)
234:     config = load_file(credentials_path).merge(:rubygems_api_key => api_key)
235: 
236:     dirname = File.dirname credentials_path
237:     Dir.mkdir(dirname) unless File.exist? dirname
238: 
239:     Gem.load_yaml
240: 
241:     File.open(credentials_path, 'w') do |f|
242:       f.write config.to_yaml
243:     end
244: 
245:     @rubygems_api_key = api_key
246:   end

Writes out this config file, replacing its source.

[Source]

     # File lib/rubygems/config_file.rb, line 346
346:   def write
347:     open config_file_name, 'w' do |io|
348:       io.write to_yaml
349:     end
350:   end

[Validate]