store_model 0.1.2 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5f9b992038ac6e32409aeb104f8a6de62c848550a7b79160ce83f777547f0ea0
4
- data.tar.gz: 8a2f3e43dbd4dea884581a5ff1caf017eb03e71c17fee015dd83cb95244dc267
3
+ metadata.gz: 9c3afb6c889480d5e06a1532206001a7080400b8617a9c87564bc1658656292c
4
+ data.tar.gz: bf50f80defd77d3774b5aae9658e3421b914e01b55ee9e547c61fedb715672f4
5
5
  SHA512:
6
- metadata.gz: 3675e1f7c3b1aea633b3931bde2d4c51e3841633b5712c8b73c7227d07d49edbb7c9ec8e029c5a1b98cf5a2ac6afcc78a2f2a3e2eb39966d4a9f5d9ccbd71e83
7
- data.tar.gz: 619425140ebbf8b9cb4fa6d3171977d19aab43b4981647a7c6b4bc28c65e79b0026083459cdec9e6d3761839225e4079ad4496ac361ba0080247d472cf0fd215
6
+ metadata.gz: 0ae10274f4904b73e4763b8b313d9fb896b88ab060c1bc929168ce0f671fc316d40b1c1f0a10238130a91803d0ea397cbf93a35e59da242cc776d3935a438050
7
+ data.tar.gz: 92b840f816dd7799691b67171d7ab2426ad345ad9bd0b7f51468071d876c955c42a17588483ceffa1f8230bd4b1272672ccd6b05dd2a139c26a40dd9b7230ed1
data/README.md CHANGED
@@ -70,6 +70,18 @@ class Product < ApplicationRecord
70
70
  end
71
71
  ```
72
72
 
73
+ ## Handling arrays
74
+
75
+ Should you store an array of models, you can use `#to_array_type` method:
76
+
77
+ ```ruby
78
+ class Product < ApplicationRecord
79
+ attribute :configurations, Configuration.to_array_type
80
+ end
81
+ ```
82
+
83
+ After that, your attribute will return array of `Configuration` instances.
84
+
73
85
  ## Validations
74
86
 
75
87
  `StoreModel` supports all the validations shipped with `ActiveModel`. Start with defining validation for the store model:
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "store_model/json_model_type"
3
+ require "store_model/types"
4
4
 
5
5
  module StoreModel
6
6
  module Model
@@ -10,7 +10,11 @@ module StoreModel
10
10
 
11
11
  base.extend(Module.new do
12
12
  def to_type
13
- JsonModelType.new(self)
13
+ Types::JsonType.new(self)
14
+ end
15
+
16
+ def to_array_type
17
+ Types::ArrayType.new(self)
14
18
  end
15
19
  end)
16
20
  end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "store_model/types/json_type"
4
+ require "store_model/types/array_type"
5
+
6
+ module StoreModel
7
+ module Types
8
+ class CastError < StandardError; end
9
+ end
10
+ end
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_model"
4
+
5
+ module StoreModel
6
+ module Types
7
+ class ArrayType < ActiveModel::Type::Value
8
+ def initialize(model_klass)
9
+ @model_klass = model_klass
10
+ end
11
+
12
+ def type
13
+ :array
14
+ end
15
+
16
+ def cast_value(value)
17
+ case value
18
+ when String then decode_and_initialize(value)
19
+ when Array then ensure_model_class(value)
20
+ else
21
+ raise StoreModel::Types::CastError,
22
+ "failed casting #{value.inspect}, only String or Array instances are allowed"
23
+ end
24
+ end
25
+
26
+ def serialize(value)
27
+ case value
28
+ when Array
29
+ ActiveSupport::JSON.encode(value)
30
+ else
31
+ super
32
+ end
33
+ end
34
+
35
+ def changed_in_place?(raw_old_value, new_value)
36
+ cast_value(raw_old_value) != new_value
37
+ end
38
+
39
+ private
40
+
41
+ # rubocop:disable Style/RescueModifier
42
+ def decode_and_initialize(array_value)
43
+ decoded = ActiveSupport::JSON.decode(array_value) rescue []
44
+ decoded.map { |attributes| @model_klass.new(attributes) }
45
+ end
46
+ # rubocop:enable Style/RescueModifier
47
+
48
+ def ensure_model_class(array)
49
+ array.map do |object|
50
+ object.is_a?(@model_klass) ? object : @model_klass.new(object)
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_model"
4
+
5
+ module StoreModel
6
+ module Types
7
+ class JsonType < ActiveModel::Type::Value
8
+ def initialize(model_klass)
9
+ @model_klass = model_klass
10
+ end
11
+
12
+ def type
13
+ :json
14
+ end
15
+
16
+ def cast_value(value)
17
+ case value
18
+ when String then decode_and_initialize(value)
19
+ when Hash then @model_klass.new(value)
20
+ when @model_klass 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"
25
+ end
26
+ end
27
+
28
+ def serialize(value)
29
+ case value
30
+ when Hash, @model_klass
31
+ ActiveSupport::JSON.encode(value)
32
+ else
33
+ super
34
+ end
35
+ end
36
+
37
+ def changed_in_place?(raw_old_value, new_value)
38
+ cast_value(raw_old_value) != new_value
39
+ end
40
+
41
+ private
42
+
43
+ # rubocop:disable Style/RescueModifier
44
+ def decode_and_initialize(value)
45
+ decoded = ActiveSupport::JSON.decode(value) rescue nil
46
+ @model_klass.new(decoded) unless decoded.nil?
47
+ end
48
+ # rubocop:enable Style/RescueModifier
49
+ end
50
+ end
51
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module StoreModel
4
- VERSION = "0.1.2"
4
+ VERSION = "0.2.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.1.2
4
+ version: 0.2.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-03-14 00:00:00.000000000 Z
11
+ date: 2019-04-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -96,8 +96,10 @@ files:
96
96
  - lib/store_model/combine_errors_strategies/mark_invalid_error_strategy.rb
97
97
  - lib/store_model/combine_errors_strategies/merge_error_strategy.rb
98
98
  - lib/store_model/configuration.rb
99
- - lib/store_model/json_model_type.rb
100
99
  - lib/store_model/model.rb
100
+ - lib/store_model/types.rb
101
+ - lib/store_model/types/array_type.rb
102
+ - lib/store_model/types/json_type.rb
101
103
  - lib/store_model/version.rb
102
104
  homepage: https://github.com/DmitryTsepelev/store_model
103
105
  licenses:
@@ -1,42 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "active_model"
4
-
5
- module StoreModel
6
- class JsonModelType < ActiveModel::Type::Value
7
- def initialize(model_klass)
8
- @model_klass = model_klass
9
- end
10
-
11
- def type
12
- :json
13
- end
14
-
15
- # rubocop:disable Style/RescueModifier
16
- def cast_value(value)
17
- case value
18
- when String
19
- decoded = ActiveSupport::JSON.decode(value) rescue nil
20
- @model_klass.new(decoded) unless decoded.nil?
21
- when Hash
22
- @model_klass.new(value)
23
- when @model_klass
24
- value
25
- end
26
- end
27
- # rubocop:enable Style/RescueModifier
28
-
29
- def serialize(value)
30
- case value
31
- when Hash, @model_klass
32
- ActiveSupport::JSON.encode(value)
33
- else
34
- super
35
- end
36
- end
37
-
38
- def changed_in_place?(raw_old_value, new_value)
39
- cast_value(raw_old_value) != new_value
40
- end
41
- end
42
- end