konfa 0.4.0 → 0.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/konfa/initializer.rb +46 -0
- data/lib/konfa/rspec.rb +27 -0
- metadata +4 -2
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
|
3
|
+
module Konfa
|
4
|
+
module Initializer
|
5
|
+
def self.included(base)
|
6
|
+
base.extend ClassMethods
|
7
|
+
end
|
8
|
+
|
9
|
+
module ClassMethods
|
10
|
+
def init_with_yaml(path)
|
11
|
+
# FIXME: It would be a lot cleaner if the YAML library would raise an
|
12
|
+
# exception if it fails to read the file. We'll handle it like this for now
|
13
|
+
# load_file just returns "false" if it fails
|
14
|
+
yaml_data = YAML.load_file(path)
|
15
|
+
|
16
|
+
unless yaml_data.nil?
|
17
|
+
raise Konfa::InitializationError.new("Bad YAML format, key/value pairs expected") unless yaml_data.kind_of?(Hash)
|
18
|
+
|
19
|
+
yaml_data.each do |variable, value|
|
20
|
+
self.store(variable, value)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
dump
|
25
|
+
end
|
26
|
+
|
27
|
+
def init_with_env
|
28
|
+
conf_prefix = self.env_variable_prefix.upcase
|
29
|
+
|
30
|
+
ENV.keys.reject { |key|
|
31
|
+
key !~ /^#{conf_prefix}/ # Ignore everything that doesn't match the prefix
|
32
|
+
}.each { |key|
|
33
|
+
variable = key[conf_prefix.size..-1].downcase
|
34
|
+
|
35
|
+
self.store(variable, ENV[key])
|
36
|
+
}
|
37
|
+
|
38
|
+
dump
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
|
44
|
+
class InitializationError < StandardError
|
45
|
+
end
|
46
|
+
end
|
data/lib/konfa/rspec.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
module Konfa
|
2
|
+
module RSpec
|
3
|
+
shared_context 'with konfa' do |klass|
|
4
|
+
before {
|
5
|
+
@konfa_klass = klass
|
6
|
+
@konfa_stubbed_klasses = []
|
7
|
+
}
|
8
|
+
|
9
|
+
def let_config(variable, value, use_klass=@konfa_klass)
|
10
|
+
raise Konfa::UnsupportedVariableError.new(variable) unless use_klass.allowed_variables.has_key?(variable)
|
11
|
+
|
12
|
+
unless @konfa_stubbed_klasses.include?(use_klass)
|
13
|
+
allow(use_klass).to receive(:get).and_call_original
|
14
|
+
@konfa_stubbed_klasses << use_klass
|
15
|
+
end
|
16
|
+
|
17
|
+
allow(use_klass).to receive(:get).with(variable).and_return(value)
|
18
|
+
end
|
19
|
+
|
20
|
+
def with_config(variables, use_klass=@konfa_klass)
|
21
|
+
variables.each_pair do |var, val|
|
22
|
+
let_config(var, val, use_klass)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: konfa
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2014-
|
13
|
+
date: 2014-01-12 00:00:00.000000000 Z
|
14
14
|
dependencies: []
|
15
15
|
description: Helps you avoid common pitfalls when dealing with app config
|
16
16
|
email: code@avidiy.se
|
@@ -19,6 +19,8 @@ extensions: []
|
|
19
19
|
extra_rdoc_files: []
|
20
20
|
files:
|
21
21
|
- lib/konfa.rb
|
22
|
+
- lib/konfa/initializer.rb
|
23
|
+
- lib/konfa/rspec.rb
|
22
24
|
homepage: http://github.com/avidity/konfa
|
23
25
|
licenses:
|
24
26
|
- MIT
|