property_sets 2.5.0 → 2.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/property_sets/active_record_extension.rb +11 -33
- data/lib/property_sets/casting.rb +23 -13
- data/lib/property_sets/property_set_model.rb +4 -6
- data/lib/property_sets/version.rb +1 -1
- metadata +13 -55
- data/.document +0 -5
- data/.gitignore +0 -10
- data/.travis.yml +0 -21
- data/Appraisals +0 -20
- data/Gemfile +0 -16
- data/LICENSE +0 -176
- data/README.md +0 -145
- data/Rakefile +0 -14
- data/benchmark/read.rb +0 -82
- data/gemfiles/rails2.3.gemfile +0 -20
- data/gemfiles/rails3.2.gemfile +0 -19
- data/gemfiles/rails4.0.gemfile +0 -20
- data/property_sets.gemspec +0 -20
- data/test/database.rb +0 -83
- data/test/fixtures/account_settings.yml +0 -14
- data/test/fixtures/account_texts.yml +0 -4
- data/test/fixtures/accounts.yml +0 -3
- data/test/helper.rb +0 -99
- data/test/test_casting.rb +0 -38
- data/test/test_delegator.rb +0 -78
- data/test/test_property_sets.rb +0 -408
- data/test/test_view_extensions.rb +0 -198
data/test/test_casting.rb
DELETED
@@ -1,38 +0,0 @@
|
|
1
|
-
require File.expand_path(File.dirname(__FILE__) + '/helper')
|
2
|
-
require 'property_sets/casting'
|
3
|
-
|
4
|
-
class TestCasting < ActiveSupport::TestCase
|
5
|
-
|
6
|
-
context "Casting#read" do
|
7
|
-
should "return nil when given value nil regardless of type" do
|
8
|
-
assert_equal nil, PropertySets::Casting.read(:string, nil)
|
9
|
-
assert_equal nil, PropertySets::Casting.read(:hello, nil)
|
10
|
-
end
|
11
|
-
|
12
|
-
should "leave serialized data alone" do
|
13
|
-
assert_equal [1,2,3], PropertySets::Casting.read(:serialized, [1, 2, 3])
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
context "Casting#write" do
|
18
|
-
should "return nil when given value nil regardless of type" do
|
19
|
-
assert_equal nil, PropertySets::Casting.write(:string, nil)
|
20
|
-
assert_equal nil, PropertySets::Casting.write(:hello, nil)
|
21
|
-
end
|
22
|
-
|
23
|
-
should "convert time instances to UTC" do
|
24
|
-
time = Time.now.in_time_zone("CET")
|
25
|
-
assert PropertySets::Casting.write(:datetime, time) =~ /UTC$/
|
26
|
-
end
|
27
|
-
|
28
|
-
should "convert integers to strings" do
|
29
|
-
assert_equal "123", PropertySets::Casting.write(:integer, 123)
|
30
|
-
end
|
31
|
-
|
32
|
-
should "leave serialized data alone for the record to deal with" do
|
33
|
-
a = [123]
|
34
|
-
assert_equal a, PropertySets::Casting.write(:serialized, a)
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
end
|
data/test/test_delegator.rb
DELETED
@@ -1,78 +0,0 @@
|
|
1
|
-
require File.expand_path(File.dirname(__FILE__) + '/helper')
|
2
|
-
|
3
|
-
class TestDelegator < ActiveSupport::TestCase
|
4
|
-
context PropertySets::Delegator do
|
5
|
-
fixtures :accounts, :account_settings, :account_texts
|
6
|
-
|
7
|
-
setup do
|
8
|
-
@account = Account.create(:name => "Name")
|
9
|
-
@default = 'skep'
|
10
|
-
end
|
11
|
-
|
12
|
-
context "read" do
|
13
|
-
should "not add a property" do
|
14
|
-
@account.old
|
15
|
-
assert_equal 0, @account.settings.size
|
16
|
-
end
|
17
|
-
|
18
|
-
should "delegate read to default" do
|
19
|
-
assert_equal @default, @account.old
|
20
|
-
end
|
21
|
-
|
22
|
-
should "delegate read to property value" do
|
23
|
-
@account.settings.hep = 'new'
|
24
|
-
assert_equal 'new', @account.old
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
context "write" do
|
29
|
-
should "add a property" do
|
30
|
-
@account.old = 'new'
|
31
|
-
assert_equal 1, @account.settings.size
|
32
|
-
end
|
33
|
-
|
34
|
-
should "delegate write" do
|
35
|
-
@account.old = 'new'
|
36
|
-
assert_equal 'new', @account.settings.hep
|
37
|
-
assert_equal 'new', @account.old
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
context "changed?" do
|
42
|
-
should "not add a property" do
|
43
|
-
@account.old_changed?
|
44
|
-
assert_equal 0, @account.settings.size
|
45
|
-
end
|
46
|
-
|
47
|
-
should "not be changed" do
|
48
|
-
assert_equal false, @account.old_changed?
|
49
|
-
end
|
50
|
-
|
51
|
-
should "be changed with new value" do
|
52
|
-
@account.old = "new"
|
53
|
-
assert_equal true, @account.old_changed?
|
54
|
-
end
|
55
|
-
|
56
|
-
should "not be changed with default value" do
|
57
|
-
@account.old = @default
|
58
|
-
assert_equal false, @account.old_changed?
|
59
|
-
end
|
60
|
-
end
|
61
|
-
|
62
|
-
context "before_type_case" do
|
63
|
-
should "not add a property" do
|
64
|
-
@account.old_before_type_cast
|
65
|
-
assert_equal 0, @account.settings.size
|
66
|
-
end
|
67
|
-
|
68
|
-
should "return default" do
|
69
|
-
assert_equal @default, @account.old_before_type_cast
|
70
|
-
end
|
71
|
-
|
72
|
-
should "return setting" do
|
73
|
-
@account.old = "new"
|
74
|
-
assert_equal "new", @account.old_before_type_cast
|
75
|
-
end
|
76
|
-
end
|
77
|
-
end
|
78
|
-
end
|
data/test/test_property_sets.rb
DELETED
@@ -1,408 +0,0 @@
|
|
1
|
-
require File.expand_path(File.dirname(__FILE__) + '/helper')
|
2
|
-
|
3
|
-
class TestPropertySets < ActiveSupport::TestCase
|
4
|
-
|
5
|
-
context "property sets" do
|
6
|
-
fixtures :accounts, :account_settings, :account_texts
|
7
|
-
|
8
|
-
setup do
|
9
|
-
@account = Account.create(:name => "Name")
|
10
|
-
end
|
11
|
-
|
12
|
-
should "construct the container class" do
|
13
|
-
assert defined?(AccountSetting)
|
14
|
-
assert defined?(AccountText)
|
15
|
-
end
|
16
|
-
|
17
|
-
should "register the property sets used on a class" do
|
18
|
-
[ :settings, :texts, :validations, :typed_data ].each do |name|
|
19
|
-
assert Account.property_set_index.include?(name)
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
should "allow the owner class to be customized" do
|
24
|
-
(Flux = Class.new(ActiveRecord::Base)).property_set(:blot, {
|
25
|
-
:owner_class_name => 'Foobar'
|
26
|
-
}) { property :test }
|
27
|
-
|
28
|
-
assert defined?(FoobarBlot)
|
29
|
-
end
|
30
|
-
|
31
|
-
should "pass-through any options from the second parameter" do
|
32
|
-
Account.expects(:has_many).with { |association, h|
|
33
|
-
association == :foo && h[:conditions] == "bar"
|
34
|
-
}
|
35
|
-
Account.property_set(:foo, :conditions => "bar") {}
|
36
|
-
end
|
37
|
-
|
38
|
-
should "support protecting attributes" do
|
39
|
-
assert @account.settings.protected?(:pro)
|
40
|
-
assert !@account.settings.protected?(:foo)
|
41
|
-
end
|
42
|
-
|
43
|
-
should "allow enabling/disabling a property" do
|
44
|
-
assert @account.settings.hep?
|
45
|
-
@account.settings.disable(:hep)
|
46
|
-
assert !@account.settings.hep?
|
47
|
-
@account.settings.enable(:hep)
|
48
|
-
assert @account.settings.hep?
|
49
|
-
|
50
|
-
account = Account.new
|
51
|
-
assert !account.settings.foo?
|
52
|
-
account.settings.enable(:foo)
|
53
|
-
assert account.settings.foo?
|
54
|
-
end
|
55
|
-
|
56
|
-
should "be empty on a new account" do
|
57
|
-
assert @account.settings.empty?
|
58
|
-
assert @account.texts.empty?
|
59
|
-
|
60
|
-
assert !@account.texts.foo?
|
61
|
-
assert !@account.texts.bar?
|
62
|
-
assert @account.texts.foo.nil?
|
63
|
-
assert @account.texts.bar.nil?
|
64
|
-
end
|
65
|
-
|
66
|
-
should "respond with defaults" do
|
67
|
-
assert_equal false, @account.settings.bar?
|
68
|
-
assert_equal nil, @account.settings.bar
|
69
|
-
assert_equal true, @account.settings.hep?
|
70
|
-
assert_equal 'skep', @account.settings.hep
|
71
|
-
end
|
72
|
-
|
73
|
-
should "be flexible when fetching property data" do
|
74
|
-
assert_equal 'skep', @account.settings.default(:hep)
|
75
|
-
assert_equal 'skep', @account.settings.default('hep')
|
76
|
-
end
|
77
|
-
|
78
|
-
context 'querying for a setting that does not exist' do
|
79
|
-
setup do
|
80
|
-
assert_equal([], @account.settings)
|
81
|
-
assert_equal(true, @account.settings.hep?)
|
82
|
-
end
|
83
|
-
|
84
|
-
should 'not add a new setting' do
|
85
|
-
assert_equal([], @account.settings)
|
86
|
-
end
|
87
|
-
|
88
|
-
should 'give back the default value' do
|
89
|
-
assert_equal('skep', @account.settings.hep)
|
90
|
-
end
|
91
|
-
end
|
92
|
-
|
93
|
-
should "reject settings with an invalid name" do
|
94
|
-
s = AccountSetting.new(:account => @account)
|
95
|
-
|
96
|
-
[ 'hello', 'hel_lo', 'hell0' ].each do |valid|
|
97
|
-
s.name = valid
|
98
|
-
assert s.valid?, "#{valid} is invalid: #{s.errors.inspect}"
|
99
|
-
end
|
100
|
-
|
101
|
-
[ '_hello', :hello ].each do |invalid|
|
102
|
-
s.name = invalid
|
103
|
-
assert !s.valid?, "#{invalid} is valid"
|
104
|
-
end
|
105
|
-
end
|
106
|
-
|
107
|
-
should "validate uniqueness of settings" do
|
108
|
-
@account.settings.create!(:name => "unique")
|
109
|
-
assert_raise ActiveRecord::RecordInvalid do
|
110
|
-
@account.settings.create!(:name => "unique")
|
111
|
-
end
|
112
|
-
end
|
113
|
-
|
114
|
-
should "be creatable using the = operator" do
|
115
|
-
assert !@account.settings.foo?
|
116
|
-
[ "1", "2" ].each do |value|
|
117
|
-
assert @account.settings.foo = value
|
118
|
-
assert @account.settings.foo?
|
119
|
-
assert @account.settings.size == 1
|
120
|
-
end
|
121
|
-
|
122
|
-
assert @account.texts.empty?
|
123
|
-
end
|
124
|
-
|
125
|
-
should "coerce everything but nil to string" do
|
126
|
-
@account.settings.foo = 3
|
127
|
-
@account.save
|
128
|
-
assert @account.settings.foo == "3"
|
129
|
-
@account.settings.foo = nil
|
130
|
-
@account.save
|
131
|
-
assert @account.settings.foo.nil?
|
132
|
-
end
|
133
|
-
|
134
|
-
should "reference the owner instance when constructing a new record" do
|
135
|
-
record = @account.settings.lookup(:baz)
|
136
|
-
assert record.new_record?
|
137
|
-
assert record.account.id == @account.id
|
138
|
-
end
|
139
|
-
|
140
|
-
should "reference the owner instance when constructing a new record ...on a new record" do
|
141
|
-
account = Account.new(:name => "New")
|
142
|
-
record = account.settings.lookup(:baz)
|
143
|
-
|
144
|
-
assert record.new_record?
|
145
|
-
assert record.account == account
|
146
|
-
end
|
147
|
-
|
148
|
-
context "validations" do
|
149
|
-
should "add an error when violated" do
|
150
|
-
@account.validations.validated = "hello"
|
151
|
-
assert !@account.valid?
|
152
|
-
assert_match /BEEP$/, @account.errors.full_messages.first
|
153
|
-
end
|
154
|
-
end
|
155
|
-
|
156
|
-
context "#get" do
|
157
|
-
setup { @account.settings.set(:baz => "456") }
|
158
|
-
|
159
|
-
should "fetch property pairs with string arguments" do
|
160
|
-
assert @account.settings.lookup_without_default(:baz)
|
161
|
-
assert_equal({"baz" => "456"}, @account.settings.get(["baz"]))
|
162
|
-
end
|
163
|
-
|
164
|
-
should "fetch property pairs with symbol arguments" do
|
165
|
-
assert_equal({"baz" => "456"}, @account.settings.get([:baz]))
|
166
|
-
end
|
167
|
-
|
168
|
-
should "return all property pairs if no arguments are provided" do
|
169
|
-
assert_same_elements(
|
170
|
-
["foo", "bar", "baz", "hep", "pro"],
|
171
|
-
@account.settings.get.keys
|
172
|
-
)
|
173
|
-
end
|
174
|
-
|
175
|
-
should "ignore non-existent keys" do
|
176
|
-
assert_equal({"baz" => "456"}, @account.settings.get([:baz, :red]))
|
177
|
-
end
|
178
|
-
|
179
|
-
should "include default property pairs" do
|
180
|
-
assert_nil @account.settings.lookup_without_default(:hep)
|
181
|
-
assert_equal({"hep" => "skep"}, @account.settings.get(["hep"]))
|
182
|
-
end
|
183
|
-
|
184
|
-
should "return a hash with values that can be fetched by string or symbol" do
|
185
|
-
assert_equal "456", @account.settings.get(["baz"]).fetch(:baz)
|
186
|
-
end
|
187
|
-
|
188
|
-
should "return serialized values" do
|
189
|
-
@account.typed_data.set(:serialized_prop => [1, 2])
|
190
|
-
assert @account.typed_data.lookup_without_default(:serialized_prop)
|
191
|
-
assert_equal({"serialized_prop" => [1, 2]}, @account.typed_data.get([:serialized_prop]))
|
192
|
-
end
|
193
|
-
end
|
194
|
-
|
195
|
-
context "#set" do
|
196
|
-
should "support writing multiple values to the association" do
|
197
|
-
assert !@account.settings.foo?
|
198
|
-
assert !@account.settings.bar?
|
199
|
-
|
200
|
-
@account.settings.set(:foo => "123", :bar => "456")
|
201
|
-
|
202
|
-
assert @account.settings.foo?
|
203
|
-
assert @account.settings.bar?
|
204
|
-
end
|
205
|
-
|
206
|
-
should "convert string keys to symbols to ensure consistent lookup" do
|
207
|
-
@account.settings.set(:foo => "123")
|
208
|
-
@account.settings.set("foo" => "456")
|
209
|
-
assert @account.save!
|
210
|
-
end
|
211
|
-
|
212
|
-
should "work identically for new and existing owner objects" do
|
213
|
-
[ @account, Account.new(:name => "Mibble") ].each do |account|
|
214
|
-
account.settings.set(:foo => "123", :bar => "456")
|
215
|
-
|
216
|
-
assert_equal account.settings.size, 2
|
217
|
-
assert_equal account.settings.foo, "123"
|
218
|
-
assert_equal account.settings.bar, "456"
|
219
|
-
|
220
|
-
account.settings.set(:bar => "789", :baz => "012")
|
221
|
-
|
222
|
-
assert_equal account.settings.size, 3
|
223
|
-
assert_equal account.settings.foo, "123"
|
224
|
-
assert_equal account.settings.bar, "789"
|
225
|
-
assert_equal account.settings.baz, "012"
|
226
|
-
end
|
227
|
-
end
|
228
|
-
|
229
|
-
should "be updateable as AR nested attributes" do
|
230
|
-
assert @account.texts_attributes = [{ :name => "foo", :value => "1" }, { :name => "bar", :value => "0" }]
|
231
|
-
@account.save!
|
232
|
-
|
233
|
-
assert @account.texts.foo == "1"
|
234
|
-
assert @account.texts.bar == "0"
|
235
|
-
|
236
|
-
@account.update_attributes!(:texts_attributes => [
|
237
|
-
{ :id => @account.texts.lookup(:foo).id, :name => "foo", :value => "0" },
|
238
|
-
{ :id => @account.texts.lookup(:bar).id, :name => "bar", :value => "1" }
|
239
|
-
])
|
240
|
-
|
241
|
-
assert @account.texts.foo == "0"
|
242
|
-
assert @account.texts.bar == "1"
|
243
|
-
end
|
244
|
-
|
245
|
-
should "be updateable as a nested structure" do
|
246
|
-
@account.settings.baz = "1"
|
247
|
-
@account.save!
|
248
|
-
|
249
|
-
assert !@account.settings.foo?
|
250
|
-
assert !@account.settings.bar?
|
251
|
-
assert @account.settings.baz?
|
252
|
-
assert !@account.settings.pro?
|
253
|
-
|
254
|
-
@account.update_attributes!(
|
255
|
-
:name => "Kim",
|
256
|
-
:settings => { :foo => "1", :baz => "0", :pro => "1" }
|
257
|
-
)
|
258
|
-
|
259
|
-
@account.reload
|
260
|
-
|
261
|
-
# set
|
262
|
-
assert @account.settings.foo?
|
263
|
-
assert_equal "1", @account.settings.foo
|
264
|
-
|
265
|
-
# kept
|
266
|
-
assert !@account.settings.bar?
|
267
|
-
assert_equal nil, @account.settings.bar
|
268
|
-
|
269
|
-
# unset
|
270
|
-
assert !@account.settings.baz?
|
271
|
-
assert_equal "0", @account.settings.baz
|
272
|
-
|
273
|
-
# protected -> not set
|
274
|
-
assert !@account.settings.pro?
|
275
|
-
assert_equal nil, @account.settings.pro
|
276
|
-
end
|
277
|
-
end
|
278
|
-
|
279
|
-
context "lookup" do
|
280
|
-
context "with data" do
|
281
|
-
setup { @account.texts.foo = "1" }
|
282
|
-
|
283
|
-
should "return the data" do
|
284
|
-
assert_equal "1", @account.texts.lookup(:foo).value
|
285
|
-
end
|
286
|
-
end
|
287
|
-
|
288
|
-
context "without data" do
|
289
|
-
should "create a new record, returning the default" do
|
290
|
-
assert_equal nil, @account.texts.lookup(:foo).value
|
291
|
-
assert @account.texts.detect { |p| p.name == "foo" }
|
292
|
-
end
|
293
|
-
end
|
294
|
-
end
|
295
|
-
|
296
|
-
context "lookup_without_default" do
|
297
|
-
should "return the row if it exists" do
|
298
|
-
@account.texts.foo = "1"
|
299
|
-
assert_equal "1", @account.texts.lookup_without_default(:foo).value
|
300
|
-
end
|
301
|
-
|
302
|
-
should "return nil otherwise" do
|
303
|
-
assert_equal nil, @account.texts.lookup_without_default(:foo)
|
304
|
-
end
|
305
|
-
end
|
306
|
-
|
307
|
-
context "save" do
|
308
|
-
should "call save on all dem records" do
|
309
|
-
@account.settings.foo = "1"
|
310
|
-
@account.settings.bar = "2"
|
311
|
-
@account.settings.save
|
312
|
-
|
313
|
-
@account.reload
|
314
|
-
assert_equal "1", @account.settings.foo
|
315
|
-
assert_equal "2", @account.settings.bar
|
316
|
-
end
|
317
|
-
end
|
318
|
-
|
319
|
-
context "typed columns" do
|
320
|
-
|
321
|
-
should "typecast the default value" do
|
322
|
-
assert_equal 123, @account.typed_data.default(:default_prop)
|
323
|
-
end
|
324
|
-
|
325
|
-
context "string data" do
|
326
|
-
should "be writable and readable" do
|
327
|
-
@account.typed_data.string_prop = "foo"
|
328
|
-
assert_equal "foo", @account.typed_data.string_prop
|
329
|
-
end
|
330
|
-
end
|
331
|
-
|
332
|
-
context "floating point data" do
|
333
|
-
should "be writable and readable" do
|
334
|
-
@account.typed_data.float_prop = 1.97898
|
335
|
-
assert_equal 1.97898, @account.typed_data.float_prop
|
336
|
-
@account.save!
|
337
|
-
assert_equal 1.97898, @account.typed_data.float_prop
|
338
|
-
end
|
339
|
-
end
|
340
|
-
|
341
|
-
context "integer data" do
|
342
|
-
should "be writable and readable" do
|
343
|
-
@account.typed_data.int_prop = 25
|
344
|
-
assert_equal 25, @account.typed_data.int_prop
|
345
|
-
@account.save!
|
346
|
-
assert_equal 25, @account.typed_data.int_prop
|
347
|
-
|
348
|
-
assert_equal "25", @account.typed_data.lookup("int_prop").value
|
349
|
-
end
|
350
|
-
end
|
351
|
-
|
352
|
-
context "datetime data" do
|
353
|
-
should "be writable and readable" do
|
354
|
-
ts = Time.at(Time.now.to_i)
|
355
|
-
@account.typed_data.datetime_prop = ts
|
356
|
-
|
357
|
-
assert_equal ts, @account.typed_data.datetime_prop
|
358
|
-
@account.save!
|
359
|
-
assert_equal ts, @account.typed_data.datetime_prop
|
360
|
-
end
|
361
|
-
|
362
|
-
should "store data in UTC" do
|
363
|
-
ts = Time.at(Time.now.to_i)
|
364
|
-
string_rep = ts.in_time_zone("UTC").to_s
|
365
|
-
@account.typed_data.datetime_prop = ts
|
366
|
-
@account.save!
|
367
|
-
assert_equal string_rep, @account.typed_data.lookup("datetime_prop").value
|
368
|
-
end
|
369
|
-
end
|
370
|
-
|
371
|
-
context "serialized data" do
|
372
|
-
should "store data in json" do
|
373
|
-
value = {:a => 1, :b => 2}
|
374
|
-
@account.typed_data.serialized_prop = value
|
375
|
-
@account.save!
|
376
|
-
@account.reload
|
377
|
-
assert_equal({'a' => 1, 'b' => 2}, @account.typed_data.serialized_prop)
|
378
|
-
end
|
379
|
-
|
380
|
-
should "retrieve default values from JSON" do
|
381
|
-
assert_equal([], @account.typed_data.serialized_prop_with_default)
|
382
|
-
end
|
383
|
-
|
384
|
-
should "not overflow the column" do
|
385
|
-
@account.typed_data.serialized_prop = (1..100_000).to_a
|
386
|
-
assert !@account.typed_data.lookup(:serialized_prop).valid?
|
387
|
-
assert !@account.save
|
388
|
-
end
|
389
|
-
|
390
|
-
should "allow for destructive operators" do
|
391
|
-
value = {:a => 1, :b => 2}
|
392
|
-
@account.typed_data.serialized_prop = value
|
393
|
-
@account.typed_data.serialized_prop[:c] = 3
|
394
|
-
assert_equal 3, @account.typed_data.serialized_prop[:c]
|
395
|
-
end
|
396
|
-
|
397
|
-
should "deal with nil values properly going in" do
|
398
|
-
@account.typed_data.serialized_prop = nil
|
399
|
-
@account.save!
|
400
|
-
end
|
401
|
-
|
402
|
-
should "deal with nil values properly coming out" do
|
403
|
-
assert_equal nil, @account.typed_data.serialized_prop
|
404
|
-
end
|
405
|
-
end
|
406
|
-
end
|
407
|
-
end
|
408
|
-
end
|