Class: OpenSSL::Config

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

Constant Summary collapse

DEFAULT_CONFIG_FILE =
rb_str_new2(default_config_file)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename = nil) ⇒ Config

Returns a new instance of Config


212
213
214
215
216
217
218
219
220
221
# File 'lib/openssl/config.rb', line 212

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:


39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/openssl/config.rb', line 39

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


20
21
22
23
24
25
26
# File 'lib/openssl/config.rb', line 20

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

.parse_config(io) ⇒ Object


30
31
32
33
34
35
36
37
# File 'lib/openssl/config.rb', line 30

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


248
249
250
# File 'lib/openssl/config.rb', line 248

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

#[]=(section, pairs) ⇒ Object


257
258
259
260
261
262
263
# File 'lib/openssl/config.rb', line 257

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


243
244
245
246
# File 'lib/openssl/config.rb', line 243

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

#eachObject


281
282
283
284
285
286
287
# File 'lib/openssl/config.rb', line 281

def each
  @data.each do |section, hash|
    hash.each do |key, value|
      yield [section, key, value]
    end
  end
end

#get_value(section, key) ⇒ Object


223
224
225
226
227
228
229
# File 'lib/openssl/config.rb', line 223

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


289
290
291
# File 'lib/openssl/config.rb', line 289

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

#section(name) ⇒ Object


252
253
254
255
# File 'lib/openssl/config.rb', line 252

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

#sectionsObject


265
266
267
# File 'lib/openssl/config.rb', line 265

def sections
  @data.keys
end

#to_sObject


269
270
271
272
273
274
275
276
277
278
279
# File 'lib/openssl/config.rb', line 269

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


231
232
233
234
235
236
237
238
239
240
241
# File 'lib/openssl/config.rb', line 231

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