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

Store the gem command options specified in the configuration file. The config file object acts much like a hash.

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', '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.
hash  [R] 
home  [RW] 
path  [RW]  Where to look for gems
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 and —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 91
 91:   def initialize(arg_list)
 92:     @config_file_name = nil
 93:     need_config_file_name = false
 94: 
 95:     arg_list = arg_list.map do |arg|
 96:       if need_config_file_name then
 97:         @config_file_name = arg
 98:         need_config_file_name = false
 99:         nil
100:       elsif arg =~ /^--config-file=(.*)/ then
101:         @config_file_name = $1
102:         nil
103:       elsif arg =~ /^--config-file$/ then
104:         need_config_file_name = true
105:         nil
106:       else
107:         arg
108:       end
109:     end.compact
110: 
111:     @backtrace = DEFAULT_BACKTRACE
112:     @benchmark = DEFAULT_BENCHMARK
113:     @bulk_threshold = DEFAULT_BULK_THRESHOLD
114:     @verbose = DEFAULT_VERBOSITY
115:     @update_sources = DEFAULT_UPDATE_SOURCES
116: 
117:     operating_system_config = Marshal.load Marshal.dump(OPERATING_SYSTEM_DEFAULTS)
118:     platform_config = Marshal.load Marshal.dump(PLATFORM_DEFAULTS)
119:     system_config = load_file SYSTEM_WIDE_CONFIG_FILE
120:     user_config = load_file config_file_name.dup.untaint
121: 
122:     @hash = operating_system_config.merge platform_config
123:     @hash = @hash.merge system_config
124:     @hash = @hash.merge user_config
125: 
126:     # HACK these override command-line args, which is bad
127:     @backtrace = @hash[:backtrace] if @hash.key? :backtrace
128:     @benchmark = @hash[:benchmark] if @hash.key? :benchmark
129:     @bulk_threshold = @hash[:bulk_threshold] if @hash.key? :bulk_threshold
130:     Gem.sources = @hash[:sources] if @hash.key? :sources
131:     @verbose = @hash[:verbose] if @hash.key? :verbose
132:     @update_sources = @hash[:update_sources] if @hash.key? :update_sources
133:     @path = @hash[:gempath] if @hash.key? :gempath
134:     @home = @hash[:gemhome] if @hash.key? :gemhome
135: 
136:     handle_arguments arg_list
137:   end

Public Instance methods

Return the configuration information for key.

[Source]

     # File lib/rubygems/config_file.rb, line 241
241:   def [](key)
242:     @hash[key.to_s]
243:   end

Set configuration option key to value.

[Source]

     # File lib/rubygems/config_file.rb, line 246
246:   def []=(key, value)
247:     @hash[key.to_s] = value
248:   end

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

[Source]

     # File lib/rubygems/config_file.rb, line 150
150:   def backtrace
151:     @backtrace or $DEBUG
152:   end

The name of the configuration file.

[Source]

     # File lib/rubygems/config_file.rb, line 155
155:   def config_file_name
156:     @config_file_name || Gem.config_file
157:   end

Delegates to @hash

[Source]

     # File lib/rubygems/config_file.rb, line 160
160:   def each(&block)
161:     hash = @hash.dup
162:     hash.delete :update_sources
163:     hash.delete :verbose
164:     hash.delete :benchmark
165:     hash.delete :backtrace
166:     hash.delete :bulk_threshold
167: 
168:     yield :update_sources, @update_sources
169:     yield :verbose, @verbose
170:     yield :benchmark, @benchmark
171:     yield :backtrace, @backtrace
172:     yield :bulk_threshold, @bulk_threshold
173: 
174:     yield 'config_file_name', @config_file_name if @config_file_name
175: 
176:     hash.each(&block)
177:   end

Handle the command arguments.

[Source]

     # File lib/rubygems/config_file.rb, line 180
180:   def handle_arguments(arg_list)
181:     @args = []
182: 
183:     arg_list.each do |arg|
184:       case arg
185:       when /^--(backtrace|traceback)$/ then
186:         @backtrace = true
187:       when /^--bench(mark)?$/ then
188:         @benchmark = true
189:       when /^--debug$/ then
190:         $DEBUG = true
191:       else
192:         @args << arg
193:       end
194:     end
195:   end

[Source]

     # File lib/rubygems/config_file.rb, line 139
139:   def load_file(filename)
140:     begin
141:       YAML.load(File.read(filename)) if filename and File.exist?(filename)
142:     rescue ArgumentError
143:       warn "Failed to load #{config_file_name}"
144:     rescue Errno::EACCES
145:       warn "Failed to load #{config_file_name} due to permissions problem."
146:     end or {}
147:   end

Really verbose mode gives you extra output.

[Source]

     # File lib/rubygems/config_file.rb, line 198
198:   def really_verbose
199:     case verbose
200:     when true, false, nil then false
201:     else true
202:     end
203:   end

Writes out this config file, replacing its source.

[Source]

     # File lib/rubygems/config_file.rb, line 234
234:   def write
235:     File.open config_file_name, 'w' do |fp|
236:       fp.write self.to_yaml
237:     end
238:   end

[Validate]