store_model 2.1.2 → 2.3.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: db36f4fa51b9f71a34198ba18b921565d46a450c62e4892628dfee8e067bf4ff
4
- data.tar.gz: 4e72e8f0fc7051105a09843040a4cfc61f248884ac2dcc8def0f6a95f9b8390f
3
+ metadata.gz: 7cd3190d949fc4fd854928abccfafa5b74658bb973e1efe1567aa7b0ab0546d8
4
+ data.tar.gz: 22945765f627eca2a7278b46b3f462ee620e5721957d401a5f06b13b7daafffe
5
5
  SHA512:
6
- metadata.gz: 5c5073d90550cd90049cb0cc2555bfab36c4b036857319ac5bf0f0eeadaea70deabcf71e549c7e1348127f719ba9f03bda444fb700153b8bdbbb5ffdc3e36c0f
7
- data.tar.gz: f5a3eed707850087dfc95c908725624352da067410b3fa69762764545fd83c243a99bfbf8a4b4ae53086fdff9094377769be720cf7497b0d82ae1d7ad736e411
6
+ metadata.gz: 1a740f2c00feb62259cae03b72f992dfc7ae15538010a3d62a88cf3cdcb3ab5986e20ae56d2f9133adf294ab4d25232b56216ff3f940ad8732bbef8de1dc0e56
7
+ data.tar.gz: e6ad43a005026d5275fd29573ab9dbddd3d8fb966c329eb9a44f7fc2b0406cc9350725cc143daea17953e8d084d9a76b5a13d87d50b213fe0a565e3b9a2c289a
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) [![Coverage Status](https://coveralls.io/repos/github/DmitryTsepelev/store_model/badge.svg?branch=master)](https://coveralls.io/github/DmitryTsepelev/store_model?branch=master) ![](https://ruby-gem-downloads-badge.herokuapp.com/store_model?type=total)
1
+ # StoreModel [![Gem Version](https://badge.fury.io/rb/store_model.svg)](https://rubygems.org/gems/store_model) ![](https://ruby-gem-downloads-badge.herokuapp.com/store_model?type=total)
2
2
 
3
3
  **StoreModel** gem allows you to wrap JSON-backed DB columns with ActiveModel-like classes.
4
4
 
@@ -10,12 +10,12 @@ module StoreModel
10
10
  # @param kwargs [Object]
11
11
  def enum(name, values = nil, **kwargs)
12
12
  values ||= kwargs[:in] || kwargs
13
- options = kwargs.slice(:_prefix, :_suffix, :default)
13
+ options = retrieve_options(kwargs)
14
14
 
15
15
  ensure_hash(values).tap do |mapping|
16
- define_attribute(name, mapping, options[:default])
16
+ define_attribute(name, mapping, options)
17
17
  define_reader(name, mapping)
18
- define_writer(name, mapping)
18
+ define_writer(name, mapping, options[:raise_on_invalid_values])
19
19
  define_method("#{name}_value") { attributes[name.to_s] }
20
20
  define_map_readers(name, mapping)
21
21
  define_predicate_methods(name, mapping, options)
@@ -24,16 +24,25 @@ module StoreModel
24
24
 
25
25
  private
26
26
 
27
- def define_attribute(name, mapping, default)
28
- attribute name, cast_type(mapping), default: default
27
+ def retrieve_options(options)
28
+ default_options = { raise_on_invalid_values: true }
29
+ options.slice(:_prefix, :_suffix, :default, :raise_on_invalid_values)
30
+ .reverse_merge(default_options)
31
+ end
32
+
33
+ def define_attribute(name, mapping, options)
34
+ attribute name, cast_type(mapping, options[:raise_on_invalid_values]), default: options[:default]
29
35
  end
30
36
 
31
37
  def define_reader(name, mapping)
32
- define_method(name) { mapping.key(send("#{name}_value")).to_s }
38
+ define_method(name) do
39
+ raw_value = send("#{name}_value")
40
+ (mapping.key(raw_value) || raw_value).to_s
41
+ end
33
42
  end
34
43
 
35
- def define_writer(name, mapping)
36
- type = cast_type(mapping)
44
+ def define_writer(name, mapping, raise_on_invalid_values)
45
+ type = cast_type(mapping, raise_on_invalid_values)
37
46
  define_method("#{name}=") { |value| super type.cast_value(value) }
38
47
  end
39
48
 
@@ -50,8 +59,8 @@ module StoreModel
50
59
  singleton_class.alias_method(ActiveSupport::Inflector.pluralize(name), "#{name}_values")
51
60
  end
52
61
 
53
- def cast_type(mapping)
54
- StoreModel::Types::EnumType.new(mapping)
62
+ def cast_type(mapping, raise_on_invalid_values)
63
+ StoreModel::Types::EnumType.new(mapping, raise_on_invalid_values)
55
64
  end
56
65
 
57
66
  def ensure_hash(values)
@@ -118,7 +118,7 @@ module StoreModel
118
118
  #
119
119
  # @return [String]
120
120
  def inspect
121
- attribute_string = attributes.map { |name, value| "#{name}: #{value.nil? ? 'nil' : value}" }
121
+ attribute_string = attributes.map { |name, value| "#{name}: #{value.inspect}" }
122
122
  .join(", ")
123
123
  "#<#{self.class.name} #{attribute_string}>"
124
124
  end
@@ -84,7 +84,7 @@ module StoreModel
84
84
  def define_attr_accessor_for_destroy(association, options)
85
85
  return unless options&.dig(:allow_destroy)
86
86
 
87
- attribute_types[association.to_s].model_klass.class_eval do
87
+ nested_attribute_type(association).model_klass.class_eval do
88
88
  attr_accessor :_destroy
89
89
  end
90
90
  end
@@ -11,8 +11,9 @@ module StoreModel
11
11
  # @param mapping [Hash] mapping for enum values
12
12
  #
13
13
  # @return [StoreModel::Types::EnumType]
14
- def initialize(mapping)
14
+ def initialize(mapping, raise_on_invalid_values)
15
15
  @mapping = mapping
16
+ @raise_on_invalid_values = raise_on_invalid_values
16
17
  end
17
18
 
18
19
  # Returns type
@@ -31,7 +32,7 @@ module StoreModel
31
32
  return if value.blank?
32
33
 
33
34
  case value
34
- when String, Symbol then cast_symbol_value(value.to_sym)
35
+ when String, Symbol then cast_symbol_value(value)
35
36
  when Integer then cast_integer_value(value)
36
37
  else
37
38
  raise StoreModel::Types::CastError,
@@ -43,12 +44,12 @@ module StoreModel
43
44
  private
44
45
 
45
46
  def cast_symbol_value(value)
46
- raise_invalid_value!(value) unless @mapping.key?(value.to_sym)
47
- @mapping[value.to_sym]
47
+ raise_invalid_value!(value) if @raise_on_invalid_values && !@mapping.key?(value.to_sym)
48
+ @mapping[value.to_sym] || value
48
49
  end
49
50
 
50
51
  def cast_integer_value(value)
51
- raise_invalid_value!(value) unless @mapping.value?(value)
52
+ raise_invalid_value!(value) if @raise_on_invalid_values && !@mapping.value?(value)
52
53
  value
53
54
  end
54
55
 
@@ -63,7 +63,7 @@ module StoreModel
63
63
  def deserialize(value)
64
64
  case value
65
65
  when String
66
- payload = ActiveSupport::JSON.decode(value) rescue nil
66
+ payload = ActiveSupport::JSON.decode(value) rescue {}
67
67
  model_instance(deserialize_by_types(payload))
68
68
  when Hash
69
69
  model_instance(deserialize_by_types(value))
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module StoreModel # :nodoc:
4
- VERSION = "2.1.2"
4
+ VERSION = "2.3.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: 2.1.2
4
+ version: 2.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - DmitryTsepelev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-10-06 00:00:00.000000000 Z
11
+ date: 2024-03-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -66,20 +66,6 @@ dependencies:
66
66
  - - '='
67
67
  - !ruby/object:Gem::Version
68
68
  version: 0.64.0
69
- - !ruby/object:Gem::Dependency
70
- name: coveralls
71
- requirement: !ruby/object:Gem::Requirement
72
- requirements:
73
- - - ">="
74
- - !ruby/object:Gem::Version
75
- version: '0'
76
- type: :development
77
- prerelease: false
78
- version_requirements: !ruby/object:Gem::Requirement
79
- requirements:
80
- - - ">="
81
- - !ruby/object:Gem::Version
82
- version: '0'
83
69
  description: Gem for working with JSON-backed attributes as ActiveRecord models
84
70
  email:
85
71
  - dmitry.a.tsepelev@gmail.com