store_model 0.1.2 → 0.2.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 +12 -0
- data/lib/store_model/model.rb +6 -2
- data/lib/store_model/types.rb +10 -0
- data/lib/store_model/types/array_type.rb +55 -0
- data/lib/store_model/types/json_type.rb +51 -0
- data/lib/store_model/version.rb +1 -1
- metadata +5 -3
- data/lib/store_model/json_model_type.rb +0 -42
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9c3afb6c889480d5e06a1532206001a7080400b8617a9c87564bc1658656292c
|
4
|
+
data.tar.gz: bf50f80defd77d3774b5aae9658e3421b914e01b55ee9e547c61fedb715672f4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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:
|
data/lib/store_model/model.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require "store_model/
|
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
|
-
|
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,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
|
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.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-
|
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
|