Class: HelpfulConfiguration
- Inherits:
-
Object
- Object
- HelpfulConfiguration
show all
- Defined in:
- lib/helpful_configuration.rb
Defined Under Namespace
Classes: ConfigurationMissingError
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(configuration = {}, file = nil) ⇒ HelpfulConfiguration
Returns a new instance of HelpfulConfiguration.
18
19
20
21
22
|
# File 'lib/helpful_configuration.rb', line 18
def initialize(configuration = {}, file=nil)
@configuration = configuration
@explanation = {}
self.file = file
end
|
Instance Attribute Details
#file ⇒ Object
Returns the value of attribute file.
2
3
4
|
# File 'lib/helpful_configuration.rb', line 2
def file
@file
end
|
Class Method Details
.from_file(file, keys) ⇒ Object
7
8
9
10
11
12
13
14
15
16
|
# File 'lib/helpful_configuration.rb', line 7
def self.from_file(file, keys)
config = new
config.file = File.expand_path(file)
if File.exists?(file)
JSON.parse(File.read(file)).each do |key, value|
config[key] = value
end
end
config
end
|
Instance Method Details
#[](key) ⇒ Object
24
25
26
27
28
29
30
31
32
33
34
35
|
# File 'lib/helpful_configuration.rb', line 24
def [](key)
key = key.to_s
assert_explanation(key)
if @configuration.has_key?(key)
@configuration[key]
else
raise ConfigurationMissingError,
"configuration key '#{key}' is missing. "\
"it should be in the file '#{file}'. "\
"Explanation: #{@explanation[key]}"
end
end
|
#[]=(key, value) ⇒ Object
37
38
39
40
|
# File 'lib/helpful_configuration.rb', line 37
def []=(key, value)
key = key.to_s
@configuration[key] = value
end
|
47
48
49
50
|
# File 'lib/helpful_configuration.rb', line 47
def configured?(key)
assert_explanation(key)
@configuration.has_key?(key)
end
|
#explain(key, explanation) ⇒ Object
42
43
44
45
|
# File 'lib/helpful_configuration.rb', line 42
def explain(key, explanation)
key = key.to_s
@explanation[key] = explanation
end
|
#with_default(default, key) ⇒ Object
52
53
54
55
|
# File 'lib/helpful_configuration.rb', line 52
def with_default(default, key)
assert_explanation(key)
configured?(key) ? self[key] : default
end
|