store_model 0.4.1 → 0.5.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 +2 -1
- data/lib/store_model/model.rb +4 -0
- data/lib/store_model/types/json_type.rb +20 -4
- 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: a99c1037fc868b8aa6af066eed972678b202a806095eb992ae618fa66a0ea97a
|
4
|
+
data.tar.gz: 9690b9e4805b6c5164a47dc61396f66557e1da13c59d8619193b727b4c283116
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3d1dbc66827d62afb4e1e3d527f0a1e7f928485b488483d16f91817fb472a3a47fb8ff907d841a542660996b9c2778e2a824cd4b5362c720ea65ff9aac36470e
|
7
|
+
data.tar.gz: 043b85616f5350e02005bc771687a0d893a949329963f63f67c4a59d7ef26a897a596539490aed6d16a3b56ea8741559226c8dc6d7dfba4d57492c48b886fc15
|
data/README.md
CHANGED
@@ -82,10 +82,11 @@ product.save
|
|
82
82
|
## Documentation
|
83
83
|
|
84
84
|
1. [Installation](./docs/installation.md)
|
85
|
-
2.
|
85
|
+
2. StoreModel::Model API:
|
86
86
|
* [Validations](./docs/validations.md)
|
87
87
|
* [Enums](./docs/enums.md)
|
88
88
|
* [Nested models](./docs/nested_models.md)
|
89
|
+
* [Unknown attributes](./docs/unknown_attributes.md)
|
89
90
|
3. [Array of stored models](./docs/array_of_stored_models.md)
|
90
91
|
4. [Alternatives](./docs/alternatives.md)
|
91
92
|
|
data/lib/store_model/model.rb
CHANGED
@@ -18,11 +18,10 @@ module StoreModel
|
|
18
18
|
when String then decode_and_initialize(value)
|
19
19
|
when Hash then @model_klass.new(value)
|
20
20
|
when @model_klass, nil then value
|
21
|
-
else
|
22
|
-
raise StoreModel::Types::CastError,
|
23
|
-
"failed casting #{value.inspect}, only String, " \
|
24
|
-
"Hash or #{@model_klass.name} instances are allowed"
|
21
|
+
else raise_cast_error(value)
|
25
22
|
end
|
23
|
+
rescue ActiveModel::UnknownAttributeError => e
|
24
|
+
handle_unknown_attribute(value, e)
|
26
25
|
end
|
27
26
|
|
28
27
|
def serialize(value)
|
@@ -44,8 +43,25 @@ module StoreModel
|
|
44
43
|
def decode_and_initialize(value)
|
45
44
|
decoded = ActiveSupport::JSON.decode(value) rescue nil
|
46
45
|
@model_klass.new(decoded) unless decoded.nil?
|
46
|
+
rescue ActiveModel::UnknownAttributeError => e
|
47
|
+
handle_unknown_attribute(decoded, e)
|
47
48
|
end
|
48
49
|
# rubocop:enable Style/RescueModifier
|
50
|
+
|
51
|
+
def raise_cast_error(value)
|
52
|
+
raise StoreModel::Types::CastError,
|
53
|
+
"failed casting #{value.inspect}, only String, " \
|
54
|
+
"Hash or #{@model_klass.name} instances are allowed"
|
55
|
+
end
|
56
|
+
|
57
|
+
def handle_unknown_attribute(value, exception)
|
58
|
+
attribute = exception.attribute.to_sym
|
59
|
+
value_symbolized = value.symbolize_keys
|
60
|
+
|
61
|
+
cast_value(value_symbolized.except(attribute)).tap do |configuration|
|
62
|
+
configuration.unknown_attributes[attribute.to_s] = value_symbolized[attribute]
|
63
|
+
end
|
64
|
+
end
|
49
65
|
end
|
50
66
|
end
|
51
67
|
end
|
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: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- DmitryTsepelev
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-08-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|