store_attribute 0.5.0 → 0.5.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.
- checksums.yaml +4 -4
- data/.gitignore +2 -1
- data/lib/store_attribute/active_record/type/typed_store.rb +13 -5
- data/lib/store_attribute/version.rb +1 -1
- data/spec/cases/store_attribute_spec.rb +23 -0
- data/spec/spec_helper.rb +8 -0
- data/spec/support/money_type.rb +1 -1
- data/spec/support/user.rb +2 -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: cd2643cf4b1909497a0de3f0d15ed6ca51ba599e
|
4
|
+
data.tar.gz: 607171026ea9b9638e20a920b732bd7258db246a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 79e570bd455ecf0f2f7c500725e4a243f4e0d5133c5bdfd45100e10963bbe16623c37396d4a87b7018c74472813efbcfd212cc9d7ebe83c697ca00ad053d338c
|
7
|
+
data.tar.gz: 0c4458f32f0a658d276218894912e3c1c53833dd0cb4e506885bcff00a824beeadda620c89a4d6c09376edd1a262b9396ad317eb275c981d4d08194eb48968da
|
data/.gitignore
CHANGED
@@ -18,23 +18,31 @@ module ActiveRecord
|
|
18
18
|
end
|
19
19
|
|
20
20
|
def add_typed_key(key, type, **options)
|
21
|
-
type =
|
21
|
+
type = ActiveRecord::Type.lookup(type, options) if type.is_a?(Symbol)
|
22
22
|
@accessor_types[key.to_s] = type
|
23
23
|
end
|
24
24
|
|
25
25
|
def deserialize(value)
|
26
26
|
hash = super
|
27
|
-
|
27
|
+
if hash
|
28
|
+
accessor_types.each do |key, type|
|
29
|
+
hash[key] = type.deserialize(hash[key]) if hash.key?(key)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
hash
|
28
33
|
end
|
29
34
|
|
30
35
|
def serialize(value)
|
31
|
-
if value
|
36
|
+
if value.is_a?(Hash)
|
37
|
+
typed_casted = {}
|
32
38
|
accessor_types.each do |key, type|
|
33
39
|
k = key_to_cast(value, key)
|
34
|
-
|
40
|
+
typed_casted[k] = type.serialize(value[k]) unless k.nil?
|
35
41
|
end
|
42
|
+
super(value.merge(typed_casted))
|
43
|
+
else
|
44
|
+
super(value)
|
36
45
|
end
|
37
|
-
super(value)
|
38
46
|
end
|
39
47
|
|
40
48
|
def cast(value)
|
@@ -140,9 +140,32 @@ describe StoreAttribute do
|
|
140
140
|
|
141
141
|
it "typecasts on reload" do
|
142
142
|
jamie = User.create!(custom: { price: '$12' })
|
143
|
+
expect(jamie.reload.price).to eq 1200
|
144
|
+
|
143
145
|
jamie = User.find(jamie.id)
|
144
146
|
|
145
147
|
expect(jamie.price).to eq 1200
|
146
148
|
end
|
147
149
|
end
|
150
|
+
|
151
|
+
context "store subtype" do
|
152
|
+
it "typecasts on build" do
|
153
|
+
user = User.new(inner_json: { x: 1 })
|
154
|
+
expect(user.inner_json).to eq('x' => 1)
|
155
|
+
end
|
156
|
+
|
157
|
+
it "typecasts on update" do
|
158
|
+
user = User.new
|
159
|
+
user.update!(inner_json: { x: 1 })
|
160
|
+
expect(user.inner_json).to eq('x' => 1)
|
161
|
+
|
162
|
+
expect(user.reload.inner_json).to eq('x' => 1)
|
163
|
+
end
|
164
|
+
|
165
|
+
it "typecasts on reload" do
|
166
|
+
jamie = User.create!(inner_json: { x: 1 })
|
167
|
+
jamie = User.find(jamie.id)
|
168
|
+
expect(jamie.inner_json).to eq('x' => 1)
|
169
|
+
end
|
170
|
+
end
|
148
171
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -30,4 +30,12 @@ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
|
|
30
30
|
|
31
31
|
RSpec.configure do |config|
|
32
32
|
config.mock_with :rspec
|
33
|
+
|
34
|
+
config.filter_run_when_matching :focus
|
35
|
+
|
36
|
+
config.example_status_persistence_file_path = "tmp/rspec_examples.txt"
|
37
|
+
|
38
|
+
if config.files_to_run.one?
|
39
|
+
config.default_formatter = 'doc'
|
40
|
+
end
|
33
41
|
end
|
data/spec/support/money_type.rb
CHANGED
data/spec/support/user.rb
CHANGED
@@ -6,6 +6,8 @@ class User < ActiveRecord::Base
|
|
6
6
|
store_accessor :jparams, :version, active: :boolean, salary: :integer
|
7
7
|
store_attribute :jparams, :birthday, :date
|
8
8
|
|
9
|
+
store_attribute :jparams, :inner_json, :json
|
10
|
+
|
9
11
|
store :custom, accessors: [price: :money_type]
|
10
12
|
|
11
13
|
store_accessor :hdata, visible: :boolean
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: store_attribute
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- palkan
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-07-
|
11
|
+
date: 2016-07-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|