attr_json 1.3.0 → 1.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/ci.yml +6 -0
- data/Appraisals +19 -9
- data/CHANGELOG.md +18 -1
- data/Gemfile +5 -5
- data/README.md +37 -27
- data/attr_json.gemspec +19 -2
- data/gemfiles/rails_5_0.gemfile +1 -1
- data/gemfiles/rails_5_1.gemfile +1 -1
- data/gemfiles/rails_5_2.gemfile +1 -1
- data/gemfiles/rails_6_0.gemfile +1 -1
- data/gemfiles/rails_6_1.gemfile +2 -2
- data/gemfiles/{rails_edge_6.gemfile → rails_7_0.gemfile} +3 -3
- data/gemfiles/rails_edge.gemfile +4 -4
- data/lib/attr_json/model.rb +17 -6
- data/lib/attr_json/type/model.rb +27 -3
- data/lib/attr_json/type/polymorphic_model.rb +5 -0
- data/lib/attr_json/version.rb +1 -1
- metadata +37 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b2f8a06eaf0058564901292a35bf15fd56c24b0eb8cb784ab1b1e9adc94b87e5
|
4
|
+
data.tar.gz: eb4917f0eb8304b8261536ae75a5abd64dfc29aed52d82c190bb9b48a738ff08
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 599a5e752e187a9e511b766efffda2bbf546c6940f42e58c2f0cb883ef835c017e4ec8b9a4fbc2a3236fde733ae6c77c8d27e4728b0fff53dc993b209a3d7882
|
7
|
+
data.tar.gz: 84dac19bcebedf7979ad3299b28ecc175fec59887cc9e6d761d708f0dbb240f8443aba52aca89a434ab8040f5ff64288ce4a428a659c61e8ff11d128399b5974
|
data/.github/workflows/ci.yml
CHANGED
data/Appraisals
CHANGED
@@ -35,20 +35,30 @@ end
|
|
35
35
|
appraise "rails-6-1" do
|
36
36
|
gem 'combustion', "~> 1.0"
|
37
37
|
|
38
|
-
gem "rails", "
|
38
|
+
gem "rails", "~> 6.1.0"
|
39
39
|
gem "pg", "~> 1.0"
|
40
40
|
end
|
41
41
|
|
42
|
-
appraise "rails-
|
43
|
-
# Edge rails needs unreleased combustion
|
44
|
-
# https://github.com/pat/combustion/issues/92
|
42
|
+
appraise "rails-7-0" do
|
45
43
|
gem 'combustion', "~> 1.0"
|
46
44
|
|
47
|
-
gem "rails",
|
45
|
+
gem "rails", "~> 7.0.0"
|
48
46
|
gem "pg", "~> 1.0"
|
49
47
|
|
50
|
-
#
|
51
|
-
#
|
52
|
-
#
|
53
|
-
gem
|
48
|
+
# to make our integration tests using simple_form work as-is,
|
49
|
+
# this works. Don't totally underestand the actual simple_form
|
50
|
+
# setup paths in Rails 7....
|
51
|
+
gem "sprockets-rails"
|
52
|
+
end
|
53
|
+
|
54
|
+
appraise "rails-edge" do
|
55
|
+
gem 'combustion', "~> 1.0"
|
56
|
+
|
57
|
+
gem "rails", git: "https://github.com/rails/rails.git", branch: "main"
|
58
|
+
gem "pg", "~> 1.0"
|
59
|
+
|
60
|
+
# We do some tests using cocoon, currently via sprockets-rails,
|
61
|
+
# which is not automatically available in Rails 7. Not sure
|
62
|
+
# the future of cocoon in general. https://github.com/nathanvda/cocoon/issues/555
|
63
|
+
gem "sprockets-rails"
|
54
64
|
end
|
data/CHANGELOG.md
CHANGED
@@ -4,7 +4,24 @@ Notable changes to this project will be documented in this file.
|
|
4
4
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
5
5
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
6
6
|
|
7
|
-
## [Unreleased](https://github.com/jrochkind/attr_json/compare/v1.
|
7
|
+
## [Unreleased](https://github.com/jrochkind/attr_json/compare/v1.4.0...HEAD)
|
8
|
+
|
9
|
+
|
10
|
+
|
11
|
+
|
12
|
+
## [1.4.0](https://github.com/jrochkind/attr_json/compare/v1.3.0...v1.4.0)
|
13
|
+
|
14
|
+
### Changed
|
15
|
+
|
16
|
+
* When using store_key feature on an AttrJson::Model, you should not be able to pass in the store_key as a key in initializer or assign_attributes. It was really a bug that this ended up mapped to attribute this way, which could cause a problem in some cases; but calling it out in Changed section because if you were accidentally relying on it, it may appear as a backwards incompat to you. https://github.com/jrochkind/attr_json/pull/125
|
17
|
+
|
18
|
+
* Rails 7.0.0 allowed by gemspec and tested in CI
|
19
|
+
|
20
|
+
### Fixed
|
21
|
+
|
22
|
+
* polymorphic single type can be set to nil https://github.com/jrochkind/attr_json/pull/115
|
23
|
+
* polymorphic models can be serialized from hash in container attribute. Thanks @machty. https://github.com/jrochkind/attr_json/pull/123
|
24
|
+
* fix bug with deserialization of nested attributes that have types that apply different serialization vs cast logic. Thanks @bradleesand. https://github.com/jrochkind/attr_json/pull/125
|
8
25
|
|
9
26
|
## [1.3.0](https://github.com/jrochkind/attr_json/compare/v1.2.0...v1.3.0)
|
10
27
|
|
data/Gemfile
CHANGED
@@ -18,17 +18,17 @@ gem 'combustion', '~> 1.1'
|
|
18
18
|
# be the way to do it.
|
19
19
|
gem 'rails'
|
20
20
|
|
21
|
-
# We should not really need to mention railties, it's already a dependency of
|
22
|
-
# rails, but seems to be necessary to get around some mystery bug in bundler
|
23
|
-
# dependency resolution.
|
24
|
-
gem 'railties'
|
25
|
-
|
26
21
|
gem "pg"
|
27
22
|
gem "rspec-rails", "~> 4.0"
|
28
23
|
gem "simple_form", ">= 4.0"
|
29
24
|
gem 'cocoon', ">= 1.2"
|
30
25
|
gem 'jquery-rails'
|
31
26
|
|
27
|
+
# Even though we don't use coffee-script, when running specs, some part of rails
|
28
|
+
# or other part of our stack is still insisting on requiring it, for reasons we
|
29
|
+
# don't understand, so we need to depend on it.
|
30
|
+
gem "coffee-rails"
|
31
|
+
|
32
32
|
gem 'capybara', "~> 3.0"
|
33
33
|
gem 'webdrivers', '~> 4.0'
|
34
34
|
gem "selenium-webdriver"
|
data/README.md
CHANGED
@@ -398,27 +398,29 @@ Rails 5.1+ on `attr_json`s on your ActiveRecord classes that include
|
|
398
398
|
`AttrJson::Record`, by including `AttrJson::Record::Dirty`.
|
399
399
|
Change-tracking methods are available off the `attr_json_changes` method.
|
400
400
|
|
401
|
-
|
402
|
-
|
403
|
-
|
404
|
-
|
405
|
-
attr_json :str, :string
|
406
|
-
end
|
407
|
-
|
408
|
-
model = MyModel.new
|
409
|
-
model.str = "old"
|
410
|
-
model.save
|
411
|
-
model.str = "new"
|
401
|
+
```ruby
|
402
|
+
class MyModel < ActiveRecord::Base
|
403
|
+
include AttrJson::Record
|
404
|
+
include AttrJson::Record::Dirty
|
412
405
|
|
413
|
-
|
414
|
-
|
406
|
+
attr_json :str, :string
|
407
|
+
end
|
415
408
|
|
416
|
-
|
417
|
-
|
418
|
-
|
419
|
-
|
420
|
-
|
421
|
-
|
409
|
+
model = MyModel.new
|
410
|
+
model.str = "old"
|
411
|
+
model.save
|
412
|
+
model.str = "new"
|
413
|
+
|
414
|
+
# All and only "new" style dirty tracking methods (Raisl 5.1+)
|
415
|
+
# are available:
|
416
|
+
|
417
|
+
model.attr_json_changes.saved_changes
|
418
|
+
model.attr_json_changes.changes_to_save
|
419
|
+
model.attr_json_changes.saved_change_to_str?
|
420
|
+
model.attr_json_changes.saved_change_to_str
|
421
|
+
model.attr_json_changes.will_save_change_to_str?
|
422
|
+
# etc
|
423
|
+
```
|
422
424
|
|
423
425
|
More options are available, including merging changes from 'ordinary'
|
424
426
|
ActiveRecord attributes in. See docs on [Dirty Tracking](./doc_src/dirty_tracking.md)
|
@@ -476,11 +478,15 @@ to prevent overwriting other updates from processes.
|
|
476
478
|
|
477
479
|
## State of Code, and To Be Done
|
478
480
|
|
479
|
-
|
481
|
+
This code is solid and stable and is being used in production by at least a handful of people, including the primary maintainer, jrochkind.
|
482
|
+
|
483
|
+
The project is currently getting very little maintainance -- and is still working reliably through Rails releases. It is tested on edge rails and ruby (and has needed very few if any changes with subsequent releases), and I endeavor to keep it working as Rails keeps releasing.
|
484
|
+
|
485
|
+
In order to keep the low-maintenace scenario sustainable, I am *very* cautious accepting new features, especially if they increase code complexity at all. Even if you have a working PR, I may be reluctant to accept it. I'm prioritizing sustainability and stability over new features, and so far this is working out well. However, discussion is always welcome! Especially when paired with code (failing tests for the bugfix or feature you want are super helpful on their own!).
|
480
486
|
|
481
487
|
We are committed to [semantic versioning](https://semver.org/) and will endeavor to release no backwards breaking changes without a major version. We are also serious about minimizing backwards incompat releases altogether (ie minimiing major version releases).
|
482
488
|
|
483
|
-
Feedback of any kind of _very welcome_, please feel free to use the issue tracker.
|
489
|
+
Feedback of any kind of _very welcome_, please feel free to use the issue tracker. It is hard to get a sense of how many people are actually using this, which is helpful both for my own sense of reward and for anyone to get a sense of the size of the userbase -- feel free to say hi and let us know how you are using it!
|
484
490
|
|
485
491
|
Except for the jsonb_contains stuff using postgres jsonb contains operator, I don't believe any postgres-specific features are used. It ought to work with MySQL, testing and feedback welcome. (Or a PR to test on MySQL?). My own interest is postgres.
|
486
492
|
|
@@ -488,26 +494,30 @@ This is still mostly a single-maintainer operation, so has all the sustainabilit
|
|
488
494
|
|
489
495
|
### Possible future features:
|
490
496
|
|
491
|
-
*
|
497
|
+
* Make AttrJson::Model lean more heavily on ActiveModel::Attributes API that did not fully exist in first version of attr_json [hope for a future attr_json 2.0 release]
|
498
|
+
|
499
|
+
* Make AttrJson::Record insist on creating rails cover attributes (no longer optional) and integrating more fully into rails, including rails dirty tricking, eliminating need for custom dirty tracking implementation. Overall decrease in lines of code. Can use jsonb_accessor as guide for some aspects. [hope for inclusion in future attr_json 2.0 release]
|
500
|
+
|
501
|
+
* partial updates for json hashes would be really nice: Using postgres jsonb merge operators to only overwrite what changed. In my initial attempts, AR doesn't make it easy to customize this. [update: this is hard, probably not coming soon]
|
492
502
|
|
493
|
-
* seamless compatibility with ransack
|
503
|
+
* seamless compatibility with ransack [update: not necessarily prioritized]
|
494
504
|
|
495
505
|
* Should we give AttrJson::Model a before_serialize hook that you might
|
496
506
|
want to use similar to AR before_save? Should AttrJson::Models
|
497
|
-
raise on trying to serialize an invalid model?
|
507
|
+
raise on trying to serialize an invalid model? [update: eh, hasn't really come up]
|
498
508
|
|
499
509
|
* There are limits to what you can do with just jsonb_contains
|
500
510
|
queries. We could support operations like `>`, `<`, `<>`
|
501
511
|
as [jsonb_accessor](https://github.com/devmynd/jsonb_accessor),
|
502
512
|
even accross keypaths. (At present, you could use a
|
503
513
|
before_savee to denormalize/renormalize copy your data into
|
504
|
-
ordinary AR columns/associations for searching. Or perhaps a postgres ts_vector for text searching. Needs to be worked out.)
|
514
|
+
ordinary AR columns/associations for searching. Or perhaps a postgres ts_vector for text searching. Needs to be worked out.) [update: interested, but not necessarily prioritized. This one would be interesting for a third-party PR draft!]
|
505
515
|
|
506
516
|
* We could/should probably support `jsonb_order` clauses, even
|
507
|
-
accross key paths, like jsonb_accessor.
|
517
|
+
accross key paths, like jsonb_accessor. [update: interested but not necessarily prioritized]
|
508
518
|
|
509
519
|
* Could we make these attributes work in ordinary AR where, same
|
510
|
-
as they do in jsonb_contains? Maybe.
|
520
|
+
as they do in jsonb_contains? Maybe. [update: probably not]
|
511
521
|
|
512
522
|
## Development
|
513
523
|
|
data/attr_json.gemspec
CHANGED
@@ -44,8 +44,8 @@ attributes use as much of the existing ActiveRecord architecture as we can.}
|
|
44
44
|
|
45
45
|
# Only to get CI to work on versions of Rails other than we release with,
|
46
46
|
# should never release a gem with RAILS_GEM set!
|
47
|
-
unless ENV['APPRAISAL_INITIALIZED'] || ENV["TRAVIS"]
|
48
|
-
spec.add_runtime_dependency "activerecord", ">= 5.0.0", "<
|
47
|
+
unless ENV['APPRAISAL_INITIALIZED'] || ENV["TRAVIS"] || ENV['CI']
|
48
|
+
spec.add_runtime_dependency "activerecord", ">= 5.0.0", "< 7.1"
|
49
49
|
end
|
50
50
|
|
51
51
|
spec.add_development_dependency "bundler"
|
@@ -54,4 +54,21 @@ attributes use as much of the existing ActiveRecord architecture as we can.}
|
|
54
54
|
spec.add_development_dependency "database_cleaner", "~> 1.5"
|
55
55
|
spec.add_development_dependency "yard-activesupport-concern"
|
56
56
|
spec.add_development_dependency "appraisal", "~> 2.2"
|
57
|
+
|
58
|
+
# Working around annoying issue in selenium 3.x with ruby 3.0.
|
59
|
+
# we don't actually use rexml ourselves. selenium 3 is a dependency
|
60
|
+
# of webdrivers, and tries to use rexml without depending on it
|
61
|
+
# as is needed in ruby 3.
|
62
|
+
#
|
63
|
+
# https://github.com/SeleniumHQ/selenium/issues/9001
|
64
|
+
#
|
65
|
+
# if in the future you can remove this dependecy and still have tests pass
|
66
|
+
# under ruby 3.x, you're good.
|
67
|
+
spec.add_development_dependency "rexml"
|
68
|
+
|
69
|
+
# Used only for Capybara.server in our spec_helper.rb.
|
70
|
+
# webrick is no longer included in ruby 3.0, so has to
|
71
|
+
# be expressed as a dependecy, unless we switch
|
72
|
+
# capybara to use alternate webserver.
|
73
|
+
spec.add_development_dependency "webrick", "~> 1.0"
|
57
74
|
end
|
data/gemfiles/rails_5_0.gemfile
CHANGED
@@ -4,12 +4,12 @@ source "https://rubygems.org"
|
|
4
4
|
|
5
5
|
gem "combustion", "~> 0.9.0"
|
6
6
|
gem "rails", "~> 5.0.0"
|
7
|
-
gem "railties"
|
8
7
|
gem "pg", "~> 0.18"
|
9
8
|
gem "rspec-rails", "~> 4.0"
|
10
9
|
gem "simple_form", ">= 4.0"
|
11
10
|
gem "cocoon", ">= 1.2"
|
12
11
|
gem "jquery-rails"
|
12
|
+
gem "coffee-rails"
|
13
13
|
gem "capybara", "~> 3.0"
|
14
14
|
gem "webdrivers", "~> 4.0"
|
15
15
|
gem "selenium-webdriver"
|
data/gemfiles/rails_5_1.gemfile
CHANGED
@@ -4,12 +4,12 @@ source "https://rubygems.org"
|
|
4
4
|
|
5
5
|
gem "combustion", "~> 0.9.0"
|
6
6
|
gem "rails", "~> 5.1.0"
|
7
|
-
gem "railties"
|
8
7
|
gem "pg", "~> 1.0"
|
9
8
|
gem "rspec-rails", "~> 4.0"
|
10
9
|
gem "simple_form", ">= 4.0"
|
11
10
|
gem "cocoon", ">= 1.2"
|
12
11
|
gem "jquery-rails"
|
12
|
+
gem "coffee-rails"
|
13
13
|
gem "capybara", "~> 3.0"
|
14
14
|
gem "webdrivers", "~> 4.0"
|
15
15
|
gem "selenium-webdriver"
|
data/gemfiles/rails_5_2.gemfile
CHANGED
@@ -4,12 +4,12 @@ source "https://rubygems.org"
|
|
4
4
|
|
5
5
|
gem "combustion", "~> 0.9.0"
|
6
6
|
gem "rails", "~> 5.2.0"
|
7
|
-
gem "railties"
|
8
7
|
gem "pg", "~> 1.0"
|
9
8
|
gem "rspec-rails", "~> 4.0"
|
10
9
|
gem "simple_form", ">= 4.0"
|
11
10
|
gem "cocoon", ">= 1.2"
|
12
11
|
gem "jquery-rails"
|
12
|
+
gem "coffee-rails"
|
13
13
|
gem "capybara", "~> 3.0"
|
14
14
|
gem "webdrivers", "~> 4.0"
|
15
15
|
gem "selenium-webdriver"
|
data/gemfiles/rails_6_0.gemfile
CHANGED
@@ -4,12 +4,12 @@ source "https://rubygems.org"
|
|
4
4
|
|
5
5
|
gem "combustion", "~> 1.0"
|
6
6
|
gem "rails", ">= 6.0.0", "< 6.1"
|
7
|
-
gem "railties"
|
8
7
|
gem "pg", "~> 1.0"
|
9
8
|
gem "rspec-rails", "~> 4.0"
|
10
9
|
gem "simple_form", ">= 4.0"
|
11
10
|
gem "cocoon", ">= 1.2"
|
12
11
|
gem "jquery-rails"
|
12
|
+
gem "coffee-rails"
|
13
13
|
gem "capybara", "~> 3.0"
|
14
14
|
gem "webdrivers", "~> 4.0"
|
15
15
|
gem "selenium-webdriver"
|
data/gemfiles/rails_6_1.gemfile
CHANGED
@@ -3,13 +3,13 @@
|
|
3
3
|
source "https://rubygems.org"
|
4
4
|
|
5
5
|
gem "combustion", "~> 1.0"
|
6
|
-
gem "rails", "~> 6.1"
|
7
|
-
gem "railties"
|
6
|
+
gem "rails", "~> 6.1.0"
|
8
7
|
gem "pg", "~> 1.0"
|
9
8
|
gem "rspec-rails", "~> 4.0"
|
10
9
|
gem "simple_form", ">= 4.0"
|
11
10
|
gem "cocoon", ">= 1.2"
|
12
11
|
gem "jquery-rails"
|
12
|
+
gem "coffee-rails"
|
13
13
|
gem "capybara", "~> 3.0"
|
14
14
|
gem "webdrivers", "~> 4.0"
|
15
15
|
gem "selenium-webdriver"
|
@@ -3,17 +3,17 @@
|
|
3
3
|
source "https://rubygems.org"
|
4
4
|
|
5
5
|
gem "combustion", "~> 1.0"
|
6
|
-
gem "rails",
|
7
|
-
gem "railties"
|
6
|
+
gem "rails", "~> 7.0.0"
|
8
7
|
gem "pg", "~> 1.0"
|
9
8
|
gem "rspec-rails", "~> 4.0"
|
10
9
|
gem "simple_form", ">= 4.0"
|
11
10
|
gem "cocoon", ">= 1.2"
|
12
11
|
gem "jquery-rails"
|
12
|
+
gem "coffee-rails"
|
13
13
|
gem "capybara", "~> 3.0"
|
14
14
|
gem "webdrivers", "~> 4.0"
|
15
15
|
gem "selenium-webdriver"
|
16
16
|
gem "byebug"
|
17
|
-
gem "
|
17
|
+
gem "sprockets-rails"
|
18
18
|
|
19
19
|
gemspec path: "../"
|
data/gemfiles/rails_edge.gemfile
CHANGED
@@ -3,17 +3,17 @@
|
|
3
3
|
source "https://rubygems.org"
|
4
4
|
|
5
5
|
gem "combustion", "~> 1.0"
|
6
|
-
gem "rails", git: "https://github.com/rails/rails.git", branch: "
|
7
|
-
gem "railties"
|
6
|
+
gem "rails", git: "https://github.com/rails/rails.git", branch: "main"
|
8
7
|
gem "pg", "~> 1.0"
|
9
|
-
gem "rspec-rails", "~>
|
8
|
+
gem "rspec-rails", "~> 4.0"
|
10
9
|
gem "simple_form", ">= 4.0"
|
11
10
|
gem "cocoon", ">= 1.2"
|
12
11
|
gem "jquery-rails"
|
12
|
+
gem "coffee-rails"
|
13
13
|
gem "capybara", "~> 3.0"
|
14
14
|
gem "webdrivers", "~> 4.0"
|
15
15
|
gem "selenium-webdriver"
|
16
16
|
gem "byebug"
|
17
|
-
gem "
|
17
|
+
gem "sprockets-rails"
|
18
18
|
|
19
19
|
gemspec path: "../"
|
data/lib/attr_json/model.rb
CHANGED
@@ -103,16 +103,27 @@ module AttrJson
|
|
103
103
|
end
|
104
104
|
|
105
105
|
|
106
|
-
#
|
106
|
+
# The inverse of model#serializable_hash -- re-hydrates a serialized hash to a model.
|
107
|
+
#
|
108
|
+
# Similar to `.new`, but translates things that need to be translated in deserialization,
|
109
|
+
# like store_keys, and properly calling deserialize on the underlying types.
|
110
|
+
#
|
111
|
+
# @example Model.new_from_serializable(hash)
|
107
112
|
def new_from_serializable(attributes = {})
|
108
|
-
attributes = attributes.
|
113
|
+
attributes = attributes.collect do |key, value|
|
109
114
|
# store keys in arguments get translated to attribute names on initialize.
|
110
115
|
if attribute_def = self.attr_json_registry.store_key_lookup("", key.to_s)
|
111
|
-
attribute_def.name.to_s
|
112
|
-
else
|
113
|
-
key
|
116
|
+
key = attribute_def.name.to_s
|
114
117
|
end
|
115
|
-
|
118
|
+
|
119
|
+
attr_type = self.attr_json_registry.has_attribute?(key) && self.attr_json_registry.type_for_attribute(key)
|
120
|
+
if attr_type
|
121
|
+
value = attr_type.deserialize(value)
|
122
|
+
end
|
123
|
+
|
124
|
+
[key, value]
|
125
|
+
end.to_h
|
126
|
+
|
116
127
|
self.new(attributes)
|
117
128
|
end
|
118
129
|
|
data/lib/attr_json/type/model.rb
CHANGED
@@ -32,10 +32,10 @@ module AttrJson
|
|
32
32
|
# to_hash is actually the 'implicit' conversion, it really is a hash
|
33
33
|
# even though it isn't is_a?(Hash), try to_hash first before to_h,
|
34
34
|
# the explicit conversion.
|
35
|
-
model.
|
35
|
+
model.new(v.to_hash)
|
36
36
|
elsif v.respond_to?(:to_h)
|
37
37
|
# TODO Maybe we ought not to do this on #to_h?
|
38
|
-
model.
|
38
|
+
model.new(v.to_h)
|
39
39
|
elsif model.attr_json_config.bad_cast == :as_nil
|
40
40
|
# This was originally default behavior, to be like existing ActiveRecord
|
41
41
|
# which kind of silently does this for non-castable basic values. That
|
@@ -58,7 +58,31 @@ module AttrJson
|
|
58
58
|
end
|
59
59
|
|
60
60
|
def deserialize(v)
|
61
|
-
|
61
|
+
if v.nil?
|
62
|
+
# important to stay nil instead of empty object, because they
|
63
|
+
# are different things.
|
64
|
+
v
|
65
|
+
elsif v.kind_of? model
|
66
|
+
v
|
67
|
+
elsif v.respond_to?(:to_hash)
|
68
|
+
# to_hash is actually the 'implicit' conversion, it really is a hash
|
69
|
+
# even though it isn't is_a?(Hash), try to_hash first before to_h,
|
70
|
+
# the explicit conversion.
|
71
|
+
model.new_from_serializable(v.to_hash)
|
72
|
+
elsif v.respond_to?(:to_h)
|
73
|
+
# TODO Maybe we ought not to do this on #to_h? especially here in deserialize?
|
74
|
+
model.new_from_serializable(v.to_h)
|
75
|
+
elsif model.attr_json_config.bad_cast == :as_nil
|
76
|
+
# TODO should we have different config value for bad_deserialize vs bad_cast?
|
77
|
+
|
78
|
+
# This was originally default behavior, to be like existing ActiveRecord
|
79
|
+
# which kind of silently does this for non-castable basic values. That
|
80
|
+
# ended up being confusing in the basic case, so now we raise by default,
|
81
|
+
# but this is still configurable.
|
82
|
+
nil
|
83
|
+
else
|
84
|
+
raise BadCast.new("Can not cast from #{v.inspect} to #{self.type}")
|
85
|
+
end
|
62
86
|
end
|
63
87
|
|
64
88
|
# these guys are definitely mutable, so we need this.
|
@@ -114,6 +114,11 @@ module AttrJson
|
|
114
114
|
end
|
115
115
|
|
116
116
|
def serialize(v)
|
117
|
+
return nil if v.nil?
|
118
|
+
|
119
|
+
# if it's not already a model cast it to a model if possible (eg it's a hash)
|
120
|
+
v = cast(v)
|
121
|
+
|
117
122
|
model_name = v.class.name
|
118
123
|
type = type_for_model_name(model_name)
|
119
124
|
|
data/lib/attr_json/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: attr_json
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jonathan Rochkind
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-12-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -19,7 +19,7 @@ dependencies:
|
|
19
19
|
version: 5.0.0
|
20
20
|
- - "<"
|
21
21
|
- !ruby/object:Gem::Version
|
22
|
-
version: '
|
22
|
+
version: '7.1'
|
23
23
|
type: :runtime
|
24
24
|
prerelease: false
|
25
25
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -29,7 +29,7 @@ dependencies:
|
|
29
29
|
version: 5.0.0
|
30
30
|
- - "<"
|
31
31
|
- !ruby/object:Gem::Version
|
32
|
-
version: '
|
32
|
+
version: '7.1'
|
33
33
|
- !ruby/object:Gem::Dependency
|
34
34
|
name: bundler
|
35
35
|
requirement: !ruby/object:Gem::Requirement
|
@@ -114,6 +114,34 @@ dependencies:
|
|
114
114
|
- - "~>"
|
115
115
|
- !ruby/object:Gem::Version
|
116
116
|
version: '2.2'
|
117
|
+
- !ruby/object:Gem::Dependency
|
118
|
+
name: rexml
|
119
|
+
requirement: !ruby/object:Gem::Requirement
|
120
|
+
requirements:
|
121
|
+
- - ">="
|
122
|
+
- !ruby/object:Gem::Version
|
123
|
+
version: '0'
|
124
|
+
type: :development
|
125
|
+
prerelease: false
|
126
|
+
version_requirements: !ruby/object:Gem::Requirement
|
127
|
+
requirements:
|
128
|
+
- - ">="
|
129
|
+
- !ruby/object:Gem::Version
|
130
|
+
version: '0'
|
131
|
+
- !ruby/object:Gem::Dependency
|
132
|
+
name: webrick
|
133
|
+
requirement: !ruby/object:Gem::Requirement
|
134
|
+
requirements:
|
135
|
+
- - "~>"
|
136
|
+
- !ruby/object:Gem::Version
|
137
|
+
version: '1.0'
|
138
|
+
type: :development
|
139
|
+
prerelease: false
|
140
|
+
version_requirements: !ruby/object:Gem::Requirement
|
141
|
+
requirements:
|
142
|
+
- - "~>"
|
143
|
+
- !ruby/object:Gem::Version
|
144
|
+
version: '1.0'
|
117
145
|
description: |-
|
118
146
|
ActiveRecord attributes stored serialized in a json column, super smooth.
|
119
147
|
For Rails 5.0, 5.1, or 5.2. Typed and cast like Active Record. Supporting nested models,
|
@@ -153,8 +181,8 @@ files:
|
|
153
181
|
- gemfiles/rails_5_2.gemfile
|
154
182
|
- gemfiles/rails_6_0.gemfile
|
155
183
|
- gemfiles/rails_6_1.gemfile
|
184
|
+
- gemfiles/rails_7_0.gemfile
|
156
185
|
- gemfiles/rails_edge.gemfile
|
157
|
-
- gemfiles/rails_edge_6.gemfile
|
158
186
|
- lib/attr_json.rb
|
159
187
|
- lib/attr_json/attribute_definition.rb
|
160
188
|
- lib/attr_json/attribute_definition/registry.rb
|
@@ -182,7 +210,7 @@ licenses:
|
|
182
210
|
metadata:
|
183
211
|
homepage_uri: https://github.com/jrochkind/attr_json
|
184
212
|
source_code_uri: https://github.com/jrochkind/attr_json
|
185
|
-
post_install_message:
|
213
|
+
post_install_message:
|
186
214
|
rdoc_options: []
|
187
215
|
require_paths:
|
188
216
|
- lib
|
@@ -197,8 +225,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
197
225
|
- !ruby/object:Gem::Version
|
198
226
|
version: '0'
|
199
227
|
requirements: []
|
200
|
-
rubygems_version: 3.
|
201
|
-
signing_key:
|
228
|
+
rubygems_version: 3.1.6
|
229
|
+
signing_key:
|
202
230
|
specification_version: 4
|
203
231
|
summary: ActiveRecord attributes stored serialized in a json column, super smooth.
|
204
232
|
test_files: []
|