store_attribute 0.5.0 → 0.5.1

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: 2fe7ad4819867227ad87556c1656e53d18405c15
4
- data.tar.gz: bd6ccc0ef54fe3ea63a5deddae91712b34b9e937
3
+ metadata.gz: cd2643cf4b1909497a0de3f0d15ed6ca51ba599e
4
+ data.tar.gz: 607171026ea9b9638e20a920b732bd7258db246a
5
5
  SHA512:
6
- metadata.gz: 09cfe9e9d985bd9d510ca0a5a7d0e5b1be2c02cae61bb99e6954c080c28fccf44d0d82a001e892ee71fc7908e0ce45792831fbbd05a7d397c1f154b6bea90c6b
7
- data.tar.gz: 144699139f1f6dbc55c8bd86c4b7cb3335cbcc0d37991cb006a4ac5802bceefdd2b26aa8de7b73f42df00a5280eeada3e50096c1f0bb8cb6519de8916493ebe0
6
+ metadata.gz: 79e570bd455ecf0f2f7c500725e4a243f4e0d5133c5bdfd45100e10963bbe16623c37396d4a87b7018c74472813efbcfd212cc9d7ebe83c697ca00ad053d338c
7
+ data.tar.gz: 0c4458f32f0a658d276218894912e3c1c53833dd0cb4e506885bcff00a824beeadda620c89a4d6c09376edd1a262b9396ad317eb275c981d4d08194eb48968da
data/.gitignore CHANGED
@@ -33,4 +33,5 @@ spec/dummy/log/*.log
33
33
  spec/dummy/tmp/
34
34
  spec/dummy/.sass-cache
35
35
  Gemfile.local
36
- Gemfile.lock
36
+ Gemfile.lock
37
+ tmp/
@@ -18,23 +18,31 @@ module ActiveRecord
18
18
  end
19
19
 
20
20
  def add_typed_key(key, type, **options)
21
- type = ActiveModel::Type.lookup(type, options) if type.is_a?(Symbol)
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
- cast(hash)
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
- value[k] = type.serialize(value[k]) unless k.nil?
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)
@@ -1,3 +1,3 @@
1
1
  module StoreAttribute # :nodoc:
2
- VERSION = "0.5.0".freeze
2
+ VERSION = "0.5.1".freeze
3
3
  end
@@ -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
@@ -9,4 +9,4 @@ class MoneyType < ActiveRecord::Type::Integer
9
9
  end
10
10
  end
11
11
 
12
- ActiveModel::Type.register(:money_type, MoneyType)
12
+ ActiveRecord::Type.register(:money_type, MoneyType)
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.0
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-02 00:00:00.000000000 Z
11
+ date: 2016-07-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord