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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0bc7099e267a11a0723eaa8b16654b6221e928bc
4
- data.tar.gz: 79fad57b11a02ac0b0093c4d579c7fc109dbbb70
3
+ metadata.gz: 6792e976af41ddb0abe8289d8e69531052566041
4
+ data.tar.gz: 829f407d909836b525d760c9e9cd5f392f7eadee
5
5
  SHA512:
6
- metadata.gz: 4700d4f626b53b6d3dae2211d16e14288b8d51327f8059f8520356bf0de0f977b023fc9d6e1b32c8ec13f6b70093cb9fab0a80e09c05ef19b7e7772afc82f808
7
- data.tar.gz: 83accbe6882fef94e44d1ab5aae58a2d66c811b792ce68b0a19f99032158a96e42d9547ed7305b147bd6d2035410948d1c75a84393dc0ced0e7d4c9178be41bd
6
+ metadata.gz: '0975551cc8167bba0327fcc9177de6891da3da649174a98518757b1f75cc0d6eba5511ab2dfe64fb19e14ff6d2787b239986c7861a2f8d51f620cbeeef8eec7b'
7
+ data.tar.gz: 2a10e8e7608f667fb0f4f819cbc223dd8921df54995e5319469721681923ed79138af64c4fa41b23c5911b03bc768878bf1cff076d65cac6bc514bfdef4d2e7a
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- attr_pouch (0.1.1)
4
+ attr_pouch (0.2.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
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 an a previous name or array of
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.constantinople? # true
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? # true
258
- nils.instanbul? # false
259
- nils.save_changes # now in db as `{ tls?: true, instanbul?: false }`
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 directly manipulate the underlying value.
267
- Required fields are still required when read via `raw_field`, and
268
- immutable fields are still immutable, but if a `default` is set, the
269
- raw value will be `nil`, rather than the default itself, to allow the
270
- user to distinguish between a field value equal to the default and an
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
@@ -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
- default = default_pouch
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 = default if was_nil
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 = default if was_nil
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
@@ -1,3 +1,3 @@
1
1
  module AttrPouch
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -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.1.1
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-08-22 00:00:00.000000000 Z
11
+ date: 2017-12-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec