Class: OpenSSL::Config

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/1.8/openssl/config.rb,
lib/1.9/openssl/config.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename = nil) ⇒ Config

Returns a new instance of Config.



215
216
217
218
219
220
221
222
223
224
# File 'lib/1.8/openssl/config.rb', line 215

def initialize(filename = nil)
  @data = {}
  if filename
    File.open(filename.to_s) do |file|
      Config.parse_config(file).each do |section, hash|
        self[section] = hash
      end
    end
  end
end

Class Method Details

.get_key_string(data, section, key) ⇒ Object

:nodoc:



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/1.8/openssl/config.rb', line 42

def get_key_string(data, section, key) # :nodoc:
  if v = data[section] && data[section][key]
    return v
  elsif section == 'ENV'
    if v = ENV[key]
      return v
    end
  end
  if v = data['default'] && data['default'][key]
    return v
  end
end

.parse(str) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/1.8/openssl/config.rb', line 23

def parse(str)
  c = new()
  parse_config(StringIO.new(str)).each do |section, hash|
    c[section] = hash
  end
  c
end

.parse_config(io) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/1.8/openssl/config.rb', line 33

def parse_config(io)
  begin
    parse_config_lines(io)
  rescue ConfigError => e
    e.message.replace("error in line #{io.lineno}: " + e.message)
    raise
  end
end

Instance Method Details

#[](section) ⇒ Object



251
252
253
# File 'lib/1.8/openssl/config.rb', line 251

def [](section)
  @data[section] || {}
end

#[]=(section, pairs) ⇒ Object



260
261
262
263
264
265
266
# File 'lib/1.8/openssl/config.rb', line 260

def []=(section, pairs)
  check_modify
  @data[section] ||= {}
  pairs.each do |key, value|
    self.add_value(section, key, value)
  end
end

#add_value(section, key, value) ⇒ Object



246
247
248
249
# File 'lib/1.8/openssl/config.rb', line 246

def add_value(section, key, value)
  check_modify
  (@data[section] ||= {})[key] = value
end

#eachObject



284
285
286
287
288
289
290
# File 'lib/1.8/openssl/config.rb', line 284

def each
  @data.each do |section, hash|
    hash.each do |key, value|
      yield(section, key, value)
    end
  end
end

#get_value(section, key) ⇒ Object



226
227
228
229
230
231
232
# File 'lib/1.8/openssl/config.rb', line 226

def get_value(section, key)
  if section.nil?
    raise TypeError.new('nil not allowed')
  end
  section = 'default' if section.empty?
  get_key_string(section, key)
end

#inspectObject



292
293
294
# File 'lib/1.8/openssl/config.rb', line 292

def inspect
  "#<#{self.class.name} sections=#{sections.inspect}>"
end

#section(name) ⇒ Object



255
256
257
258
# File 'lib/1.8/openssl/config.rb', line 255

def section(name)
  warn('Config#section is deprecated; use Config#[]')
  @data[name] || {}
end

#sectionsObject



268
269
270
# File 'lib/1.8/openssl/config.rb', line 268

def sections
  @data.keys
end

#to_sObject



272
273
274
275
276
277
278
279
280
281
282
# File 'lib/1.8/openssl/config.rb', line 272

def to_s
  ary = []
  @data.keys.sort.each do |section|
    ary << "[ #{section} ]\n"
    @data[section].keys.each do |key|
      ary << "#{key}=#{@data[section][key]}\n"
    end
    ary << "\n"
  end
  ary.join
end

#value(arg1, arg2 = nil) ⇒ Object



234
235
236
237
238
239
240
241
242
243
244
# File 'lib/1.8/openssl/config.rb', line 234

def value(arg1, arg2 = nil)
  warn('Config#value is deprecated; use Config#get_value')
  if arg2.nil?
    section, key = 'default', arg1
  else
    section, key = arg1, arg2
  end
  section ||= 'default'
  section = 'default' if section.empty?
  get_key_string(section, key)
end