spicerack 0.25.3 → 0.25.4

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
  SHA256:
3
- metadata.gz: bfda9212fbb28605aa0117ae3636c5f1bb244a90847aed48781a399b5bcd5222
4
- data.tar.gz: 29a3a0ca7297be6ae910fbf211a274d80d010467ff0de53bcfae6df3e6273075
3
+ metadata.gz: c7afa73ffa6ebc50d2ee2e37e69e9da61bd343d9f58eacb4a34d3d894a0454a5
4
+ data.tar.gz: 271cf1031356c0e921f3c4b27ffb7a35a8a155f05b269fbe7678c7e7dbfa0e5b
5
5
  SHA512:
6
- metadata.gz: 385d1dea3ddd6ec85aae6182f3b9409b220a437dbe564405019df7c7eaa596c972b6f08f5e17a1817e09d4fe83678dc903775c3b44e3143f4e2bafcd0877890f
7
- data.tar.gz: a344da11aca2dcf17a6f1cb84381339a42272c0401c62f34aa72f4da0be43f967ba66af3957999eb3280a53aae7bb8ae3d95186b020750dc02334a30265851a3
6
+ metadata.gz: 553b6da722c20cf74da06bdd74c9d8672a0ab279650a50b00d1ec0db5be76b942c2991e18eaca66c1ac36ada60061de1b7d6ca6616685eafc828dbb855b63bb9
7
+ data.tar.gz: 3e49c44b2accd83a196bef0e65b63601b69f42c66a887cf36738d62645d10d2eb208213d794bfe0601c4d526fd15433c04eae1dd3e7705704717ba4b6e956e2e
@@ -3,6 +3,7 @@
3
3
  require "active_model"
4
4
 
5
5
  module Spicerack
6
+ # Provides on-demand synchronization between a `data' object and ActiveModel::Attributes
6
7
  module HashModel
7
8
  extend ActiveSupport::Concern
8
9
 
@@ -23,6 +24,7 @@ module Spicerack
23
24
  private
24
25
 
25
26
  def field(name, type = Type::Value.new, **options)
27
+ name = name.to_s
26
28
  _fields << name
27
29
  attribute(name, type, **options)
28
30
  define_field_methods(name)
@@ -30,12 +32,36 @@ module Spicerack
30
32
 
31
33
  def define_field_methods(name)
32
34
  define_method("#{name}?".to_sym) { data[name].present? }
33
- define_method("#{name}=".to_sym) { |value| data[name] = value }
35
+ define_method("#{name}=".to_sym) do |value|
36
+ data[name] = value
37
+ synchronize_attribute_from_datastore(name)
38
+ end
34
39
  define_method(name) do
35
- write_attribute(name, data[name] || attribute(name))
40
+ synchronize_attribute_from_datastore(name)
36
41
  attribute(name)
37
42
  end
38
43
  end
39
44
  end
45
+
46
+ private
47
+
48
+ # The data object can be anything from a normal hash to a hash-like object backed by redis.
49
+ def synchronize_attribute_from_datastore(name)
50
+ value_from_datastore = data[name]
51
+ value_from_attribute = attribute(name)
52
+ return if value_from_datastore == value_from_attribute
53
+
54
+ value = value_from_datastore || value_from_attribute
55
+
56
+ # Otherwise, the dataset is value takes priority and is written as the attribute value
57
+ # ActiveModel changed the interface to this method between Rails 6.0 and 6.1
58
+ # This method is a patch which allows this class to work with either version
59
+ # Once support for pre rails 6.0 is sunset this should likely be removed
60
+ if respond_to?(:_write_attribute, true)
61
+ _write_attribute(name, value)
62
+ else
63
+ write_attribute(name, value)
64
+ end
65
+ end
40
66
  end
41
67
  end
@@ -20,7 +20,7 @@ RSpec::Matchers.define :define_field do |field, type = nil, default: nil|
20
20
  match do
21
21
  expect(test_subject.attribute_types[field.to_s].type).to eq type unless type.nil?
22
22
  expect(test_subject._default_attributes[field.to_s].value).to eq default unless default.nil?
23
- expect(test_subject._fields).to include field
23
+ expect(test_subject._fields).to include field.to_s
24
24
  end
25
25
  description { "define field #{field}" }
26
26
  failure_message { "expected #{test_subject} to define field #{field} #{with_details(type, default)}" }
@@ -2,5 +2,5 @@
2
2
 
3
3
  module Spicerack
4
4
  # This constant is managed by spicerack
5
- VERSION = "0.25.3"
5
+ VERSION = "0.25.4"
6
6
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spicerack
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.25.3
4
+ version: 0.25.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eric Garside
@@ -12,7 +12,7 @@ authors:
12
12
  autorequire:
13
13
  bindir: bin
14
14
  cert_chain: []
15
- date: 2020-12-23 00:00:00.000000000 Z
15
+ date: 2021-02-17 00:00:00.000000000 Z
16
16
  dependencies:
17
17
  - !ruby/object:Gem::Dependency
18
18
  name: around_the_world
@@ -20,70 +20,70 @@ dependencies:
20
20
  requirements:
21
21
  - - '='
22
22
  - !ruby/object:Gem::Version
23
- version: 0.25.3
23
+ version: 0.25.4
24
24
  type: :runtime
25
25
  prerelease: false
26
26
  version_requirements: !ruby/object:Gem::Requirement
27
27
  requirements:
28
28
  - - '='
29
29
  - !ruby/object:Gem::Version
30
- version: 0.25.3
30
+ version: 0.25.4
31
31
  - !ruby/object:Gem::Dependency
32
32
  name: redis_hash
33
33
  requirement: !ruby/object:Gem::Requirement
34
34
  requirements:
35
35
  - - '='
36
36
  - !ruby/object:Gem::Version
37
- version: 0.25.3
37
+ version: 0.25.4
38
38
  type: :runtime
39
39
  prerelease: false
40
40
  version_requirements: !ruby/object:Gem::Requirement
41
41
  requirements:
42
42
  - - '='
43
43
  - !ruby/object:Gem::Version
44
- version: 0.25.3
44
+ version: 0.25.4
45
45
  - !ruby/object:Gem::Dependency
46
46
  name: short_circu_it
47
47
  requirement: !ruby/object:Gem::Requirement
48
48
  requirements:
49
49
  - - '='
50
50
  - !ruby/object:Gem::Version
51
- version: 0.25.3
51
+ version: 0.25.4
52
52
  type: :runtime
53
53
  prerelease: false
54
54
  version_requirements: !ruby/object:Gem::Requirement
55
55
  requirements:
56
56
  - - '='
57
57
  - !ruby/object:Gem::Version
58
- version: 0.25.3
58
+ version: 0.25.4
59
59
  - !ruby/object:Gem::Dependency
60
60
  name: technologic
61
61
  requirement: !ruby/object:Gem::Requirement
62
62
  requirements:
63
63
  - - '='
64
64
  - !ruby/object:Gem::Version
65
- version: 0.25.3
65
+ version: 0.25.4
66
66
  type: :runtime
67
67
  prerelease: false
68
68
  version_requirements: !ruby/object:Gem::Requirement
69
69
  requirements:
70
70
  - - '='
71
71
  - !ruby/object:Gem::Version
72
- version: 0.25.3
72
+ version: 0.25.4
73
73
  - !ruby/object:Gem::Dependency
74
74
  name: tablesalt
75
75
  requirement: !ruby/object:Gem::Requirement
76
76
  requirements:
77
77
  - - '='
78
78
  - !ruby/object:Gem::Version
79
- version: 0.25.3
79
+ version: 0.25.4
80
80
  type: :runtime
81
81
  prerelease: false
82
82
  version_requirements: !ruby/object:Gem::Requirement
83
83
  requirements:
84
84
  - - '='
85
85
  - !ruby/object:Gem::Version
86
- version: 0.25.3
86
+ version: 0.25.4
87
87
  - !ruby/object:Gem::Dependency
88
88
  name: bundler
89
89
  requirement: !ruby/object:Gem::Requirement
@@ -208,28 +208,28 @@ dependencies:
208
208
  requirements:
209
209
  - - '='
210
210
  - !ruby/object:Gem::Version
211
- version: 0.25.3
211
+ version: 0.25.4
212
212
  type: :development
213
213
  prerelease: false
214
214
  version_requirements: !ruby/object:Gem::Requirement
215
215
  requirements:
216
216
  - - '='
217
217
  - !ruby/object:Gem::Version
218
- version: 0.25.3
218
+ version: 0.25.4
219
219
  - !ruby/object:Gem::Dependency
220
220
  name: spicerack-styleguide
221
221
  requirement: !ruby/object:Gem::Requirement
222
222
  requirements:
223
223
  - - '='
224
224
  - !ruby/object:Gem::Version
225
- version: 0.25.3
225
+ version: 0.25.4
226
226
  type: :development
227
227
  prerelease: false
228
228
  version_requirements: !ruby/object:Gem::Requirement
229
229
  requirements:
230
230
  - - '='
231
231
  - !ruby/object:Gem::Version
232
- version: 0.25.3
232
+ version: 0.25.4
233
233
  - !ruby/object:Gem::Dependency
234
234
  name: will_paginate
235
235
  requirement: !ruby/object:Gem::Requirement