Class: Configuration
- Inherits:
-
Object
show all
- Defined in:
- lib/configuration.rb
Overview
Indexing
Author: Stefan Rusterholz Contact: [email protected]> Version: 1.0.0 Date: 2007-10-12
About
Map configuration keys/data to files
Synopsis
config = Configuration.new("my/config")
config["some/key"] = "Some value"
config["some/key"] = config["some/key"].upcase
Defined Under Namespace
Modules: VERSION
Classes: ConfFile, InvalidKey
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(base, dir_tree = []) ⇒ Configuration
Returns a new instance of Configuration.
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
# File 'lib/configuration.rb', line 58
def initialize(base, dir_tree=[])
@base = File.expand_path(base).freeze
@files = {}
unless File.directory?(@base) && File.exist?("#@base/.configdir.yaml") then
if File.file?(@base) then
raise "Can't create directory #@base because of existing file #@base"
end
if File.exist?(@base) then
raise "Directory #@base already exists and is not a configuration directory"
end
end
unless File.exist?(@base) then
FileUtils.mkdir_p(@base)
File.open("#@base/.configdir.yaml", "w") { |fh|
fh.write({
:created => Time.now,
:modified => Time.now,
:revision => 1,
}.to_yaml)
}
end
setup(dir_tree)
end
|
Instance Attribute Details
#base ⇒ Object
Returns the value of attribute base.
56
57
58
|
# File 'lib/configuration.rb', line 56
def base
@base
end
|
Class Method Details
.escape(name) ⇒ Object
46
47
48
49
|
# File 'lib/configuration.rb', line 46
def escape(name)
name.gsub(/[\x00-\x1f%\x2f\x7f{}()\[\]~*+-]/) { |m| "%%%02x"%m[0] }.sub(/^\x2e/, '%2e')
end
|
.unescape(name) ⇒ Object
51
52
53
|
# File 'lib/configuration.rb', line 51
def unescape(name)
name.gsub(/%[a-f\d]{2}/) { |m| m[1,2].to_i(16).chr }
end
|
Instance Method Details
#[](key) ⇒ Object
84
85
86
87
88
89
|
# File 'lib/configuration.rb', line 84
def [](key)
file, key = split(key)
return nil unless key_exist?(file, key)
load(file) unless key
key ? @files[file][key] : @files[file]
end
|
#[]=(key, value) ⇒ Object
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
# File 'lib/configuration.rb', line 91
def []=(key, value)
file, key = split(key)
load(file)
if key then
if Hash === @files[file] then
@files[file][key] = value
else
@files[file] = {key => value}
end
else
@files[file] = value
end
store(file)
value
end
|
#delete(key) ⇒ Object
113
114
115
116
117
118
119
120
121
122
123
|
# File 'lib/configuration.rb', line 113
def delete(key)
file, key = split(key)
return nil unless key_exist?(file, key) and File.exist?(file)
if key.empty? then
File.delete(file)
else
load(file)
@files[file].delete(key)
store(file)
end
end
|
#exist?(key) ⇒ Boolean
Also known as:
has_key?
107
108
109
110
|
# File 'lib/configuration.rb', line 107
def exist?(key)
file, key = split(key)
key_exist?(file, key)
end
|
#setup(dirs) ⇒ Object
125
126
127
|
# File 'lib/configuration.rb', line 125
def setup(dirs)
FileUtils.mkdir_p(dirs.map { |dir| "#@base/#{dir}" })
end
|