configure 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -9,10 +9,10 @@ receives a block and returns well-structured configuration values.
9
9
  The most simple way <tt>configure</tt> can be used is to pass a block to the <tt>Configure.process</tt> method and
10
10
  receive back a hash with the configuration values.
11
11
 
12
- laser = Configure.process do
12
+ laser = Configure.process {
13
13
  label "red laser"
14
14
  wave_length 700
15
- end
15
+ }
16
16
 
17
17
  laser == {
18
18
  :label => "red laser",
@@ -23,17 +23,17 @@ receive back a hash with the configuration values.
23
23
 
24
24
  It is also possible to pass blocks to configuration keys and combine multiple values to an array.
25
25
 
26
- laser = Configure.process do
26
+ laser = Configure.process {
27
27
  label "red and violet pulse laser"
28
- pulse do
28
+ pulse {
29
29
  wave_length 700
30
30
  duration 20
31
- end
32
- pulse do
31
+ }
32
+ pulse {
33
33
  wave_length 400
34
34
  duration 20
35
- end
36
- end
35
+ }
36
+ }
37
37
 
38
38
  laser == {
39
39
  :label => "red and violet pulse laser",
@@ -46,13 +46,13 @@ It is also possible to pass blocks to configuration keys and combine multiple va
46
46
  If arguments and a block is passed to a configuration key, the arguments are stored at the <tt>:arguments</tt> key
47
47
  inside the nested configuration.
48
48
 
49
- laser = Configure.process do
49
+ laser = Configure.process {
50
50
  label "red pulse laser"
51
- pulse "test" do
51
+ pulse "test" {
52
52
  wave_length 700
53
53
  duration 20
54
- end
55
- end
54
+ }
55
+ }
56
56
 
57
57
  laser = {
58
58
  :label => "red pulse laser",
@@ -67,21 +67,22 @@ inside the nested configuration.
67
67
  == Schema
68
68
 
69
69
  In order receive a more structured configuration, a schema can be passed to the <tt>Configure.process</tt> method. This
70
- schema is a simple hash, that can be created - off course - using the configure syntax ;-)
70
+ schema is a simple hash, that can be created - off course - using the configure syntax ;-) (Guess how the
71
+ schema-structure is defined internally ;-) )
71
72
 
72
73
  === Defaults
73
74
 
74
- schema = Configure::Schema.build do
75
- defaults do
75
+ schema = Configure::Schema.build {
76
+ defaults {
76
77
  wave_length 700
77
78
  duration 20
78
- end
79
- end
79
+ }
80
+ }
80
81
 
81
- laser = Configure.process schema do
82
+ laser = Configure.process(schema) {
82
83
  label "violet pulse laser"
83
84
  wave_length 400
84
- end
85
+ }
85
86
 
86
87
  laser = {
87
88
  :label => "violet pulse laser",
@@ -93,33 +94,33 @@ schema is a simple hash, that can be created - off course - using the configure
93
94
 
94
95
  Nested schema have to go to the <tt>nested</tt> section.
95
96
 
96
- schema = Configure::Schema.build do
97
- nested do
98
- pulse do
99
- defaults do
97
+ schema = Configure::Schema.build {
98
+ nested {
99
+ pulse {
100
+ defaults {
100
101
  wave_length 700
101
102
  duration 20
102
- end
103
- end
104
- end
105
- end
103
+ }
104
+ }
105
+ }
106
+ }
106
107
 
107
108
  If a nested schema provides the key <tt>argument_keys</tt>, the arguments that have been passed along with the nested
108
109
  configuration, are stored to these keys. The rest will be assigned to the <tt>:arguments</tt> key as before.
109
110
 
110
- schema = Configure::Schema.build do
111
- nested do
112
- pulse do
111
+ schema = Configure::Schema.build {
112
+ nested {
113
+ pulse {
113
114
  argument_keys :wave_length, :duration
114
- end
115
- end
116
- end
115
+ }
116
+ }
117
+ }
117
118
 
118
- laser = Configure.process schema do
119
- pulse 700, 20, :extra do
119
+ laser = Configure.process(schema) {
120
+ pulse(700, 20, :extra) {
120
121
  label "red pulse"
121
- end
122
- end
122
+ }
123
+ }
123
124
 
124
125
  laser == {
125
126
  :pulse => {
@@ -130,6 +131,8 @@ configuration, are stored to these keys. The rest will be assigned to the <tt>:a
130
131
  }
131
132
  } # => true
132
133
 
134
+ The <tt>:nested_default</tt> attribute can be used to define a default schema for all nested blocks.
135
+
133
136
  === Configuration class
134
137
 
135
138
  Sometimes, a hash may not be the best data structure to store the configuration. If a more strict structure is needed,
@@ -145,26 +148,52 @@ is raised.
145
148
 
146
149
  end
147
150
 
148
- schema = Configure::Schema.build do
151
+ schema = Configure::Schema.build {
149
152
  configuration_class Laser
150
- defaults do
153
+ defaults {
151
154
  duration 20
152
- end
153
- end
155
+ }
156
+ }
154
157
 
155
- laser = Configure.process schema do
158
+ laser = Configure.process(schema) {
156
159
  label "red laser"
157
160
  wave_length 700
158
- end
161
+ }
159
162
 
160
163
  laser.is_a? Laser # => true
161
164
  laser.label == "red laser" # => true
162
165
  laser.wave_length == 700 # => true
163
166
  laser.duration == 20 # => true
164
167
 
165
- Configure.process schema do
168
+ Configure.process(schema) {
166
169
  invalid_key "value"
167
- end # => raises Configure::Injector::Error
170
+ } # => raises Configure::InvalidKeyError
171
+
172
+ === The Only list
173
+
174
+ If the <tt>:only</tt> attribute is defined in a schema, only the values of the given keys are allowed to be set.
175
+
176
+ schema = Configure::Schema.build {
177
+ only :duration
178
+ }
179
+
180
+ laser = Configure.process(schema) {
181
+ wave_length 700
182
+ } # => raises Configure::InvalidKeyError
183
+
184
+ === The Not-Nil list
185
+
186
+ If the <tt>:not_nil</tt> attribute is defined in a schema, an error will be raised if one of the values of the given
187
+ keys remains <tt>nil</tt>.
188
+
189
+ schema = Configure::Schema.build {
190
+ only :wave_length, :duration
191
+ not_nil :duration
192
+ }
193
+
194
+ laser = Configure.process(schema) {
195
+ wave_length 700
196
+ } # => raises Configure::NilValueError
168
197
 
169
198
  == Development
170
199
 
@@ -1,19 +1,29 @@
1
1
 
2
2
  module Configure
3
3
 
4
+ autoload :Checker, File.join(File.dirname(__FILE__), "configure", "checker")
4
5
  autoload :Injector, File.join(File.dirname(__FILE__), "configure", "injector")
5
6
  autoload :Sandbox, File.join(File.dirname(__FILE__), "configure", "sandbox")
6
7
  autoload :Schema, File.join(File.dirname(__FILE__), "configure", "schema")
8
+ autoload :Value, File.join(File.dirname(__FILE__), "configure", "value")
7
9
 
8
- def self.process(schema = { }, &block)
10
+ # This error is thrown, if a key can't be set or get.
11
+ class InvalidKeyError < StandardError; end
12
+
13
+ # This error is thrown, if a value is nil that shouldn't be nil.
14
+ class NilValueError < StandardError; end
15
+
16
+ def self.process(schema = nil, &block)
17
+ schema ||= Schema.build { }
9
18
  process_configuration schema, &block
10
19
  end
11
20
 
12
21
  def self.process_configuration(schema = { }, &block)
13
22
  injector = Injector.new schema
14
- sandbox = Sandbox.new injector
15
- sandbox.instance_eval &block
16
- injector.configuration
23
+ Sandbox.new(injector).instance_eval &block
24
+ configuration = injector.configuration
25
+ Checker.new(schema, configuration).check!
26
+ configuration
17
27
  end
18
28
 
19
29
  end
@@ -0,0 +1,29 @@
1
+
2
+ # Checks the value against nil.
3
+ class Configure::Checker
4
+
5
+ attr_accessor :schema
6
+ attr_accessor :configuration
7
+
8
+ def initialize(schema, configuration)
9
+ @schema, @configuration = schema, configuration
10
+ end
11
+
12
+ def check!
13
+ check_not_nils!
14
+ end
15
+
16
+ private
17
+
18
+ def check_not_nils!
19
+ [ @schema[:not_nil] ].flatten.compact.each do |key|
20
+ check_not_nil! key
21
+ end
22
+ end
23
+
24
+ def check_not_nil!(key)
25
+ value = Configure::Value.new @schema, @configuration, key
26
+ raise Configure::NilValueError, "the value of the key #{key} can't be nil!" if value.get.nil?
27
+ end
28
+
29
+ end
@@ -2,9 +2,6 @@
2
2
  # Creates a configuration from the passed class and provides methods to inject values.
3
3
  class Configure::Injector
4
4
 
5
- # The error will be raised if the configuration value can't be set or get.
6
- class Error < StandardError; end
7
-
8
5
  attr_reader :schema
9
6
 
10
7
  def initialize(schema)
@@ -13,7 +10,7 @@ class Configure::Injector
13
10
  end
14
11
 
15
12
  def configuration_class
16
- @schema[:configuration_class] || Hash
13
+ @schema[:configuration_class]
17
14
  end
18
15
 
19
16
  def defaults
@@ -21,15 +18,15 @@ class Configure::Injector
21
18
  end
22
19
 
23
20
  def put_block(key, arguments, &block)
24
- nested_schema = (self.schema[:nested] || { })[key] || { }
21
+ nested_schema = (@schema[:nested] || { })[key] || @schema[:nested_default] || Configure::Schema.build { }
25
22
  nested_configuration = Configure.process_configuration nested_schema, &block
26
23
  Arguments.new(nested_schema, nested_configuration, arguments).put
27
- value = Value.new @configuration, key
24
+ value = Configure::Value.new @schema, @configuration, key
28
25
  value.put_or_combine nested_configuration
29
26
  end
30
27
 
31
28
  def put_arguments(key, arguments)
32
- value = Value.new @configuration, key
29
+ value = Configure::Value.new @schema, @configuration, key
33
30
  value.put_single_or_multiple arguments
34
31
  end
35
32
 
@@ -42,8 +39,8 @@ class Configure::Injector
42
39
 
43
40
  def apply_defaults
44
41
  self.defaults.each do |key, default_value|
45
- value = Value.new @configuration, key
46
- value.put_if_nil default_value
42
+ value = Configure::Value.new @schema, @configuration, key
43
+ value.put_unless_existing default_value
47
44
  end
48
45
  end
49
46
 
@@ -57,68 +54,28 @@ class Configure::Injector
57
54
  def put
58
55
  put_to_specified_keys
59
56
  put_to_argument_key
57
+ check_values!
60
58
  end
61
59
 
60
+ private
61
+
62
62
  def put_to_specified_keys
63
63
  return if @arguments.empty?
64
64
  argument_keys = [ @schema[:argument_keys] ].flatten.compact
65
65
  argument_keys.each do |argument_key|
66
- arguments_value = Value.new @configuration, argument_key
66
+ arguments_value = Configure::Value.new @schema, @configuration, argument_key
67
67
  arguments_value.put @arguments.shift
68
68
  end
69
69
  end
70
70
 
71
71
  def put_to_argument_key
72
72
  return if @arguments.empty?
73
- arguments_value = Value.new @configuration, :arguments
73
+ arguments_value = Configure::Value.new @schema, @configuration, :arguments
74
74
  arguments_value.put @arguments
75
75
  end
76
76
 
77
- end
78
-
79
- # Injector for a single configuration value.
80
- class Value
81
-
82
- def initialize(configuration, key)
83
- @configuration, @key = configuration, key
84
- end
85
-
86
- def put_single_or_multiple(values)
87
- self.put_or_combine values.size == 1 ? values.first : values
88
- end
89
-
90
- def put_or_combine(value)
91
- self.put exists? ? [ get, value ].flatten : value
92
- end
93
-
94
- def put_if_nil(value)
95
- self.put value if get.nil?
96
- end
97
-
98
- def put(value)
99
- method_name = :"#{@key}="
100
- if @configuration.respond_to?(method_name)
101
- @configuration.send method_name, value
102
- elsif @configuration.respond_to?(:[]=)
103
- @configuration[@key] = value
104
- else
105
- raise Error, "couldn't set configuration value for key #{@key}!"
106
- end
107
- end
108
-
109
- def get
110
- method_name = :"#{@key}"
111
- if @configuration.respond_to?(method_name)
112
- @configuration.send method_name
113
- elsif @configuration.respond_to?(:[])
114
- @configuration[@key]
115
- else
116
- raise Error, "couldn't get configuration value for key #{@key}!"
117
- end
118
- end
119
-
120
- def exists?
121
- !!self.get
77
+ def check_values!
78
+ Configure::Checker.new(@schema, @configuration).check!
122
79
  end
123
80
 
124
81
  end
@@ -2,8 +2,30 @@
2
2
  module Configure::Schema
3
3
 
4
4
  SCHEMA = {
5
-
6
- }.freeze
5
+ :configuration_class => Hash,
6
+ :only => [
7
+ :configuration_class,
8
+ :only,
9
+ :not_nil,
10
+ :argument_keys,
11
+ :defaults,
12
+ :nested_default,
13
+ :nested
14
+ ],
15
+ :not_nil => [
16
+ :configuration_class
17
+ ],
18
+ :defaults => {
19
+ :configuration_class => Hash
20
+ },
21
+ :nested => {
22
+ :nested => {
23
+ :configuration_class => Hash
24
+ }
25
+ }
26
+ }
27
+ SCHEMA[:nested][:nested][:nested_default] = SCHEMA
28
+ SCHEMA.freeze
7
29
 
8
30
  def self.build(&block)
9
31
  Configure.process SCHEMA, &block
@@ -0,0 +1,61 @@
1
+
2
+ # Handler for a single configuration value.
3
+ class Configure::Value
4
+
5
+ attr_accessor :schema
6
+ attr_accessor :configuration
7
+ attr_accessor :key
8
+
9
+ def initialize(schema, configuration, key)
10
+ @schema, @configuration, @key = schema, configuration, key
11
+ end
12
+
13
+ def put_single_or_multiple(values)
14
+ self.put_or_combine values.size == 1 ? values.first : values
15
+ end
16
+
17
+ def put_or_combine(value)
18
+ existing_value = get
19
+ self.put existing_value.nil? ? value : [ existing_value, value ].flatten
20
+ end
21
+
22
+ def put_unless_existing(value)
23
+ self.put value unless exists?
24
+ end
25
+
26
+ def put(value)
27
+ check_only_list!
28
+ method_name = :"#{@key}="
29
+ if @configuration.respond_to?(method_name)
30
+ @configuration.send method_name, value
31
+ elsif @configuration.respond_to?(:[]=)
32
+ @configuration[@key] = value
33
+ else
34
+ raise Configure::InvalidKeyError, "couldn't set configuration value for key #{@key}!"
35
+ end
36
+ end
37
+
38
+ def get
39
+ check_only_list!
40
+ method_name = :"#{@key}"
41
+ if @configuration.respond_to?(method_name)
42
+ @configuration.send method_name
43
+ elsif @configuration.respond_to?(:[])
44
+ @configuration[@key]
45
+ else
46
+ raise Configure::InvalidKeyError, "couldn't get configuration value for key #{@key}!"
47
+ end
48
+ end
49
+
50
+ def exists?
51
+ @configuration.respond_to?(:has_key?) ? @configuration.has_key?(@key) : !!self.get
52
+ end
53
+
54
+ private
55
+
56
+ def check_only_list!
57
+ raise Configure::InvalidKeyError, "access to set configuration value for key #{@key} denied!" if
58
+ @schema.has_key?(:only) && ![ @schema[:only] ].flatten.include?(@key)
59
+ end
60
+
61
+ end
@@ -12,17 +12,19 @@ describe Configure do
12
12
  configuration_class = @configuration_class
13
13
  @schema = described_class::Schema.build do
14
14
  configuration_class configuration_class
15
- defaults do
15
+ only :test_key_one, :test_key_two, :test_key_three, :test_key_four, :test_key_five
16
+ not_nil :test_key_one
17
+ defaults {
16
18
  test_key_one "default value"
17
- end
18
- nested do
19
- test_key_two do
19
+ }
20
+ nested {
21
+ test_key_two {
20
22
  argument_keys :test_key_three, :test_key_four
21
- defaults do
23
+ defaults {
22
24
  test_key_five "default value"
23
- end
24
- end
25
- end
25
+ }
26
+ }
27
+ }
26
28
  end
27
29
  end
28
30
 
@@ -42,6 +44,23 @@ describe Configure do
42
44
  configuration.test_key_one.should == "one"
43
45
  end
44
46
 
47
+ it "should raise an #{Configure::InvalidKeyError} if key isn't included in :only list" do
48
+ lambda do
49
+ described_class.process @schema do
50
+ test_key_six "six"
51
+ end
52
+ end.should raise_error(Configure::InvalidKeyError)
53
+ end
54
+
55
+ it "should raise an #{Configure::NilValueError} if a not-nil value is nil" do
56
+ @configuration_class.send(:define_method, :has_key?) { |key| key == :test_key_one }
57
+ lambda do
58
+ described_class.process @schema do
59
+ test_key_one nil
60
+ end
61
+ end.should raise_error(Configure::NilValueError)
62
+ end
63
+
45
64
  it "should use the specified default values" do
46
65
  configuration = described_class.process @schema do
47
66
 
@@ -77,6 +77,16 @@ describe Configure do
77
77
  }
78
78
  end
79
79
 
80
+ it "should take nil values" do
81
+ configuration = described_class.process do
82
+ test_key nil
83
+ end
84
+
85
+ configuration.should == {
86
+ :test_key => nil
87
+ }
88
+ end
89
+
80
90
  end
81
91
 
82
92
  end
@@ -0,0 +1,53 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "spec_helper"))
2
+
3
+ describe Configure::Schema do
4
+
5
+ describe "build" do
6
+
7
+ before :each do
8
+ @block = Proc.new {
9
+ configuration_class Hash
10
+ nested {
11
+ test_key_one {
12
+ only :test_key_two
13
+ }
14
+ }
15
+ }
16
+ end
17
+
18
+ it "should return a hash with the schema" do
19
+ schema = described_class.build &@block
20
+ schema.should == {
21
+ :configuration_class => Hash,
22
+ :nested => {
23
+ :test_key_one => {
24
+ :configuration_class => Hash,
25
+ :only => :test_key_two
26
+ }
27
+ }
28
+ }
29
+ end
30
+
31
+ it "should raise an #{Configure::InvalidKeyError} if an invalid schema key is given" do
32
+ lambda do
33
+ described_class.build {
34
+ invalid :value
35
+ }
36
+ end.should raise_error(Configure::InvalidKeyError)
37
+ end
38
+
39
+ it "should raise an #{Configure::InvalidKeyError} if an invalid nested schema key is given" do
40
+ lambda do
41
+ described_class.build {
42
+ nested {
43
+ test_key_one {
44
+ invalid :value
45
+ }
46
+ }
47
+ }
48
+ end.should raise_error(Configure::InvalidKeyError)
49
+ end
50
+
51
+ end
52
+
53
+ end
@@ -0,0 +1,41 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "spec_helper"))
2
+
3
+ describe Configure::Checker do
4
+
5
+ before :each do
6
+ @schema = {
7
+ :not_nil => [ :test ]
8
+ }
9
+ @configuration = { }
10
+
11
+ @checker = described_class.new @schema, @configuration
12
+ end
13
+
14
+ describe "#check!" do
15
+
16
+ before :each do
17
+ @value = mock Configure::Value, :get => "value"
18
+ Configure::Value.stub :new => @value
19
+ end
20
+
21
+ it "should initialize #{Configure::Value}" do
22
+ Configure::Value.should_receive(:new).with(@schema, @configuration, :test).and_return(@value)
23
+ @checker.check!
24
+ end
25
+
26
+ it "should not raise any error" do
27
+ lambda do
28
+ @checker.check!
29
+ end.should_not raise_error
30
+ end
31
+
32
+ it "should raise a #{Configure::NilValueError} if value is nil" do
33
+ @value.stub :get => nil
34
+ lambda do
35
+ @checker.check!
36
+ end.should raise_error(Configure::NilValueError)
37
+ end
38
+
39
+ end
40
+
41
+ end
@@ -6,10 +6,16 @@ describe Configure::Injector do
6
6
  @configuration_class = Class.new
7
7
  @configuration_class.send :attr_accessor, :test_key
8
8
  @configuration_class.send :attr_accessor, :another_test_key
9
+ @configuration_class.send :attr_accessor, :invalid_key
9
10
 
10
- @nested_schema = { }
11
+ @nested_schema = {
12
+ :configuration_class => @configuration_class
13
+ }
11
14
  @schema = {
12
15
  :configuration_class => @configuration_class,
16
+ :only => [ :test_key, :another_test_key, :not_existing_key ],
17
+ :not_nil => :another_test_key,
18
+ :nested_default => @nested_schema,
13
19
  :nested => {
14
20
  :test_key => @nested_schema
15
21
  },
@@ -40,7 +46,10 @@ describe Configure::Injector do
40
46
  @block = Proc.new { }
41
47
  @arguments = [ "one" ]
42
48
  @nested_configuration = { }
43
- Configure.stub(:process_configuration => @nested_configuration)
49
+ Configure.stub :process_configuration => @nested_configuration
50
+
51
+ @checker = mock Configure::Checker, :check! => nil
52
+ Configure::Checker.stub :new => @checker
44
53
  end
45
54
 
46
55
  it "should process a new (nested) configuration" do
@@ -48,6 +57,11 @@ describe Configure::Injector do
48
57
  @injector.put_block :test_key, @arguments, &@block
49
58
  end
50
59
 
60
+ it "should use the nested default if no nested schema is specified for the given key" do
61
+ Configure.should_receive(:process_configuration).with(@nested_schema, &@block).and_return(@nested_configuration)
62
+ @injector.put_block :another_test_key, @arguments, &@block
63
+ end
64
+
51
65
  it "should nest the configuration" do
52
66
  @injector.put_block :test_key, @arguments, &@block
53
67
  @injector.configuration.test_key.should == @nested_configuration
@@ -69,6 +83,16 @@ describe Configure::Injector do
69
83
  @injector.configuration.test_key[:another_test_key].should == "one"
70
84
  end
71
85
 
86
+ it "should initialize the checker" do
87
+ Configure::Checker.should_receive(:new).with(@nested_schema, @nested_configuration).and_return(@checker)
88
+ @injector.put_block :test_key, @arguments, &@block
89
+ end
90
+
91
+ it "should check the nested configuration values" do
92
+ @checker.should_receive(:check!)
93
+ @injector.put_block :test_key, @arguments, &@block
94
+ end
95
+
72
96
  it "should combine nested configurations to an array" do
73
97
  @injector.put_block :test_key, @arguments, &@block
74
98
  @injector.put_block :test_key, @arguments, &@block
@@ -100,10 +124,16 @@ describe Configure::Injector do
100
124
  @injector.configuration.test_key.should == [ "one", "two" ]
101
125
  end
102
126
 
103
- it "should raise a #{described_class::Error} if key can't be set" do
127
+ it "should raise a #{Configure::InvalidKeyError} if key is not in only-list" do
104
128
  lambda do
105
129
  @injector.put_arguments :invalid_key, [ "one" ]
106
- end.should raise_error(described_class::Error)
130
+ end.should raise_error(Configure::InvalidKeyError)
131
+ end
132
+
133
+ it "should raise a #{Configure::InvalidKeyError} if key is not existing" do
134
+ lambda do
135
+ @injector.put_arguments :not_existing_key, [ "one" ]
136
+ end.should raise_error(Configure::InvalidKeyError)
107
137
  end
108
138
 
109
139
  end
@@ -6,7 +6,7 @@ describe Configure::Schema do
6
6
 
7
7
  before :each do
8
8
  @block = Proc.new { }
9
- Configure.stub(:process)
9
+ Configure.stub :process => nil
10
10
  end
11
11
 
12
12
  it "should use configure to build the schema hash" do
@@ -0,0 +1,151 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "spec_helper"))
2
+
3
+ describe Configure::Value do
4
+
5
+ before :each do
6
+ @schema = { :only => :test }
7
+ @configuration = { }
8
+
9
+ @value = described_class.new @schema, @configuration, :test
10
+ end
11
+
12
+ describe "#put_single_or_multiple" do
13
+
14
+ before :each do
15
+ @value.stub :put_or_combine => nil
16
+ end
17
+
18
+ it "should call :put_or_combine with a single value" do
19
+ @value.should_receive(:put_or_combine).with(:value)
20
+ @value.put_single_or_multiple [ :value ]
21
+ end
22
+
23
+ it "should call :put_or_combine with a multiple values" do
24
+ @value.should_receive(:put_or_combine).with([ :value_one, :value_two ])
25
+ @value.put_single_or_multiple [ :value_one, :value_two ]
26
+ end
27
+
28
+ end
29
+
30
+ describe "#put_or_combine" do
31
+
32
+ before :each do
33
+ @value.stub :put => nil
34
+ @value.stub :get => :test
35
+ end
36
+
37
+ it "should call :put with the argument combined with the existing value if it's existing" do
38
+ @value.should_receive(:put).with([ :test, :value ])
39
+ @value.put_or_combine :value
40
+ end
41
+
42
+ it "should call :put with the argument if no value is existing" do
43
+ @value.stub :get => nil
44
+ @value.should_receive(:put).with(:value)
45
+ @value.put_or_combine :value
46
+ end
47
+
48
+ end
49
+
50
+ describe "#put_unless_existing" do
51
+
52
+ before :each do
53
+ @value.stub :put => nil
54
+ end
55
+
56
+ it "should call :put with the argument if the value isn't existing" do
57
+ @value.should_receive(:put).with(:value)
58
+ @value.put_unless_existing :value
59
+ end
60
+
61
+ it "should not call :put with the argument if the value is existing" do
62
+ @configuration[:test] = nil
63
+ @value.should_not_receive(:put)
64
+ @value.put_unless_existing :value
65
+ end
66
+
67
+ end
68
+
69
+ describe "#put" do
70
+
71
+ it "should set the configuration value" do
72
+ @value.put :value
73
+ @configuration[:test].should == :value
74
+ end
75
+
76
+ it "should raise an #{Configure::InvalidKeyError} if the key isn't included in only-list" do
77
+ @value.key = :invalid
78
+ lambda do
79
+ @value.put :value
80
+ end.should raise_error(Configure::InvalidKeyError)
81
+ end
82
+
83
+ it "should raise an #{Configure::InvalidKeyError} if the value can't be set" do
84
+ @value.configuration = Object.new
85
+ lambda do
86
+ @value.put :value
87
+ end.should raise_error(Configure::InvalidKeyError)
88
+ end
89
+
90
+ end
91
+
92
+ describe "#get" do
93
+
94
+ before :each do
95
+ @configuration[:test] = :value
96
+ end
97
+
98
+ it "should set the configuration value" do
99
+ value = @value.get
100
+ value.should == :value
101
+ end
102
+
103
+ it "should raise an #{Configure::InvalidKeyError} if the key isn't included in only-list" do
104
+ @value.key = :invalid
105
+ lambda do
106
+ @value.get
107
+ end.should raise_error(Configure::InvalidKeyError)
108
+ end
109
+
110
+ it "should raise an #{Configure::InvalidKeyError} if the value can't be set" do
111
+ @value.configuration = Object.new
112
+ lambda do
113
+ @value.get
114
+ end.should raise_error(Configure::InvalidKeyError)
115
+ end
116
+
117
+ end
118
+
119
+ describe "#exists?" do
120
+
121
+ before :each do
122
+ @configuration[:test] = :value
123
+ @value.stub :get => :value
124
+ end
125
+
126
+ it "should return true" do
127
+ exists = @value.exists?
128
+ exists.should be_true
129
+ end
130
+
131
+ it "should return true if key is existing, but value is nil" do
132
+ @configuration[:test] = nil
133
+ exists = @value.exists?
134
+ exists.should be_true
135
+ end
136
+
137
+ it "should return false if key isn't existing" do
138
+ @configuration.delete :test
139
+ exists = @value.exists?
140
+ exists.should be_false
141
+ end
142
+
143
+ it "should check just the value if configuration doesn't respond to :has_key?" do
144
+ @configuration.stub :respond_to? => false
145
+ exists = @value.exists?
146
+ exists.should be_true
147
+ end
148
+
149
+ end
150
+
151
+ end
@@ -9,7 +9,13 @@ describe Configure do
9
9
  describe "#process" do
10
10
 
11
11
  before :each do
12
- described_class.stub(:process_configuration)
12
+ described_class::Schema.stub :build => :schema
13
+ described_class.stub :process_configuration => nil
14
+ end
15
+
16
+ it "should build an empty schema if none given" do
17
+ described_class::Schema.should_receive(:build).and_return(:schema)
18
+ described_class.process nil, &@block
13
19
  end
14
20
 
15
21
  it "should call :process_configuration" do
@@ -27,6 +33,9 @@ describe Configure do
27
33
 
28
34
  @sandbox = mock described_class::Sandbox, :instance_eval => nil
29
35
  described_class::Sandbox.stub :new => @sandbox
36
+
37
+ @checker = mock described_class::Checker, :check! => nil
38
+ described_class::Checker.stub :new => @checker
30
39
  end
31
40
 
32
41
  it "should initialize the injector" do
@@ -44,6 +53,16 @@ describe Configure do
44
53
  described_class.process_configuration :schema, &@block
45
54
  end
46
55
 
56
+ it "should initialize the checker" do
57
+ described_class::Checker.should_receive(:new).with(:schema, :configuration).and_return(@checker)
58
+ described_class.process_configuration :schema, &@block
59
+ end
60
+
61
+ it "should check the configuration values" do
62
+ @checker.should_receive(:check!)
63
+ described_class.process_configuration :schema, &@block
64
+ end
65
+
47
66
  it "should return the configuration" do
48
67
  configuration = described_class.process_configuration :schema, &@block
49
68
  configuration.should == :configuration
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: configure
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.2.0
5
+ version: 0.3.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - "Philipp Br\xC3\xBCll"
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-03-15 00:00:00 +01:00
13
+ date: 2011-04-08 00:00:00 +02:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -48,14 +48,19 @@ files:
48
48
  - LICENSE
49
49
  - Rakefile
50
50
  - lib/configure.rb
51
+ - lib/configure/value.rb
51
52
  - lib/configure/injector.rb
53
+ - lib/configure/checker.rb
52
54
  - lib/configure/sandbox.rb
53
55
  - lib/configure/schema.rb
54
56
  - spec/lib/configure_spec.rb
55
57
  - spec/lib/configure/injector_spec.rb
56
58
  - spec/lib/configure/schema_spec.rb
59
+ - spec/lib/configure/checker_spec.rb
60
+ - spec/lib/configure/value_spec.rb
57
61
  - spec/lib/configure/sandbox_spec.rb
58
62
  - spec/spec_helper.rb
63
+ - spec/acceptance/schema_spec.rb
59
64
  - spec/acceptance/configure_without_schema_spec.rb
60
65
  - spec/acceptance/configure_with_schema_spec.rb
61
66
  has_rdoc: true
@@ -90,6 +95,9 @@ test_files:
90
95
  - spec/lib/configure_spec.rb
91
96
  - spec/lib/configure/injector_spec.rb
92
97
  - spec/lib/configure/schema_spec.rb
98
+ - spec/lib/configure/checker_spec.rb
99
+ - spec/lib/configure/value_spec.rb
93
100
  - spec/lib/configure/sandbox_spec.rb
101
+ - spec/acceptance/schema_spec.rb
94
102
  - spec/acceptance/configure_without_schema_spec.rb
95
103
  - spec/acceptance/configure_with_schema_spec.rb