Class: Tor::Config
- Inherits:
-
Object
- Object
- Tor::Config
- Defined in:
- lib/tor/config.rb
Overview
Tor configuration.
Constant Summary collapse
- CONFDIR =
'/etc/tor'
Class Method Summary collapse
-
.load(filename, options = {}) ⇒ Config
Loads the configuration options from a Tor configuration file.
-
.open(filename, options = {}) {|config| ... } ⇒ Config
Opens a Tor configuration file.
Instance Method Summary collapse
-
#<<(kv) ⇒ Config
Appends a new configuration option.
-
#[](key) ⇒ String
Looks up the last value of a particular configuration option.
-
#each(key = nil) {|key, value| ... } ⇒ Enumerator
Enumerates configuration options.
-
#initialize(options = {}) {|config| ... } ⇒ Config
constructor
A new instance of Config.
Constructor Details
#initialize(options = {}) {|config| ... } ⇒ Config
Returns a new instance of Config.
65 66 67 68 |
# File 'lib/tor/config.rb', line 65 def initialize( = {}, &block) @lines, @options = [], .dup block.call(self) if block_given? end |
Class Method Details
.load(filename, options = {}) ⇒ Config
Loads the configuration options from a Tor configuration file.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/tor/config.rb', line 45 def self.load(filename, = {}) self.new() do |config| File.open(filename.to_s, 'rb') do |file| file.each_line do |line| case line = line.strip.chomp.strip when '' then next # skip empty lines when /^#/ then next # skip comments else line = line.split('#').first.strip end # TODO: support for unquoting and unescaping values config << line.split(/\s+/, 2) end end end end |
.open(filename, options = {}) {|config| ... } ⇒ Config
Opens a Tor configuration file.
31 32 33 34 35 36 37 |
# File 'lib/tor/config.rb', line 31 def self.open(filename, = {}, &block) if block_given? block.call(self.load(filename, )) else self.load(filename, ) end end |
Instance Method Details
#<<(kv) ⇒ Config
Appends a new configuration option.
75 76 77 78 |
# File 'lib/tor/config.rb', line 75 def <<(kv) @lines << kv self end |
#[](key) ⇒ String
Looks up the last value of a particular configuration option.
85 86 87 88 |
# File 'lib/tor/config.rb', line 85 def [](key) values = each(key).map(&:last) values.empty? ? nil : values.last end |
#each(key = nil) {|key, value| ... } ⇒ Enumerator
Enumerates configuration options.
98 99 100 101 |
# File 'lib/tor/config.rb', line 98 def each(key = nil, &block) return enum_for(:each, key) unless block_given? key ? @lines.find_all { |k, v| key === k }.each(&block) : @lines.each(&block) end |