configurations 1.3.0 → 1.3.1
Sign up to get free protection for your applications and to get access to all the features.
data/lib/configurations.rb
CHANGED
@@ -54,7 +54,7 @@ module Configurations
|
|
54
54
|
def configurable(*properties, &block)
|
55
55
|
type = properties.shift if properties.first.is_a?(Class)
|
56
56
|
@configurable ||= {}
|
57
|
-
@configurable.merge!
|
57
|
+
@configurable.merge! to_configurable_hash(properties, type, &block)
|
58
58
|
end
|
59
59
|
|
60
60
|
# returns whether a property is set to be configurable
|
@@ -65,8 +65,10 @@ module Configurations
|
|
65
65
|
_assign!(property, value)
|
66
66
|
elsif !_is_writer?(method) && @_writeable || _configured?(method)
|
67
67
|
@configuration[method]
|
68
|
-
|
68
|
+
elsif ::Kernel.respond_to?(method, true)
|
69
69
|
::Kernel.send(method, *args, &block)
|
70
|
+
else
|
71
|
+
super
|
70
72
|
end
|
71
73
|
end
|
72
74
|
|
@@ -112,6 +114,11 @@ module Configurations
|
|
112
114
|
@configurable.nil? or @configurable.empty?
|
113
115
|
end
|
114
116
|
|
117
|
+
# @return [Hash] the configurations configurable hash
|
118
|
+
def _configurable
|
119
|
+
@configurable
|
120
|
+
end
|
121
|
+
|
115
122
|
private
|
116
123
|
|
117
124
|
# @param [Symbol] property The property to test for
|
@@ -137,19 +144,23 @@ module Configurations
|
|
137
144
|
@configurable.each do |k, assertion|
|
138
145
|
if k.is_a?(::Hash)
|
139
146
|
k.each do |property, nested|
|
140
|
-
@configuration[property] = Configuration.new(nil,
|
147
|
+
@configuration[property] = Configuration.new(nil, _configurable_hash(property, nested, assertion))
|
141
148
|
end
|
142
149
|
end
|
143
150
|
end
|
144
151
|
end
|
145
152
|
|
153
|
+
# @param [Symbol, Hash, Array] property configurable properties, either single or nested
|
146
154
|
# @param [Symbol, Hash, Array] value configurable properties, either single or nested
|
147
|
-
# @param [
|
155
|
+
# @param [Hash] assertions if any
|
148
156
|
# @return a hash with configurable values pointing to their types
|
149
157
|
#
|
150
|
-
def
|
158
|
+
def _configurable_hash(property, value, assertion)
|
151
159
|
value = [value] unless value.is_a?(::Array)
|
152
|
-
::Hash[value.zip([assertion].flatten*value.size)]
|
160
|
+
hash = ::Hash[value.zip([assertion].flatten*value.size)]
|
161
|
+
hash = @configuration[property]._configurable.merge(hash) if @configuration.has_key?(property)
|
162
|
+
|
163
|
+
hash
|
153
164
|
end
|
154
165
|
|
155
166
|
# Assigns a value after running the assertions
|
@@ -172,7 +183,7 @@ module Configurations
|
|
172
183
|
return unless _evaluable?(property, :type)
|
173
184
|
|
174
185
|
assertion = @configurable[property][:type]
|
175
|
-
::Kernel.raise ConfigurationError, "Expected #{property} to be configured with #{
|
186
|
+
::Kernel.raise ConfigurationError, "Expected #{property} to be configured with #{assertion}, but got #{value.class.inspect}", caller unless value.is_a?(assertion)
|
176
187
|
end
|
177
188
|
|
178
189
|
# Block assertion for configurable properties
|
@@ -7,7 +7,10 @@ class TestStricterConfiguration < Minitest::Test
|
|
7
7
|
configurable :property1, :property2
|
8
8
|
configurable String, :property3
|
9
9
|
configurable Symbol, property4: :property5, property6: [:property7, :property8]
|
10
|
-
configurable
|
10
|
+
configurable Fixnum, property6: :property14
|
11
|
+
configurable Fixnum, property4: :property12
|
12
|
+
configurable Array, property9: { property10: { property11: :property12 } }
|
13
|
+
configurable Hash, property9: { property10: { property11: :property13 } }
|
11
14
|
end
|
12
15
|
|
13
16
|
def setup
|
@@ -16,9 +19,12 @@ class TestStricterConfiguration < Minitest::Test
|
|
16
19
|
c.property2 = 'BASIC2'
|
17
20
|
c.property3 = 'STRING'
|
18
21
|
c.property4.property5 = :something
|
22
|
+
c.property4.property12 = 12
|
19
23
|
c.property6.property7 = :anything
|
20
24
|
c.property6.property8 = :everything
|
21
|
-
c.
|
25
|
+
c.property6.property14 = 555
|
26
|
+
c.property9.property10.property11.property12 = %w(here I am)
|
27
|
+
c.property9.property10.property11.property13 = { hi: :bye }
|
22
28
|
end
|
23
29
|
|
24
30
|
@configuration = StrictConfigurationTestModule.configuration
|
@@ -33,13 +39,41 @@ class TestStricterConfiguration < Minitest::Test
|
|
33
39
|
assert_equal :something, @configuration.property4.property5
|
34
40
|
end
|
35
41
|
|
42
|
+
def test_configurable_with_nested_calls_and_deeply_nested_property
|
43
|
+
assert_equal 12, @configuration.property4.property12
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_configurable_with_nested_calls_and_added_property
|
47
|
+
assert_equal 555, @configuration.property6.property14
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_configurable_type_assertion_with_nested_calls_and_added_property
|
51
|
+
assert_raises Configurations::ConfigurationError do
|
52
|
+
StrictConfigurationTestModule.configure do |c|
|
53
|
+
c.property6.property14 = ''
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
36
58
|
def test_configurable_with_same_key_when_set_nested_configurable
|
37
59
|
assert_equal :anything, @configuration.property6.property7
|
38
60
|
assert_equal :everything, @configuration.property6.property8
|
39
61
|
end
|
40
62
|
|
41
63
|
def test_configurable_with_deeply_nested_property
|
42
|
-
assert_equal %w(here I am), @configuration.property9.property10.property11
|
64
|
+
assert_equal %w(here I am), @configuration.property9.property10.property11.property12
|
65
|
+
end
|
66
|
+
|
67
|
+
def test_configurable_with_nested_calls_and_deeply_nested_property
|
68
|
+
assert_equal({ hi: :bye }, @configuration.property9.property10.property11.property13)
|
69
|
+
end
|
70
|
+
|
71
|
+
def test_configurable_type_restricted_with_nested_calls_and_deeply_nested_property
|
72
|
+
assert_raises Configurations::ConfigurationError do
|
73
|
+
StrictConfigurationTestModule.configure do |c|
|
74
|
+
c.property9.property10.property11.property13 = ''
|
75
|
+
end
|
76
|
+
end
|
43
77
|
end
|
44
78
|
|
45
79
|
def test_not_configurable_with_wrong_type
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: configurations
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.3.
|
4
|
+
version: 1.3.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-08-
|
12
|
+
date: 2014-08-18 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: minitest
|