In Files

  • rubygems/config_file.rb

Class/Module Index [+]

Quicksearch

Gem::ConfigFile

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

Constants

DEFAULT_BACKTRACE
DEFAULT_BENCHMARK
DEFAULT_BULK_THRESHOLD
DEFAULT_UPDATE_SOURCES
DEFAULT_VERBOSITY
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

SYSTEM_WIDE_CONFIG_FILE

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

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

new(arg_list) click to toggle source

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.

 
               # File rubygems/config_file.rb, line 136
def initialize(arg_list)
  @config_file_name = nil
  need_config_file_name = false

  arg_list = arg_list.map do |arg|
    if need_config_file_name then
      @config_file_name = arg
      need_config_file_name = false
      nil
    elsif arg =~ /^--config-file=(.*)/ then
      @config_file_name = $1
      nil
    elsif arg =~ /^--config-file$/ then
      need_config_file_name = true
      nil
    else
      arg
    end
  end.compact

  @backtrace = DEFAULT_BACKTRACE
  @benchmark = DEFAULT_BENCHMARK
  @bulk_threshold = DEFAULT_BULK_THRESHOLD
  @verbose = DEFAULT_VERBOSITY
  @update_sources = DEFAULT_UPDATE_SOURCES

  operating_system_config = Marshal.load Marshal.dump(OPERATING_SYSTEM_DEFAULTS)
  platform_config = Marshal.load Marshal.dump(PLATFORM_DEFAULTS)
  system_config = load_file SYSTEM_WIDE_CONFIG_FILE
  user_config = load_file config_file_name.dup.untaint

  @hash = operating_system_config.merge platform_config
  @hash = @hash.merge system_config
  @hash = @hash.merge user_config

  # HACK these override command-line args, which is bad
  @backtrace        = @hash[:backtrace]        if @hash.key? :backtrace
  @benchmark        = @hash[:benchmark]        if @hash.key? :benchmark
  @bulk_threshold   = @hash[:bulk_threshold]   if @hash.key? :bulk_threshold
  @home             = @hash[:gemhome]          if @hash.key? :gemhome
  @path             = @hash[:gempath]          if @hash.key? :gempath
  @update_sources   = @hash[:update_sources]   if @hash.key? :update_sources
  @verbose          = @hash[:verbose]          if @hash.key? :verbose
  @ssl_verify_mode  = @hash[:ssl_verify_mode]  if @hash.key? :ssl_verify_mode
  @ssl_ca_cert      = @hash[:ssl_ca_cert]      if @hash.key? :ssl_ca_cert

  load_rubygems_api_key

  Gem.sources = @hash[:sources] if @hash.key? :sources
  handle_arguments arg_list
end
            

Public Instance Methods

[](key) click to toggle source

Return the configuration information for key.

 
               # File rubygems/config_file.rb, line 321
def [](key)
  @hash[key.to_s]
end
            
[]=(key, value) click to toggle source

Set configuration option key to value.

 
               # File rubygems/config_file.rb, line 326
def []=(key, value)
  @hash[key.to_s] = value
end
            
backtrace() click to toggle source

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

 
               # File rubygems/config_file.rb, line 229
def backtrace
  @backtrace or $DEBUG
end
            
config_file_name() click to toggle source

The name of the configuration file.

 
               # File rubygems/config_file.rb, line 234
def config_file_name
  @config_file_name || Gem.config_file
end
            
credentials_path() click to toggle source

Location of RubyGems.org credentials

 
               # File rubygems/config_file.rb, line 191
def credentials_path
  File.join(Gem.user_home, '.gem', 'credentials')
end
            
each(&block) click to toggle source

Delegates to @hash

 
               # File rubygems/config_file.rb, line 239
def each(&block)
  hash = @hash.dup
  hash.delete :update_sources
  hash.delete :verbose
  hash.delete :benchmark
  hash.delete :backtrace
  hash.delete :bulk_threshold

  yield :update_sources, @update_sources
  yield :verbose, @verbose
  yield :benchmark, @benchmark
  yield :backtrace, @backtrace
  yield :bulk_threshold, @bulk_threshold

  yield 'config_file_name', @config_file_name if @config_file_name

  hash.each(&block)
end
            
handle_arguments(arg_list) click to toggle source

Handle the command arguments.

 
               # File rubygems/config_file.rb, line 259
def handle_arguments(arg_list)
  @args = []

  arg_list.each do |arg|
    case arg
    when /^--(backtrace|traceback)$/ then
      @backtrace = true
    when /^--bench(mark)?$/ then
      @benchmark = true
    when /^--debug$/ then
      $DEBUG = true
    else
      @args << arg
    end
  end
end
            
load_file(filename) click to toggle source
 
               # File rubygems/config_file.rb, line 216
def load_file(filename)
  return {} unless filename and File.exists?(filename)
  begin
    require 'yaml'
    YAML.load(File.read(filename))
  rescue ArgumentError
    warn "Failed to load #{config_file_name}"
  rescue Errno::EACCES
    warn "Failed to load #{config_file_name} due to permissions problem."
  end or {}
end
            
load_rubygems_api_key() click to toggle source
 
               # File rubygems/config_file.rb, line 195
def load_rubygems_api_key
  api_key_hash = File.exists?(credentials_path) ? load_file(credentials_path) : @hash

  @rubygems_api_key = api_key_hash[:rubygems_api_key] if api_key_hash.key? :rubygems_api_key
end
            
really_verbose() click to toggle source

Really verbose mode gives you extra output.

 
               # File rubygems/config_file.rb, line 277
def really_verbose
  case verbose
  when true, false, nil then false
  else true
  end
end
            
rubygems_api_key=(api_key) click to toggle source
 
               # File rubygems/config_file.rb, line 201
def rubygems_api_key=(api_key)
  config = load_file(credentials_path).merge(:rubygems_api_key => api_key)

  dirname = File.dirname(credentials_path)
  Dir.mkdir(dirname) unless File.exists?(dirname)

  require 'yaml'

  File.open(credentials_path, 'w') do |f|
    f.write config.to_yaml
  end

  @rubygems_api_key = api_key
end
            
write() click to toggle source

Writes out this config file, replacing its source.

 
               # File rubygems/config_file.rb, line 313
def write
  require 'yaml'
  open config_file_name, 'w' do |io|
    io.write to_yaml
  end
end
            

Commenting is here to help enhance the documentation. For example, code samples, or clarification of the documentation.

If you have questions about Ruby or the documentation, please post to one of the Ruby mailing lists. You will get better, faster, help that way.

If you wish to post a correction of the docs, please do so, but also file bug report so that it can be corrected for the next release. Thank you.

If you want to help improve the Ruby documentation, please visit Documenting-ruby.org.

blog comments powered by Disqus