store_model 0.6.2 → 0.9.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: a4da91e0f6f9e27a71947f5a31b7bb0d936680bd263ad46f69916d995380af27
4
- data.tar.gz: 463730c176403dd845382cd078d90e58faae6d2ee1a7c9670c7f7c48d68032d3
3
+ metadata.gz: e85a27e1f3a20217b3f6134d8f2679d556ce445c74b47ff223a3abec247219b9
4
+ data.tar.gz: e4c5baceadbbd1049ff8c0616e31d41302178ed74929ee512e1625a8d983c870
5
5
  SHA512:
6
- metadata.gz: 1cb34bbaf868eea40c26d33b2c15a8fbd8c79f3e5429714172f0f38ea9e11a396241831744be82c280b8ace4de60237a5226d8a9cf77bedf2b200b8bb3e58485
7
- data.tar.gz: 8dbf31fb3f4ece28979fa49d799b1f633e7e9f5007ceb38d0cf883ea214c50dc621c8f5b8c5367cb979910c74af039de675acbf7d69e7720d2f6d61b25a240e4
6
+ metadata.gz: 8e900e6ffeaffb83c1bc87b66b810d5e652871908c46542f1d3edbd7791761721d8a69bef57b9e187ce700c480dbd7c59d0dc9cda6a01092d64fcba27c4eb277
7
+ data.tar.gz: 8744a4125da41b45bb0bb510296924758b5b07345e173cf4193a5ca0b3759f62aa2dfc54b4d9233d757d7c92e8ce0a0fed2c139f95c51ab91935bfc8e440001e
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) [![Build Status](https://travis-ci.org/DmitryTsepelev/store_model.svg?branch=master)](https://travis-ci.org/DmitryTsepelev/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)
2
2
 
3
3
  **StoreModel** gem allows you to wrap JSON-backed DB columns with ActiveModel-like classes.
4
4
 
@@ -44,7 +44,7 @@ This approach works fine when you don't have a lot of keys with logic around the
44
44
 
45
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-repsponsibility principle: why parent model (`Product`) should know about the logic related to a child (`Configuration`)?
46
46
 
47
- > 📖 Read more about the motivation in the [Wrapping JSON-based ActiveRecord attributes with classes](https://dev.to/evilmartians/wrapping-json-based-activerecord-attributes-with-classes-4apf) post
47
+ > 📖 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
48
 
49
49
  ## Getting started
50
50
 
@@ -88,6 +88,7 @@ product.save
88
88
  * [Nested models](./docs/nested_models.md)
89
89
  * [Unknown attributes](./docs/unknown_attributes.md)
90
90
  3. [Array of stored models](./docs/array_of_stored_models.md)
91
+ 4. [One of](./docs/one_of.md)
91
92
  4. [Alternatives](./docs/alternatives.md)
92
93
  5. [Defining custom types](./docs/defining_custom_types.md)
93
94
 
@@ -21,17 +21,29 @@ module ActiveModel
21
21
  end
22
22
 
23
23
  case record.type_for_attribute(attribute).type
24
- when :json
25
- strategy.call(attribute, record.errors, value.errors) if value.invalid?
26
- when :array
27
- record.errors.add(attribute, :invalid) if value.select(&:invalid?).present?
24
+ when :json, :polymorphic
25
+ call_json_strategy(attribute, record.errors, value)
26
+ when :array, :polymorphic_array
27
+ call_array_strategy(attribute, record.errors, value)
28
28
  end
29
29
  end
30
30
 
31
31
  private
32
32
 
33
+ def call_json_strategy(attribute, record_errors, value)
34
+ strategy.call(attribute, record_errors, value.errors) if value.invalid?
35
+ end
36
+
37
+ def call_array_strategy(attribute, record_errors, value)
38
+ array_strategy.call(attribute, record_errors, value) if value.select(&:invalid?).present?
39
+ end
40
+
33
41
  def strategy
34
- StoreModel::CombineErrorsStrategies.configure(options)
42
+ @strategy ||= StoreModel::CombineErrorsStrategies.configure(options)
43
+ end
44
+
45
+ def array_strategy
46
+ @array_strategy ||= StoreModel::CombineErrorsStrategies.configure_array(options)
35
47
  end
36
48
  end
37
49
  end
data/lib/store_model.rb CHANGED
@@ -10,5 +10,10 @@ module StoreModel # :nodoc:
10
10
  def config
11
11
  @config ||= Configuration.new
12
12
  end
13
+
14
+ # @return instance [Types::OneOf]
15
+ def one_of(&block)
16
+ Types::OneOf.new(&block)
17
+ end
13
18
  end
14
19
  end
@@ -2,6 +2,7 @@
2
2
 
3
3
  require "store_model/combine_errors_strategies/mark_invalid_error_strategy"
4
4
  require "store_model/combine_errors_strategies/merge_error_strategy"
5
+ require "store_model/combine_errors_strategies/merge_array_error_strategy"
5
6
 
6
7
  module StoreModel
7
8
  # Module with built-in strategies for combining errors.
@@ -16,10 +17,31 @@ module StoreModel
16
17
  def configure(options)
17
18
  configured_strategy = options[:merge_errors] || StoreModel.config.merge_errors
18
19
 
20
+ get_configured_strategy(
21
+ configured_strategy,
22
+ StoreModel::CombineErrorsStrategies::MergeErrorStrategy
23
+ )
24
+ end
25
+
26
+ # Finds a array strategy based on +options+ and global config.
27
+ #
28
+ # @param options [Hash]
29
+ #
30
+ # @return [Object] strategy
31
+ def configure_array(options)
32
+ configured_strategy = options[:merge_array_errors] || StoreModel.config.merge_array_errors
33
+
34
+ get_configured_strategy(
35
+ configured_strategy,
36
+ StoreModel::CombineErrorsStrategies::MergeArrayErrorStrategy
37
+ )
38
+ end
39
+
40
+ def get_configured_strategy(configured_strategy, true_strategy_class)
19
41
  if configured_strategy.respond_to?(:call)
20
42
  configured_strategy
21
43
  elsif configured_strategy == true
22
- StoreModel::CombineErrorsStrategies::MergeErrorStrategy.new
44
+ true_strategy_class.new
23
45
  elsif configured_strategy.nil?
24
46
  StoreModel::CombineErrorsStrategies::MarkInvalidErrorStrategy.new
25
47
  else
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module StoreModel
4
+ module CombineErrorsStrategies
5
+ # +MergeArrayErrorStrategy+ copies errors from the StoreModel::Model to the parent
6
+ # record attribute errors.
7
+ class MergeArrayErrorStrategy
8
+ # Merges errors on +attribute+ from the child model with parent errors.
9
+ #
10
+ # @param attribute [String] name of the validated attribute
11
+ # @param base_errors [ActiveModel::Errors] errors object of the parent record
12
+ # @param store_models [Array] an array or store_models that have been validated
13
+ def call(attribute, base_errors, store_models)
14
+ store_models.each_with_index do |store_model, index|
15
+ store_model.errors.full_messages.each do |full_message|
16
+ base_errors.add(attribute, :invalid, message: "[#{index}] #{full_message}")
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -7,16 +7,17 @@ module StoreModel
7
7
  class MergeErrorStrategy
8
8
  # Merges errors on +attribute+ from the child model with parent errors.
9
9
  #
10
- # @param _attribute [String] name of the validated attribute
10
+ # @param attribute [String] name of the validated attribute
11
11
  # @param base_errors [ActiveModel::Errors] errors object of the parent record
12
12
  # @param store_model_errors [ActiveModel::Errors] errors object of the StoreModel::Model
13
13
  # attribute
14
- def call(_attribute, base_errors, store_model_errors)
14
+ def call(attribute, base_errors, store_model_errors)
15
15
  if Rails::VERSION::MAJOR < 6 || Rails::VERSION::MAJOR == 6 && Rails::VERSION::MINOR.zero?
16
- base_errors.copy!(store_model_errors)
16
+ base_errors.delete(attribute)
17
+ store_model_errors.each { |field, error| base_errors.add(field, error) }
17
18
  else
18
19
  store_model_errors.errors.each do |error|
19
- base_errors.add(:configuration, :invalid, message: error.full_message)
20
+ base_errors.add(attribute, :invalid, message: error.full_message)
20
21
  end
21
22
  end
22
23
  end
@@ -6,5 +6,9 @@ module StoreModel
6
6
  # Controls usage of MergeErrorStrategy
7
7
  # @return [Boolean]
8
8
  attr_accessor :merge_errors
9
+
10
+ # Controls usage of MergeArrayErrorStrategy
11
+ # @return [Boolean]
12
+ attr_accessor :merge_array_errors
9
13
  end
10
14
  end
@@ -70,13 +70,37 @@ module StoreModel
70
70
 
71
71
  # Checks if the attribute with a given name is defined
72
72
  #
73
+ # @example
74
+ # class Person
75
+ # include StoreModel::Model
76
+ # attribute :name, :string
77
+ # alias_attribute :new_name, :name
78
+ # end
79
+ #
80
+ # Person.has_attribute?('name') # => true
81
+ # Person.has_attribute?('new_name') # => true
82
+ # Person.has_attribute?(:age) # => true
83
+ # Person.has_attribute?(:nothing) # => false
84
+ #
73
85
  # @param attr_name [String] name of the attribute
74
86
  #
75
87
  # @return [Boolean]
76
88
  # rubocop:disable Naming/PredicateName
77
89
  def has_attribute?(attr_name)
78
- attribute_types.key?(attr_name.to_s)
90
+ attr_name = attr_name.to_s
91
+ attr_name = self.class.attribute_aliases[attr_name] || attr_name
92
+ attribute_types.key?(attr_name)
93
+ end
94
+
95
+ # Legacy implementation of #has_attribute?
96
+ #
97
+ # @param attr_name [String] name of the attribute
98
+ #
99
+ # @return [Boolean]
100
+ def _has_attribute?(attr_name)
101
+ attribute_types.key?(attr_name)
79
102
  end
103
+
80
104
  # rubocop:enable Naming/PredicateName
81
105
 
82
106
  # Contains a hash of attributes which are not defined but exist in the
@@ -14,9 +14,9 @@ module StoreModel
14
14
  def accepts_nested_attributes_for(*associations)
15
15
  associations.each do |association|
16
16
  case attribute_types[association.to_s]
17
- when Types::JsonType
17
+ when Types::One
18
18
  alias_method "#{association}_attributes=", "#{association}="
19
- when Types::ArrayType
19
+ when Types::Many
20
20
  define_method "#{association}_attributes=" do |attributes|
21
21
  assign_nested_attributes_for_collection_association(association, attributes)
22
22
  end
@@ -3,16 +3,16 @@
3
3
  module StoreModel
4
4
  # Contains methods for converting StoreModel::Model to ActiveModel::Type::Value.
5
5
  module TypeBuilders
6
- # Converts StoreModel::Model to Types::JsonType
7
- # @return [Types::JsonType]
6
+ # Converts StoreModel::Model to Types::One
7
+ # @return [Types::One]
8
8
  def to_type
9
- Types::JsonType.new(self)
9
+ Types::One.new(self)
10
10
  end
11
11
 
12
- # Converts StoreModel::Model to Types::ArrayType
13
- # @return [Types::ArrayType]
12
+ # Converts StoreModel::Model to Types::Many
13
+ # @return [Types::Many]
14
14
  def to_array_type
15
- Types::ArrayType.new(self)
15
+ Types::Many.new(self)
16
16
  end
17
17
  end
18
18
  end
@@ -1,12 +1,23 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "store_model/types/json_type"
4
- require "store_model/types/array_type"
3
+ require "store_model/types/polymorphic_helper"
4
+
5
+ require "store_model/types/one_base"
6
+ require "store_model/types/one"
7
+ require "store_model/types/one_polymorphic"
8
+
9
+ require "store_model/types/many_base"
10
+ require "store_model/types/many"
11
+ require "store_model/types/many_polymorphic"
12
+
5
13
  require "store_model/types/enum_type"
6
14
 
15
+ require "store_model/types/one_of"
16
+
7
17
  module StoreModel
8
18
  # Contains all custom types.
9
19
  module Types
10
20
  class CastError < StandardError; end
21
+ class ExpandWrapperError < StandardError; end
11
22
  end
12
23
  end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_model"
4
+
5
+ module StoreModel
6
+ module Types
7
+ # Implements ActiveModel::Type::Value type for handling an array of
8
+ # StoreModel::Model
9
+ class Many < ManyBase
10
+ # Initializes type for model class
11
+ #
12
+ # @param model_klass [StoreModel::Model] model class to handle
13
+ #
14
+ # @return [StoreModel::Types::Many]
15
+ def initialize(model_klass)
16
+ @model_klass = model_klass
17
+ end
18
+
19
+ # Returns type
20
+ #
21
+ # @return [Symbol]
22
+ def type
23
+ :array
24
+ end
25
+
26
+ protected
27
+
28
+ def ensure_model_class(array)
29
+ array.map do |object|
30
+ object.is_a?(@model_klass) ? object : cast_model_type_value(object)
31
+ end
32
+ end
33
+
34
+ def cast_model_type_value(value)
35
+ model_klass_type.cast_value(value)
36
+ end
37
+
38
+ def model_klass_type
39
+ @model_klass_type ||= @model_klass.to_type
40
+ end
41
+
42
+ def raise_cast_error(value)
43
+ raise StoreModel::Types::CastError,
44
+ "failed casting #{value.inspect}, only String or Array instances are allowed"
45
+ end
46
+ end
47
+ end
48
+ end
@@ -6,21 +6,12 @@ module StoreModel
6
6
  module Types
7
7
  # Implements ActiveModel::Type::Value type for handling an array of
8
8
  # StoreModel::Model
9
- class ArrayType < ActiveModel::Type::Value
10
- # Initializes type for model class
11
- #
12
- # @param model_klass [StoreModel::Model] model class to handle
13
- #
14
- # @return [StoreModel::Types::ArrayType]
15
- def initialize(model_klass)
16
- @model_klass = model_klass
17
- end
18
-
9
+ class ManyBase < ActiveModel::Type::Value
19
10
  # Returns type
20
11
  #
21
12
  # @return [Symbol]
22
13
  def type
23
- :array
14
+ raise NotImplementedError
24
15
  end
25
16
 
26
17
  # Casts +value+ from DB or user to StoreModel::Model instance
@@ -34,8 +25,7 @@ module StoreModel
34
25
  when Array then ensure_model_class(value)
35
26
  when nil then value
36
27
  else
37
- raise StoreModel::Types::CastError,
38
- "failed casting #{value.inspect}, only String or Array instances are allowed"
28
+ raise_cast_error(value)
39
29
  end
40
30
  end
41
31
 
@@ -64,28 +54,28 @@ module StoreModel
64
54
  cast_value(raw_old_value) != new_value
65
55
  end
66
56
 
67
- private
57
+ protected
68
58
 
69
- # rubocop:disable Style/RescueModifier
70
- def decode_and_initialize(array_value)
71
- decoded = ActiveSupport::JSON.decode(array_value) rescue []
72
- decoded.map { |attributes| cast_model_type_value(attributes) }
59
+ def ensure_model_class(_array)
60
+ raise NotImplementedError
73
61
  end
74
- # rubocop:enable Style/RescueModifier
75
62
 
76
- def ensure_model_class(array)
77
- array.map do |object|
78
- object.is_a?(@model_klass) ? object : cast_model_type_value(object)
79
- end
63
+ def cast_model_type_value(_value)
64
+ raise NotImplementedError
80
65
  end
81
66
 
82
- def cast_model_type_value(value)
83
- model_klass_type.cast_value(value)
67
+ def raise_cast_error(_value)
68
+ raise NotImplementedError
84
69
  end
85
70
 
86
- def model_klass_type
87
- @model_klass_type ||= @model_klass.to_type
71
+ private
72
+
73
+ # rubocop:disable Style/RescueModifier
74
+ def decode_and_initialize(array_value)
75
+ decoded = ActiveSupport::JSON.decode(array_value) rescue []
76
+ decoded.map { |attributes| cast_model_type_value(attributes) }
88
77
  end
78
+ # rubocop:enable Style/RescueModifier
89
79
  end
90
80
  end
91
81
  end
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_model"
4
+
5
+ module StoreModel
6
+ module Types
7
+ # Implements ActiveModel::Type::Value type for handling an array of
8
+ # StoreModel::Model
9
+ class ManyPolymorphic < ManyBase
10
+ include PolymorphicHelper
11
+
12
+ # Initializes type for model class
13
+ #
14
+ # @param model_wrapper [Proc] class to handle
15
+ #
16
+ # @return [StoreModel::Types::PolymorphicArrayType ]
17
+ def initialize(model_wrapper)
18
+ @model_wrapper = model_wrapper
19
+ end
20
+
21
+ # Returns type
22
+ #
23
+ # @return [Symbol]
24
+ def type
25
+ :polymorphic_array
26
+ end
27
+
28
+ private
29
+
30
+ def ensure_model_class(array)
31
+ array.map do |object|
32
+ next object if implements_model?(object.class)
33
+
34
+ cast_model_type_value(object)
35
+ end
36
+ end
37
+
38
+ def cast_model_type_value(value)
39
+ model_klass = @model_wrapper.call(value)
40
+
41
+ raise_extract_wrapper_error(model_klass) unless implements_model?(model_klass)
42
+
43
+ model_klass.to_type.cast_value(value)
44
+ end
45
+
46
+ def raise_cast_error(value)
47
+ raise StoreModel::Types::CastError,
48
+ "failed casting #{value.inspect}, only String, " \
49
+ "Hash or instances which implement StoreModel::Model are allowed"
50
+ end
51
+ end
52
+ end
53
+ end
@@ -5,12 +5,12 @@ require "active_model"
5
5
  module StoreModel
6
6
  module Types
7
7
  # Implements ActiveModel::Type::Value type for handling an instance of StoreModel::Model
8
- class JsonType < ActiveModel::Type::Value
8
+ class One < OneBase
9
9
  # Initializes type for model class
10
10
  #
11
11
  # @param model_klass [StoreModel::Model] model class to handle
12
12
  #
13
- # @return [StoreModel::Types::JsonType]
13
+ # @return [StoreModel::Types::One]
14
14
  def initialize(model_klass)
15
15
  @model_klass = model_klass
16
16
  end
@@ -30,7 +30,7 @@ module StoreModel
30
30
  def cast_value(value)
31
31
  case value
32
32
  when String then decode_and_initialize(value)
33
- when Hash then @model_klass.new(value)
33
+ when Hash then model_instance(value)
34
34
  when @model_klass, nil then value
35
35
  else raise_cast_error(value)
36
36
  end
@@ -53,40 +53,16 @@ module StoreModel
53
53
  end
54
54
  end
55
55
 
56
- # Determines whether the mutable value has been modified since it was read
57
- #
58
- # @param raw_old_value [Object] old value
59
- # @param new_value [Object] new value
60
- #
61
- # @return [Boolean]
62
- def changed_in_place?(raw_old_value, new_value)
63
- cast_value(raw_old_value) != new_value
64
- end
65
-
66
56
  private
67
57
 
68
- # rubocop:disable Style/RescueModifier
69
- def decode_and_initialize(value)
70
- decoded = ActiveSupport::JSON.decode(value) rescue nil
71
- @model_klass.new(decoded) unless decoded.nil?
72
- rescue ActiveModel::UnknownAttributeError => e
73
- handle_unknown_attribute(decoded, e)
74
- end
75
- # rubocop:enable Style/RescueModifier
76
-
77
58
  def raise_cast_error(value)
78
59
  raise StoreModel::Types::CastError,
79
60
  "failed casting #{value.inspect}, only String, " \
80
61
  "Hash or #{@model_klass.name} instances are allowed"
81
62
  end
82
63
 
83
- def handle_unknown_attribute(value, exception)
84
- attribute = exception.attribute.to_sym
85
- value_symbolized = value.symbolize_keys
86
-
87
- cast_value(value_symbolized.except(attribute)).tap do |configuration|
88
- configuration.unknown_attributes[attribute.to_s] = value_symbolized[attribute]
89
- end
64
+ def model_instance(value)
65
+ @model_klass.new(value)
90
66
  end
91
67
  end
92
68
  end
@@ -0,0 +1,67 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_model"
4
+
5
+ module StoreModel
6
+ module Types
7
+ # Implements ActiveModel::Type::Value type for handling an instance of StoreModel::Model
8
+ class OneBase < ActiveModel::Type::Value
9
+ # Returns type
10
+ #
11
+ # @return [Symbol]
12
+ def type
13
+ raise NotImplementedError
14
+ end
15
+
16
+ # Casts +value+ from DB or user to StoreModel::Model instance
17
+ #
18
+ # @param value [Object] a value to cast
19
+ #
20
+ # @return StoreModel::Model
21
+ def cast_value(_value)
22
+ raise NotImplementedError
23
+ end
24
+
25
+ # Determines whether the mutable value has been modified since it was read
26
+ #
27
+ # @param raw_old_value [Object] old value
28
+ # @param new_value [Object] new value
29
+ #
30
+ # @return [Boolean]
31
+ def changed_in_place?(raw_old_value, new_value)
32
+ cast_value(raw_old_value) != new_value
33
+ end
34
+
35
+ protected
36
+
37
+ def raise_cast_error(_value)
38
+ raise NotImplementedError
39
+ end
40
+
41
+ def model_instance(_value)
42
+ raise NotImplementedError
43
+ end
44
+
45
+ private
46
+
47
+ # rubocop:disable Style/RescueModifier
48
+ def decode_and_initialize(value)
49
+ decoded = ActiveSupport::JSON.decode(value) rescue nil
50
+ model_instance(decoded) unless decoded.nil?
51
+ rescue ActiveModel::UnknownAttributeError => e
52
+ handle_unknown_attribute(decoded, e)
53
+ end
54
+ # rubocop:enable Style/RescueModifier
55
+
56
+ def handle_unknown_attribute(value, exception)
57
+ attribute = exception.attribute.to_sym
58
+ value_symbolized = value.symbolize_keys
59
+ value_symbolized = value_symbolized[:attributes] if value_symbolized.key?(:attributes)
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
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_model"
4
+
5
+ module StoreModel
6
+ module Types
7
+ # Implements ActiveModel::Type::Value type for handling an array of
8
+ # StoreModel::Model
9
+ class OneOf
10
+ def initialize(&block)
11
+ @block = block
12
+ end
13
+
14
+ def to_type
15
+ Types::OnePolymorphic.new(@block)
16
+ end
17
+
18
+ def to_array_type
19
+ Types::ManyPolymorphic.new(@block)
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,89 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_model"
4
+
5
+ module StoreModel
6
+ module Types
7
+ # Implements ActiveModel::Type::Value type for handling an instance of StoreModel::Model
8
+ class OnePolymorphic < OneBase
9
+ include PolymorphicHelper
10
+
11
+ # Initializes type for model class
12
+ #
13
+ # @param model_wrapper [Proc] class to handle
14
+ #
15
+ # @return [StoreModel::Types::OnePolymorphic ]
16
+ def initialize(model_wrapper)
17
+ @model_wrapper = model_wrapper
18
+ end
19
+
20
+ # Returns type
21
+ #
22
+ # @return [Symbol]
23
+ def type
24
+ :polymorphic
25
+ end
26
+
27
+ # Casts +value+ from DB or user to StoreModel::Model instance
28
+ #
29
+ # @param value [Object] a value to cast
30
+ #
31
+ # @return StoreModel::Model
32
+ def cast_value(value)
33
+ case value
34
+ when String then decode_and_initialize(value)
35
+ when Hash then extract_model_klass(value).new(value)
36
+ when nil then value
37
+ else
38
+ raise_cast_error(value) unless value.class.ancestors.include?(StoreModel::Model)
39
+
40
+ value
41
+ end
42
+ rescue ActiveModel::UnknownAttributeError => e
43
+ handle_unknown_attribute(value, e)
44
+ end
45
+
46
+ # Casts a value from the ruby type to a type that the database knows how
47
+ # to understand.
48
+ #
49
+ # @param value [Object] value to serialize
50
+ #
51
+ # @return [String] serialized value
52
+ def serialize(value)
53
+ case value
54
+ when Hash
55
+ ActiveSupport::JSON.encode(value)
56
+ else
57
+ return ActiveSupport::JSON.encode(value) if implements_model?(value.class)
58
+
59
+ super
60
+ end
61
+ end
62
+
63
+ protected
64
+
65
+ # Check if block returns an appropriate class and raise cast error if not
66
+ #
67
+ # @param value [Object] raw data
68
+ #
69
+ # @return [Class] which implements StoreModel::Model
70
+ def extract_model_klass(value)
71
+ model_klass = @model_wrapper.call(value)
72
+
73
+ raise_extract_wrapper_error(model_klass) unless implements_model?(model_klass)
74
+
75
+ model_klass
76
+ end
77
+
78
+ def raise_cast_error(value)
79
+ raise StoreModel::Types::CastError,
80
+ "failed casting #{value.inspect}, only String, " \
81
+ "Hash or instances which implement StoreModel::Model are allowed"
82
+ end
83
+
84
+ def model_instance(value)
85
+ extract_model_klass(value).new(value)
86
+ end
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Shared methods for polymorphic classes
4
+ module PolymorphicHelper
5
+ def raise_extract_wrapper_error(invalid_klass)
6
+ raise StoreModel::Types::ExpandWrapperError,
7
+ "#{invalid_klass.inspect} is an invalid model klass"
8
+ end
9
+
10
+ def implements_model?(klass)
11
+ klass&.ancestors&.include?(StoreModel::Model)
12
+ end
13
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module StoreModel # :nodoc:
4
- VERSION = "0.6.2"
4
+ VERSION = "0.9.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.6.2
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - DmitryTsepelev
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-11-17 00:00:00.000000000 Z
11
+ date: 2021-04-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -56,16 +56,16 @@ dependencies:
56
56
  name: rubocop
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - ">="
59
+ - - '='
60
60
  - !ruby/object:Gem::Version
61
- version: '0'
61
+ version: 0.64.0
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - ">="
66
+ - - '='
67
67
  - !ruby/object:Gem::Version
68
- version: '0'
68
+ version: 0.64.0
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: coveralls
71
71
  requirement: !ruby/object:Gem::Requirement
@@ -94,6 +94,7 @@ files:
94
94
  - lib/store_model.rb
95
95
  - lib/store_model/combine_errors_strategies.rb
96
96
  - lib/store_model/combine_errors_strategies/mark_invalid_error_strategy.rb
97
+ - lib/store_model/combine_errors_strategies/merge_array_error_strategy.rb
97
98
  - lib/store_model/combine_errors_strategies/merge_error_strategy.rb
98
99
  - lib/store_model/configuration.rb
99
100
  - lib/store_model/enum.rb
@@ -105,15 +106,21 @@ files:
105
106
  - lib/store_model/railtie.rb
106
107
  - lib/store_model/type_builders.rb
107
108
  - lib/store_model/types.rb
108
- - lib/store_model/types/array_type.rb
109
109
  - lib/store_model/types/enum_type.rb
110
- - lib/store_model/types/json_type.rb
110
+ - lib/store_model/types/many.rb
111
+ - lib/store_model/types/many_base.rb
112
+ - lib/store_model/types/many_polymorphic.rb
113
+ - lib/store_model/types/one.rb
114
+ - lib/store_model/types/one_base.rb
115
+ - lib/store_model/types/one_of.rb
116
+ - lib/store_model/types/one_polymorphic.rb
117
+ - lib/store_model/types/polymorphic_helper.rb
111
118
  - lib/store_model/version.rb
112
119
  homepage: https://github.com/DmitryTsepelev/store_model
113
120
  licenses:
114
121
  - MIT
115
122
  metadata: {}
116
- post_install_message:
123
+ post_install_message:
117
124
  rdoc_options: []
118
125
  require_paths:
119
126
  - lib
@@ -128,8 +135,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
128
135
  - !ruby/object:Gem::Version
129
136
  version: '0'
130
137
  requirements: []
131
- rubygems_version: 3.0.3
132
- signing_key:
138
+ rubygems_version: 3.1.2
139
+ signing_key:
133
140
  specification_version: 4
134
141
  summary: Gem for working with JSON-backed attributes as ActiveRecord models
135
142
  test_files: []