ns-options 1.1.0 → 1.1.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/ns-options/namespace_data.rb +8 -2
- data/lib/ns-options/option.rb +1 -6
- data/lib/ns-options/version.rb +1 -1
- data/lib/ns-options.rb +8 -0
- data/test/unit/namespace_data_tests.rb +42 -18
- metadata +4 -4
@@ -47,7 +47,11 @@ module NsOptions
|
|
47
47
|
# for the option/namespace name-keys
|
48
48
|
def to_hash
|
49
49
|
Hash.new.tap do |hash|
|
50
|
-
@child_options.each
|
50
|
+
@child_options.each do |name, opt|
|
51
|
+
# this is meant to be a "value exporter", so always use distinct values
|
52
|
+
# on the returned hash to prevent unintentional pass-by-ref shared objects
|
53
|
+
hash[name.to_sym] = NsOptions.distinct_value(opt.value)
|
54
|
+
end
|
51
55
|
@child_namespaces.each{|name, value| hash[name.to_sym] = value.to_hash}
|
52
56
|
end
|
53
57
|
end
|
@@ -60,8 +64,10 @@ module NsOptions
|
|
60
64
|
# recursively apply namespace values if hash given; ignore otherwise.
|
61
65
|
get_namespace(name).apply(value) if value.kind_of?(Hash)
|
62
66
|
else
|
67
|
+
# this is meant as a "value importer", so always apply distinct values
|
68
|
+
# to prevent unintentional pass-by-ref shared objects.
|
63
69
|
# be sure to use the namespace's writer to write the option value
|
64
|
-
@ns.send("#{name}=", value)
|
70
|
+
@ns.send("#{name}=", NsOptions.distinct_value(value))
|
65
71
|
end
|
66
72
|
end
|
67
73
|
end
|
data/lib/ns-options/option.rb
CHANGED
@@ -52,12 +52,7 @@ module NsOptions
|
|
52
52
|
end
|
53
53
|
|
54
54
|
def reset
|
55
|
-
|
56
|
-
self.rules[:default].clone
|
57
|
-
rescue TypeError
|
58
|
-
self.rules[:default]
|
59
|
-
end
|
60
|
-
save_value(default_value)
|
55
|
+
save_value NsOptions.distinct_value(self.rules[:default])
|
61
56
|
end
|
62
57
|
|
63
58
|
def is_set?
|
data/lib/ns-options/version.rb
CHANGED
data/lib/ns-options.rb
CHANGED
@@ -8,6 +8,14 @@ module NsOptions
|
|
8
8
|
receiver.class_eval { extend NsOptions::DSL }
|
9
9
|
end
|
10
10
|
|
11
|
+
def self.distinct_value(value)
|
12
|
+
begin
|
13
|
+
value.clone
|
14
|
+
rescue TypeError
|
15
|
+
value
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
11
19
|
module DSL
|
12
20
|
|
13
21
|
# This is the main DSL method for creating a namespace of options for your
|
@@ -127,22 +127,6 @@ class NsOptions::NamespaceData
|
|
127
127
|
assert_not subject.has_namespace? 'dlakjsglasdjgaklsdgjas'
|
128
128
|
end
|
129
129
|
|
130
|
-
should "return its Hash representation" do
|
131
|
-
exp_hash = {
|
132
|
-
:first => nil, :second => nil, :third => nil,
|
133
|
-
:child_a => {
|
134
|
-
:fourth => nil, :fifth => nil,
|
135
|
-
:child_b => { :sixth => nil }
|
136
|
-
}
|
137
|
-
}
|
138
|
-
assert_equal(exp_hash, subject.to_hash)
|
139
|
-
end
|
140
|
-
|
141
|
-
should "apply a given hash value to itself" do
|
142
|
-
subject.apply(@named_values)
|
143
|
-
assert_equal @named_values, subject.to_hash
|
144
|
-
end
|
145
|
-
|
146
130
|
end
|
147
131
|
|
148
132
|
class EachTests < HandlingTests
|
@@ -213,7 +197,7 @@ class NsOptions::NamespaceData
|
|
213
197
|
|
214
198
|
end
|
215
199
|
|
216
|
-
class
|
200
|
+
class HashHandlingTests < BaseTests
|
217
201
|
setup do
|
218
202
|
@data.define do
|
219
203
|
option :first; option :second; option :third
|
@@ -223,21 +207,61 @@ class NsOptions::NamespaceData
|
|
223
207
|
end
|
224
208
|
end
|
225
209
|
|
210
|
+
@shared_array = []
|
226
211
|
@named_values = {
|
227
212
|
:first => "1", :second => "2", :third => "3", :twenty_one => "21",
|
228
213
|
:child_a => {
|
229
214
|
:fourth => "4", :fifth => "5",
|
230
215
|
:child_b => { :sixth => "6" }
|
231
216
|
},
|
232
|
-
:child_c => { :what => "?" }
|
217
|
+
:child_c => { :what => "?" },
|
218
|
+
:shared => @shared_array
|
233
219
|
}
|
234
220
|
end
|
235
221
|
|
222
|
+
end
|
223
|
+
|
224
|
+
class ToHashTests < HashHandlingTests
|
225
|
+
|
226
|
+
should "return its Hash representation" do
|
227
|
+
exp_hash = {
|
228
|
+
:first => nil, :second => nil, :third => nil,
|
229
|
+
:child_a => {
|
230
|
+
:fourth => nil, :fifth => nil,
|
231
|
+
:child_b => { :sixth => nil }
|
232
|
+
}
|
233
|
+
}
|
234
|
+
assert_equal(exp_hash, subject.to_hash)
|
235
|
+
end
|
236
|
+
|
237
|
+
should "use distinct values in its Hash representation" do
|
238
|
+
subject.add_option(:shared)
|
239
|
+
subject.set_option(:shared, [])
|
240
|
+
hash_rep = subject.to_hash
|
241
|
+
subject.get_option(:shared) << 1
|
242
|
+
|
243
|
+
assert_not_empty subject.get_option(:shared)
|
244
|
+
assert_empty hash_rep[:shared]
|
245
|
+
assert_not_equal subject.get_option(:shared).object_id, hash_rep[:shared].object_id
|
246
|
+
end
|
247
|
+
|
248
|
+
end
|
249
|
+
|
250
|
+
class ApplyTests < HashHandlingTests
|
251
|
+
|
236
252
|
should "apply a given hash value to itself" do
|
237
253
|
subject.apply(@named_values)
|
238
254
|
assert_equal @named_values, subject.to_hash
|
239
255
|
end
|
240
256
|
|
257
|
+
should "use distinct values when applying hashes" do
|
258
|
+
subject.apply(@named_values)
|
259
|
+
@shared_array << 1
|
260
|
+
|
261
|
+
assert_empty subject.get_option(:shared)
|
262
|
+
assert_not_equal @shared_array.object_id, subject.get_option(:shared).object_id
|
263
|
+
end
|
264
|
+
|
241
265
|
should "ignore applying non-hash values to namespaces" do
|
242
266
|
assert_nil subject.ns.child_a.fourth
|
243
267
|
prev_child_b = subject.ns.child_a.child_b.dup
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ns-options
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 17
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 1
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 1.1.
|
9
|
+
- 1
|
10
|
+
version: 1.1.1
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Collin Redding
|
@@ -16,7 +16,7 @@ autorequire:
|
|
16
16
|
bindir: bin
|
17
17
|
cert_chain: []
|
18
18
|
|
19
|
-
date: 2012-12-
|
19
|
+
date: 2012-12-18 00:00:00 Z
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
22
22
|
name: assert
|