store_model 1.2.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/README.md +6 -8
- data/lib/store_model/model.rb +18 -1
- data/lib/store_model/types/one_polymorphic.rb +2 -9
- data/lib/store_model/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 04d0447a36db9c3cec8c6636e0480c188d5d1f70011485dc8aad3aef17979fe5
|
4
|
+
data.tar.gz: ab7835f6a2fa0e3d481a92c9dd94f7946f0da2d788f665faefe8196d141d57e4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 271ef39c6e35da560a31646b0731f25daa56005ae955c42c40236befbc98bfb3479e233f84cbabc4a40d299c65812bbc752b55e4e9350f65aaaea89cf5c8e0ac
|
7
|
+
data.tar.gz: a5da18d298f3c254b0f0f2d38075953077a601fc4589df632d6d31222338b12f808cfecc5138d1916a34bf6aa67da96bb71535bb74fa3989a31df44e013d9305
|
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# StoreModel [![Gem Version](https://badge.fury.io/rb/store_model.svg)](https://rubygems.org/gems/store_model) [![Coverage Status](https://coveralls.io/repos/github/DmitryTsepelev/store_model/badge.svg?branch=master)](https://coveralls.io/github/DmitryTsepelev/store_model?branch=master)
|
1
|
+
# StoreModel [![Gem Version](https://badge.fury.io/rb/store_model.svg)](https://rubygems.org/gems/store_model) [![Coverage Status](https://coveralls.io/repos/github/DmitryTsepelev/store_model/badge.svg?branch=master)](https://coveralls.io/github/DmitryTsepelev/store_model?branch=master) ![](https://ruby-gem-downloads-badge.herokuapp.com/store_model?type=total)
|
2
2
|
|
3
3
|
**StoreModel** gem allows you to wrap JSON-backed DB columns with ActiveModel-like classes.
|
4
4
|
|
@@ -22,12 +22,6 @@ class Product < ApplicationRecord
|
|
22
22
|
end
|
23
23
|
```
|
24
24
|
|
25
|
-
<p align="center">
|
26
|
-
<a href="https://evilmartians.com/?utm_source=store_model">
|
27
|
-
<img src="https://evilmartians.com/badges/sponsored-by-evil-martians.svg" alt="Sponsored by Evil Martians" width="236" height="54">
|
28
|
-
</a>
|
29
|
-
</p>
|
30
|
-
|
31
25
|
## Why should I wrap my JSON columns?
|
32
26
|
|
33
27
|
Imagine that you have a model `Product` with a `jsonb` column called `configuration`. This is how you likely gonna work with this column:
|
@@ -42,7 +36,7 @@ product.save
|
|
42
36
|
|
43
37
|
This approach works fine when you don't have a lot of keys with logic around them and just read the data. However, when you start working with that data more intensively–you may find the code a bit verbose and error-prone.
|
44
38
|
|
45
|
-
For instance, try to find a way to validate `:model` value to be required. Despite of the fact, that you'll have to write this validation by hand, it violates single-
|
39
|
+
For instance, try to find a way to validate `:model` value to be required. Despite of the fact, that you'll have to write this validation by hand, it violates the single-responsibility principle: why parent model (`Product`) should know about the logic related to a child (`Configuration`)?
|
46
40
|
|
47
41
|
> 📖 Read more about the motivation in the [Wrapping JSON-based ActiveRecord attributes with classes](https://evilmartians.com/chronicles/wrapping-json-based-active-record-attributes-with-classes) post
|
48
42
|
|
@@ -97,6 +91,10 @@ _Usage note: Rails and assigning Arrays/Hashes to records_
|
|
97
91
|
4. [Alternatives](./docs/alternatives.md)
|
98
92
|
5. [Defining custom types](./docs/defining_custom_types.md)
|
99
93
|
|
94
|
+
## Credits
|
95
|
+
|
96
|
+
Initially sponsored by [Evil Martians](http://evilmartians.com).
|
97
|
+
|
100
98
|
## License
|
101
99
|
|
102
100
|
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/lib/store_model/model.rb
CHANGED
@@ -23,7 +23,7 @@ module StoreModel
|
|
23
23
|
|
24
24
|
attr_accessor :parent
|
25
25
|
|
26
|
-
delegate :each_value,
|
26
|
+
delegate :each_value, to: :attributes
|
27
27
|
|
28
28
|
# Returns a hash representing the model. Some configuration can be
|
29
29
|
# passed through +options+.
|
@@ -43,6 +43,21 @@ module StoreModel
|
|
43
43
|
result.as_json(options)
|
44
44
|
end
|
45
45
|
|
46
|
+
# Returns an Object, similar to Hash#fetch, raises
|
47
|
+
# a KeyError if attr_name doesn't exist.
|
48
|
+
# @param attr_name [String, Symbol]
|
49
|
+
#
|
50
|
+
# @return Object
|
51
|
+
def fetch(attr_name)
|
52
|
+
stringified_key = attr_name.to_s
|
53
|
+
if attribute_names.include?(stringified_key) || attribute_aliases.key?(stringified_key)
|
54
|
+
public_send(stringified_key)
|
55
|
+
else
|
56
|
+
message = attr_name.is_a?(Symbol) ? "key not found: :#{attr_name}" : "key not found: #{attr_name}"
|
57
|
+
raise KeyError, message
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
46
61
|
# Compares two StoreModel::Model instances
|
47
62
|
#
|
48
63
|
# @param other [StoreModel::Model]
|
@@ -57,6 +72,8 @@ module StoreModel
|
|
57
72
|
|
58
73
|
# Accessing attribute using brackets
|
59
74
|
#
|
75
|
+
# @param attr_name [String, Symbol]
|
76
|
+
#
|
60
77
|
# @return [Object]
|
61
78
|
def [](attr_name)
|
62
79
|
@attributes.fetch_value(attr_name.to_s)
|
@@ -50,16 +50,9 @@ module StoreModel
|
|
50
50
|
#
|
51
51
|
# @return [String] serialized value
|
52
52
|
def serialize(value)
|
53
|
-
|
54
|
-
when Hash
|
55
|
-
ActiveSupport::JSON.encode(value, serialize_unknown_attributes: true)
|
56
|
-
else
|
57
|
-
if implements_model?(value.class)
|
58
|
-
return ActiveSupport::JSON.encode(value, serialize_unknown_attributes: true)
|
59
|
-
end
|
53
|
+
return super unless value.is_a?(Hash) || implements_model?(value.class)
|
60
54
|
|
61
|
-
|
62
|
-
end
|
55
|
+
ActiveSupport::JSON.encode(value, serialize_unknown_attributes: true)
|
63
56
|
end
|
64
57
|
|
65
58
|
protected
|
data/lib/store_model/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: store_model
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- DmitryTsepelev
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-01-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|