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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 56c80c30cd987d635b40bcd5863401d5ca45644078eaafbc4f0d4278e6ed29fb
4
- data.tar.gz: e17b443b615750a1b87b35a0bda3acb6a287b267dbb6e3f3f4b7677e4ec0e11e
3
+ metadata.gz: a99c1037fc868b8aa6af066eed972678b202a806095eb992ae618fa66a0ea97a
4
+ data.tar.gz: 9690b9e4805b6c5164a47dc61396f66557e1da13c59d8619193b727b4c283116
5
5
  SHA512:
6
- metadata.gz: 94605281aeead99bbdff837e4211f019bb4038754a3205b541adb5e76e58089ba1cbb202b22ec4c076dd4e842d2b12044cf28757fee7781b4fd4497851c35cee
7
- data.tar.gz: 99f12655273e0a70158541723d5314c2b73a76eb21da79ea5ebc58c8d4375646e1ad092570031a7e75932d994f75f9d95720bd48bc8d1321b2c808eab2fcb9d9
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. `StoreModel::Model` API:
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
 
@@ -39,5 +39,9 @@ module StoreModel
39
39
  def type_for_attribute(attribute)
40
40
  self.class.attribute_types[attribute.to_s]
41
41
  end
42
+
43
+ def unknown_attributes
44
+ @unknown_attributes ||= {}
45
+ end
42
46
  end
43
47
  end
@@ -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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module StoreModel
4
- VERSION = "0.4.1"
4
+ VERSION = "0.5.0"
5
5
  end
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.1
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-07-30 00:00:00.000000000 Z
11
+ date: 2019-08-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails