attr_pouch 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +12 -11
- data/lib/attr_pouch.rb +3 -7
- data/lib/attr_pouch/version.rb +1 -1
- data/spec/attr_pouch_spec.rb +8 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6792e976af41ddb0abe8289d8e69531052566041
|
4
|
+
data.tar.gz: 829f407d909836b525d760c9e9cd5f392f7eadee
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: '0975551cc8167bba0327fcc9177de6891da3da649174a98518757b1f75cc0d6eba5511ab2dfe64fb19e14ff6d2787b239986c7861a2f8d51f620cbeeef8eec7b'
|
7
|
+
data.tar.gz: 2a10e8e7608f667fb0f4f819cbc223dd8921df54995e5319469721681923ed79138af64c4fa41b23c5911b03bc768878bf1cff076d65cac6bc514bfdef4d2e7a
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -230,7 +230,7 @@ abbas.lucky = false # raises ImmutableFieldUpdateError
|
|
230
230
|
|
231
231
|
#### Renaming fields
|
232
232
|
|
233
|
-
Fields can be renamed by providing
|
233
|
+
Fields can be renamed by providing a previous name or an array of
|
234
234
|
previous names under the `was` option.
|
235
235
|
|
236
236
|
```ruby
|
@@ -245,7 +245,7 @@ end
|
|
245
245
|
|
246
246
|
nils = User[name: 'nils'] # in db we have `{ ssl?: true, byzantion?: true }`
|
247
247
|
nils.tls? # true
|
248
|
-
nils.
|
248
|
+
nils.istanbul? # true
|
249
249
|
```
|
250
250
|
|
251
251
|
Note that no direct accessors are defined for the old names, and if
|
@@ -253,22 +253,23 @@ the value is updated, it is written under the new name and any old
|
|
253
253
|
values in the pouch are deleted:
|
254
254
|
|
255
255
|
```ruby
|
256
|
+
nils.byzantion? # raises NoMethodError
|
256
257
|
nils.istanbul = false
|
257
|
-
nils.tls?
|
258
|
-
nils.instanbul?
|
259
|
-
nils.save_changes
|
258
|
+
nils.tls? # true
|
259
|
+
nils.instanbul? # false
|
260
|
+
nils.save_changes # now in db as `{ tls?: true, instanbul?: false }`
|
260
261
|
```
|
261
262
|
|
262
263
|
#### Raw value access
|
263
264
|
|
264
265
|
Any field can be accessed directly, bypassing the encoder and decoder,
|
265
266
|
by specifying the `raw_field` option to provide the name of the setter
|
266
|
-
and getter that will
|
267
|
-
Required fields are still required when read via
|
268
|
-
immutable fields are still immutable, but if a
|
269
|
-
raw value will be `nil`, rather than the default
|
270
|
-
user to distinguish between a field value equal
|
271
|
-
absent field value deferring to the default:
|
267
|
+
and getter that will be created to manipulate the underlying value
|
268
|
+
directly. Required fields are still required when read via
|
269
|
+
`raw_field`, and immutable fields are still immutable, but if a
|
270
|
+
`default` is set, the raw value will be `nil`, rather than the default
|
271
|
+
itself, to allow the user to distinguish between a field value equal
|
272
|
+
to the default and an absent field value deferring to the default:
|
272
273
|
|
273
274
|
```ruby
|
274
275
|
class User < Sequel::Model
|
data/lib/attr_pouch.rb
CHANGED
@@ -308,10 +308,6 @@ module AttrPouch
|
|
308
308
|
end
|
309
309
|
end
|
310
310
|
|
311
|
-
def default_pouch
|
312
|
-
wrap({})
|
313
|
-
end
|
314
|
-
|
315
311
|
def field(name, opts={})
|
316
312
|
unless VALID_FIELD_NAME_REGEXP.match(name)
|
317
313
|
raise InvalidFieldError, "Field name must match #{VALID_FIELD_NAME_REGEXP}"
|
@@ -321,7 +317,7 @@ module AttrPouch
|
|
321
317
|
@fields[name.to_s] = field
|
322
318
|
|
323
319
|
storage_field = @storage_field
|
324
|
-
|
320
|
+
default_store = wrap({})
|
325
321
|
|
326
322
|
@host.class_eval do
|
327
323
|
define_method(name) do
|
@@ -332,7 +328,7 @@ module AttrPouch
|
|
332
328
|
define_method("#{name.to_s.sub(/\?\z/, '')}=") do |value|
|
333
329
|
store = self[storage_field]
|
334
330
|
was_nil = store.nil?
|
335
|
-
store =
|
331
|
+
store = default_store.dup if was_nil
|
336
332
|
changed = field.write(store, value)
|
337
333
|
if was_nil
|
338
334
|
self[storage_field] = store
|
@@ -368,7 +364,7 @@ module AttrPouch
|
|
368
364
|
define_method("#{raw_name.to_s.sub(/\?\z/, '')}=") do |value|
|
369
365
|
store = self[storage_field]
|
370
366
|
was_nil = store.nil?
|
371
|
-
store =
|
367
|
+
store = default_store.dup if was_nil
|
372
368
|
changed = field.write(store, value, encode: false)
|
373
369
|
if was_nil
|
374
370
|
self[storage_field] = store
|
data/lib/attr_pouch/version.rb
CHANGED
data/spec/attr_pouch_spec.rb
CHANGED
@@ -57,6 +57,14 @@ describe AttrPouch do
|
|
57
57
|
expect(pouchy.foo).to eq('bar')
|
58
58
|
end
|
59
59
|
|
60
|
+
it "does not share stores across object instances" do
|
61
|
+
pouchy1 = pouchy.class.new(foo: "bar")
|
62
|
+
pouchy2 = pouchy.class.new(foo: "baz")
|
63
|
+
pouch1_id = pouchy1[column_name].object_id
|
64
|
+
pouch2_id = pouchy2[column_name].object_id
|
65
|
+
expect(pouch1_id).not_to eq(pouch2_id)
|
66
|
+
end
|
67
|
+
|
60
68
|
it "avoids marking the field as modified if it is not changing" do
|
61
69
|
pouchy.foo = 'bar'
|
62
70
|
expect(pouchy.save_changes).to_not be_nil
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: attr_pouch
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Maciek Sakrejda
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-12-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|