Class: Conf::Configuration
- Inherits:
-
Object
- Object
- Conf::Configuration
show all
- Includes:
- ConfigValue
- Defined in:
- lib/conf.rb
Instance Method Summary
collapse
#__setup__, create, #method_missing
Constructor Details
#initialize(parent = nil) ⇒ Configuration
Returns a new instance of Configuration.
93
94
95
96
97
98
99
100
101
102
|
# File 'lib/conf.rb', line 93
def initialize(parent = nil)
if parent and not parent.kind_of? self.class
raise TypeError, "expected #{self.class}, got #{parent.inspect}:#{parent.class}"
end
@parent = parent
@data = {}
@locked = false
@__root__ = self
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
in the class Conf::ConfigValue
Instance Method Details
#[](key) ⇒ Object
161
162
163
164
165
166
167
168
169
|
# File 'lib/conf.rb', line 161
def [](key)
val = @data[key]
if val.nil?
@parent && @parent[key]
else
val
end
end
|
#[]=(key, value) ⇒ Object
171
172
173
|
# File 'lib/conf.rb', line 171
def []=(key, value)
@data[key] = value
end
|
#check_lock ⇒ Object
122
123
124
125
126
|
# File 'lib/conf.rb', line 122
def check_lock
if locked?
raise InvalidStateError, "config is locked #{@data.keys.inspect}"
end
end
|
#data ⇒ Object
149
|
# File 'lib/conf.rb', line 149
def data() @data end
|
#edit(&blk) ⇒ Object
128
129
130
131
132
|
# File 'lib/conf.rb', line 128
def edit(&blk)
edit!
instance_eval(&blk)
done!
end
|
#fetch(key, &blk) ⇒ Object
151
152
153
154
155
156
157
158
|
# File 'lib/conf.rb', line 151
def fetch(key, &blk)
val = self[key]
if val.nil?
@data[key] = yield(key)
else
val
end
end
|
#key?(key) ⇒ Boolean
104
105
106
|
# File 'lib/conf.rb', line 104
def key?(key)
@data.key?(key) || (@parent && @parent.key?(key))
end
|
#lock! ⇒ Object
108
109
110
|
# File 'lib/conf.rb', line 108
def lock!
@locked = true
end
|
#locked? ⇒ Boolean
134
135
136
|
# File 'lib/conf.rb', line 134
def locked?
@locked
end
|
#section(start_key) ⇒ Object
138
139
140
141
142
143
144
145
146
147
|
# File 'lib/conf.rb', line 138
def section(start_key)
result = @parent ? @parent.section(start_key) : {}
rx = /^#{Regexp.escape(start_key).gsub("\\*", ".+?")}/
@data.each do |key, value|
result[key] = value if key =~ rx and not value.instance_of? Object
end
result
end
|
#unlock! ⇒ Object
112
113
114
|
# File 'lib/conf.rb', line 112
def unlock!
@locked = false
end
|
#unlocked(&blk) ⇒ Object
116
117
118
119
120
|
# File 'lib/conf.rb', line 116
def unlocked(&blk)
unlock!
yield
lock!
end
|