helpful_configuration 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/helpful_configuration.rb +66 -0
  2. metadata +68 -0
@@ -0,0 +1,66 @@
1
+ class HelpfulConfiguration
2
+ attr_accessor :file
3
+
4
+ class ConfigurationMissingError < StandardError
5
+ end
6
+
7
+ def self.from_file(file, keys)
8
+ config = new
9
+ config.file = File.expand_path(file)
10
+ if File.exists?(file)
11
+ JSON.parse(File.read(file)).each do |key, value|
12
+ config[key] = value
13
+ end
14
+ end
15
+ config
16
+ end
17
+
18
+ def initialize(configuration = {}, file=nil)
19
+ @configuration = configuration
20
+ @explanation = {}
21
+ self.file = file
22
+ end
23
+
24
+ def [](key)
25
+ key = key.to_s
26
+ assert_explanation(key)
27
+ if @configuration.has_key?(key)
28
+ @configuration[key]
29
+ else
30
+ raise ConfigurationMissingError,
31
+ "configuration key '#{key}' is missing. "\
32
+ "it should be in the file '#{file}'. "\
33
+ "Explanation: #{@explanation[key]}"
34
+ end
35
+ end
36
+
37
+ def []=(key, value)
38
+ key = key.to_s
39
+ @configuration[key] = value
40
+ end
41
+
42
+ def explain(key, explanation)
43
+ key = key.to_s
44
+ @explanation[key] = explanation
45
+ end
46
+
47
+ def configured?(key)
48
+ assert_explanation(key)
49
+ @configuration.has_key?(key)
50
+ end
51
+
52
+ def with_default(default, key)
53
+ assert_explanation(key)
54
+ configured?(key) ? self[key] : default
55
+ end
56
+
57
+ private
58
+
59
+ def assert_explanation(key)
60
+ unless @explanation.has_key?(key)
61
+ raise "no explanation found for key #{key}. "\
62
+ "each configuration key must have an explanation. "\
63
+ "typo in the key name?"
64
+ end
65
+ end
66
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: helpful_configuration
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Kristian Hanekamp
14
+ - Infopark AG
15
+ autorequire:
16
+ bindir: bin
17
+ cert_chain: []
18
+
19
+ date: 2011-09-22 00:00:00 +02:00
20
+ default_executable:
21
+ dependencies: []
22
+
23
+ description: Configuration documenation library
24
+ email: kristian.hanekamp@infopark.de
25
+ executables: []
26
+
27
+ extensions: []
28
+
29
+ extra_rdoc_files: []
30
+
31
+ files:
32
+ - lib/helpful_configuration.rb
33
+ has_rdoc: true
34
+ homepage:
35
+ licenses: []
36
+
37
+ post_install_message:
38
+ rdoc_options: []
39
+
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ hash: 3
48
+ segments:
49
+ - 0
50
+ version: "0"
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ hash: 3
57
+ segments:
58
+ - 0
59
+ version: "0"
60
+ requirements: []
61
+
62
+ rubyforge_project:
63
+ rubygems_version: 1.3.7
64
+ signing_key:
65
+ specification_version: 3
66
+ summary: HelpfulConfiguration offers an easy way to provide helpful explanations for ruby applications that need to be configured.
67
+ test_files: []
68
+