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

Public Instance methods

Return the configuration information for key.

[Source]

     # File lib/rubygems/config_file.rb, line 248
248:   def [](key)
249:     @hash[key.to_s]
250:   end

Set configuration option key to value.

[Source]

     # File lib/rubygems/config_file.rb, line 253
253:   def []=(key, value)
254:     @hash[key.to_s] = value
255:   end

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

[Source]

     # File lib/rubygems/config_file.rb, line 157
157:   def backtrace
158:     @backtrace or $DEBUG
159:   end

The name of the configuration file.

[Source]

     # File lib/rubygems/config_file.rb, line 162
162:   def config_file_name
163:     @config_file_name || Gem.config_file
164:   end

Delegates to @hash

[Source]

     # File lib/rubygems/config_file.rb, line 167
167:   def each(&block)
168:     hash = @hash.dup
169:     hash.delete :update_sources
170:     hash.delete :verbose
171:     hash.delete :benchmark
172:     hash.delete :backtrace
173:     hash.delete :bulk_threshold
174: 
175:     yield :update_sources, @update_sources
176:     yield :verbose, @verbose
177:     yield :benchmark, @benchmark
178:     yield :backtrace, @backtrace
179:     yield :bulk_threshold, @bulk_threshold
180: 
181:     yield 'config_file_name', @config_file_name if @config_file_name
182: 
183:     hash.each(&block)
184:   end

Handle the command arguments.

[Source]

     # File lib/rubygems/config_file.rb, line 187
187:   def handle_arguments(arg_list)
188:     @args = []
189: 
190:     arg_list.each do |arg|
191:       case arg
192:       when /^--(backtrace|traceback)$/ then
193:         @backtrace = true
194:       when /^--bench(mark)?$/ then
195:         @benchmark = true
196:       when /^--debug$/ then
197:         $DEBUG = true
198:       else
199:         @args << arg
200:       end
201:     end
202:   end

[Source]

     # File lib/rubygems/config_file.rb, line 146
146:   def load_file(filename)
147:     begin
148:       YAML.load(File.read(filename)) if filename and File.exist?(filename)
149:     rescue ArgumentError
150:       warn "Failed to load #{config_file_name}"
151:     rescue Errno::EACCES
152:       warn "Failed to load #{config_file_name} due to permissions problem."
153:     end or {}
154:   end

Really verbose mode gives you extra output.

[Source]

     # File lib/rubygems/config_file.rb, line 205
205:   def really_verbose
206:     case verbose
207:     when true, false, nil then false
208:     else true
209:     end
210:   end

Writes out this config file, replacing its source.

[Source]

     # File lib/rubygems/config_file.rb, line 241
241:   def write
242:     File.open config_file_name, 'w' do |fp|
243:       fp.write self.to_yaml
244:     end
245:   end

[Validate]