store_attribute 0.4.0 → 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +2 -1
- data/lib/store_attribute/active_record/type/typed_store.rb +12 -4
- data/lib/store_attribute/version.rb +1 -1
- data/spec/cases/store_attribute_spec.rb +23 -0
- data/spec/spec_helper.rb +9 -0
- 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: 5b686f8993ef12feb2c0a1b36dcf90fbf0a7225b
|
4
|
+
data.tar.gz: 5498a60d8e4ac7ce66e4316b5fe2dfb50856c662
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b41a03c88333430a322a2b0147b445284c4335063fcfaccaae53200b8438cad9a22996b1951df936da18fbbde23bd57d06847a49042971346896e181a9729e86
|
7
|
+
data.tar.gz: eb62f09e1f44a05d3d29540ac9b7f4751d226c0b4f37e6c92cc7b1c285c7cf993e16585e3f33edae18d2170f6544ac30f810590d8ac3a0fd23f1df124fed1ec6
|
data/.gitignore
CHANGED
@@ -39,17 +39,25 @@ module ActiveRecord
|
|
39
39
|
|
40
40
|
def type_cast_from_database(value)
|
41
41
|
hash = super
|
42
|
-
|
42
|
+
if hash
|
43
|
+
accessor_types.each do |key, type|
|
44
|
+
hash[key] = type.type_cast_from_database(hash[key]) if hash.key?(key)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
hash
|
43
48
|
end
|
44
49
|
|
45
50
|
def type_cast_for_database(value)
|
46
|
-
if value
|
51
|
+
if value.is_a?(Hash)
|
52
|
+
typed_casted = {}
|
47
53
|
accessor_types.each do |key, type|
|
48
54
|
k = key_to_cast(value, key)
|
49
|
-
|
55
|
+
typed_casted[k] = type.type_cast_for_database(value[k]) unless k.nil?
|
50
56
|
end
|
57
|
+
super(value.merge(typed_casted))
|
58
|
+
else
|
59
|
+
super(value)
|
51
60
|
end
|
52
|
-
super(value)
|
53
61
|
end
|
54
62
|
|
55
63
|
def type_cast_from_user(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,13 @@ 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 :focus
|
35
|
+
config.run_all_when_everything_filtered = true
|
36
|
+
|
37
|
+
config.example_status_persistence_file_path = "tmp/rspec_examples.txt"
|
38
|
+
|
39
|
+
if config.files_to_run.one?
|
40
|
+
config.default_formatter = 'doc'
|
41
|
+
end
|
33
42
|
end
|
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.4.
|
4
|
+
version: 0.4.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-
|
11
|
+
date: 2016-07-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|