confiture 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +1 -1
- data/lib/confiture/configuration.rb +52 -6
- data/lib/confiture/version.rb +1 -1
- data/spec/configuration_spec.rb +30 -6
- metadata +7 -7
data/README.md
CHANGED
@@ -7,14 +7,29 @@ module Confiture
|
|
7
7
|
end
|
8
8
|
|
9
9
|
module ClassExtension
|
10
|
+
|
11
|
+
# Set a hash of defaults.
|
12
|
+
# Defaults will be used while configuring the options, but may be overridden.
|
13
|
+
#
|
10
14
|
def confiture_defaults(defaults)
|
11
15
|
@defaults = defaults
|
12
16
|
end
|
13
17
|
|
18
|
+
# Set a list of allowed configuration options.
|
19
|
+
# If set, trying to access an option that is not configured will result in an ArgumentError.
|
20
|
+
#
|
14
21
|
def confiture_allowed_keys(*allowed_keys)
|
15
22
|
@allowed_keys = allowed_keys
|
16
23
|
end
|
17
24
|
|
25
|
+
# Set a list of mandatory configuration options.
|
26
|
+
# If set, trying to access an option that is not configured properly will result in an ArgumentError.
|
27
|
+
# The validation can be triggered manually by calling +validate!+
|
28
|
+
#
|
29
|
+
def confiture_mandatory_keys(*mandatory_keys)
|
30
|
+
@mandatory_keys = mandatory_keys
|
31
|
+
end
|
32
|
+
|
18
33
|
# Rails initializer configuration:
|
19
34
|
#
|
20
35
|
# Confiture::Configuration.configure do |config|
|
@@ -59,6 +74,8 @@ module Confiture
|
|
59
74
|
self
|
60
75
|
end
|
61
76
|
|
77
|
+
# Run a block of code with temporary configuration.
|
78
|
+
#
|
62
79
|
def with_config(options={})
|
63
80
|
current_data = data
|
64
81
|
configure(options, true)
|
@@ -72,8 +89,41 @@ module Confiture
|
|
72
89
|
init_config(true)
|
73
90
|
end
|
74
91
|
|
92
|
+
# Raises an ArgumentError if the configuration is not valid.
|
93
|
+
#
|
94
|
+
def validate!
|
95
|
+
unless valid?
|
96
|
+
raise ArgumentError.new("you are missing mandatory configuration options. please set #{@mandatory_keys}")
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
# Validates the configuration. All mandatory keys have to be not blank.
|
101
|
+
#
|
102
|
+
def valid?
|
103
|
+
@mandatory_keys.nil? || @mandatory_keys.none? { |key| blank?(key) }
|
104
|
+
end
|
105
|
+
|
106
|
+
# Raises an ArgumentError if the given key is not allowed as a configuration option.
|
107
|
+
#
|
108
|
+
def validate_key!(key)
|
109
|
+
unless valid_key?(key)
|
110
|
+
raise ArgumentError.new("#{key} is not allowed, use one of #{@allowed_keys}")
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
# Validates if a given key is valid for configuration.
|
115
|
+
#
|
116
|
+
def valid_key?(key)
|
117
|
+
@allowed_keys.nil? || @allowed_keys.include?(key)
|
118
|
+
end
|
119
|
+
|
75
120
|
private
|
76
121
|
|
122
|
+
def blank?(key)
|
123
|
+
val = self.send key
|
124
|
+
val.nil? || val.empty?
|
125
|
+
end
|
126
|
+
|
77
127
|
def data
|
78
128
|
Thread.current[:confiture]
|
79
129
|
end
|
@@ -92,20 +142,16 @@ module Confiture
|
|
92
142
|
meth = "#{meth}"
|
93
143
|
if meth =~ /.+=/ && args.size == 1
|
94
144
|
key = meth[0..-2].to_sym
|
95
|
-
|
145
|
+
validate_key!(key)
|
96
146
|
data.options[key] = args.last
|
97
147
|
elsif args.size == 0
|
98
148
|
key = meth.to_sym
|
99
|
-
|
149
|
+
validate_key!(key)
|
100
150
|
data.options[key]
|
101
151
|
else
|
102
152
|
super
|
103
153
|
end
|
104
154
|
end
|
105
|
-
|
106
|
-
def check_key!(key)
|
107
|
-
raise ArgumentError.new("#{key} is not allowed, use one of #{@allowed_keys}") if @allowed_keys && !@allowed_keys.include?(key)
|
108
|
-
end
|
109
155
|
end
|
110
156
|
end
|
111
157
|
end
|
data/lib/confiture/version.rb
CHANGED
data/spec/configuration_spec.rb
CHANGED
@@ -5,6 +5,11 @@ module Confiture
|
|
5
5
|
include Confiture::Configuration
|
6
6
|
end
|
7
7
|
|
8
|
+
class Valid
|
9
|
+
include Confiture::Configuration
|
10
|
+
confiture_mandatory_keys(:key)
|
11
|
+
end
|
12
|
+
|
8
13
|
class Allowed
|
9
14
|
include Confiture::Configuration
|
10
15
|
confiture_allowed_keys(:key)
|
@@ -16,6 +21,22 @@ module Confiture
|
|
16
21
|
end
|
17
22
|
|
18
23
|
describe Confiture do
|
24
|
+
context "reset" do
|
25
|
+
it "should reset the configuration" do
|
26
|
+
Config.configure(:key => "value").tap do |config|
|
27
|
+
config.key.should eql("value")
|
28
|
+
config.reset!
|
29
|
+
config.key.should be_nil
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
context "validate" do
|
35
|
+
it "should validate the configuration" do
|
36
|
+
expect { Valid.configure.validate! }.should raise_error(ArgumentError, "you are missing mandatory configuration options. please set [:key]")
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
19
40
|
context "defaults" do
|
20
41
|
it "should have default settings" do
|
21
42
|
Default.configure.key.should eql('value')
|
@@ -28,6 +49,7 @@ module Confiture
|
|
28
49
|
Default.configure.key.should eql('value')
|
29
50
|
end
|
30
51
|
end
|
52
|
+
|
31
53
|
context "allowed_keys" do
|
32
54
|
it "should have access to allowed fields" do
|
33
55
|
Allowed.configure do |config|
|
@@ -58,9 +80,10 @@ module Confiture
|
|
58
80
|
end
|
59
81
|
|
60
82
|
it "should read configuration from yml" do
|
61
|
-
|
62
|
-
|
63
|
-
|
83
|
+
Config.configure(:yaml => 'spec/config.yml').tap do |config|
|
84
|
+
config.secret.should eql('secret_yml')
|
85
|
+
config.key.should eql('key_yml')
|
86
|
+
end
|
64
87
|
end
|
65
88
|
|
66
89
|
it "should read configuration from yml with block" do
|
@@ -75,9 +98,10 @@ module Confiture
|
|
75
98
|
|
76
99
|
context "with_config" do
|
77
100
|
it "should have a config on a per block basis" do
|
78
|
-
|
79
|
-
|
80
|
-
|
101
|
+
Config.configure(:key => "value").tap do |config|
|
102
|
+
config.with_config(:key => "bla") do
|
103
|
+
Config.key.should eql("bla")
|
104
|
+
end
|
81
105
|
end
|
82
106
|
Config.key.should eql("value")
|
83
107
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: confiture
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -13,7 +13,7 @@ date: 2012-07-06 00:00:00.000000000 Z
|
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|
16
|
-
requirement: &
|
16
|
+
requirement: &70354285626600 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: 0.9.2
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70354285626600
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rspec
|
27
|
-
requirement: &
|
27
|
+
requirement: &70354285626100 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ~>
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '2.7'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70354285626100
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: pry
|
38
|
-
requirement: &
|
38
|
+
requirement: &70354285625640 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ~>
|
@@ -43,7 +43,7 @@ dependencies:
|
|
43
43
|
version: 0.9.9
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70354285625640
|
47
47
|
description: Confiture helps with configuring your gem.
|
48
48
|
email:
|
49
49
|
- phoetmail@googlemail.com
|