store_model 2.2.0 → 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: 02f882325fbae00f97a61603cdab0578ccfd1723a54b8b6e4cfd61aeaabe8a6f
4
- data.tar.gz: bfd3a7c92a01debecb1fea0d36f73306486612ec7cba22095f9673907ae6b9e4
3
+ metadata.gz: 7cd3190d949fc4fd854928abccfafa5b74658bb973e1efe1567aa7b0ab0546d8
4
+ data.tar.gz: 22945765f627eca2a7278b46b3f462ee620e5721957d401a5f06b13b7daafffe
5
5
  SHA512:
6
- metadata.gz: d8a703611d38eedfa8d66e5db71084df8a89676e8f6401207451160278ec4df9c7274acab73027d6eba9c6bcb884c77ebd06c420fc08dd592ea423b7e45f9821
7
- data.tar.gz: c2a876abb12ccd218c40f0e0d9d89613ea03f0d318f7dcffb83c85ea6f6cee3bebc52caf8ae2cf21bc0bc37969d271d04d61a6199d08865ab54ce40277f36a91
6
+ metadata.gz: 1a740f2c00feb62259cae03b72f992dfc7ae15538010a3d62a88cf3cdcb3ab5986e20ae56d2f9133adf294ab4d25232b56216ff3f940ad8732bbef8de1dc0e56
7
+ data.tar.gz: e6ad43a005026d5275fd29573ab9dbddd3d8fb966c329eb9a44f7fc2b0406cc9350725cc143daea17953e8d084d9a76b5a13d87d50b213fe0a565e3b9a2c289a
@@ -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)
@@ -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
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module StoreModel # :nodoc:
4
- VERSION = "2.2.0"
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.2.0
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: 2024-01-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