store_model 2.2.0 → 2.4.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: 738610a630b9d60da6e855965f5f2ecfee4bb1acd68d89b8f012645f62930591
4
+ data.tar.gz: 512a83d32803641b177f9bd385f7c543595492851e96f296f7257fcc6ac8275e
5
5
  SHA512:
6
- metadata.gz: d8a703611d38eedfa8d66e5db71084df8a89676e8f6401207451160278ec4df9c7274acab73027d6eba9c6bcb884c77ebd06c420fc08dd592ea423b7e45f9821
7
- data.tar.gz: c2a876abb12ccd218c40f0e0d9d89613ea03f0d318f7dcffb83c85ea6f6cee3bebc52caf8ae2cf21bc0bc37969d271d04d61a6199d08865ab54ce40277f36a91
6
+ metadata.gz: 4ef1e9fab0a057edc2322593fb17595e4517c1c10c87b726c02f84deb1aabbd73a37949c885987cf9f342892d2df17675b4e18839a82f0d6153c57797930acb8
7
+ data.tar.gz: 150c9d6dc291512309046c9049316c3ad9f2784e0989751ae406dcc6924a43973b88c2c606a8645c6a25d9243816d7f0dc7470ffbe02512837db4862f783b988
data/README.md CHANGED
@@ -132,6 +132,7 @@ end
132
132
  4. [One of](./docs/one_of.md)
133
133
  4. [Alternatives](./docs/alternatives.md)
134
134
  5. [Defining custom types](./docs/defining_custom_types.md)
135
+ 6. [Disabling Parent Tracking](./docs/enable_parent_assignment.md)
135
136
 
136
137
  ## Credits
137
138
 
@@ -15,12 +15,18 @@ module StoreModel
15
15
  # @return [Boolean]
16
16
  attr_accessor :serialize_unknown_attributes
17
17
 
18
- # Controls if the result of `as_json` will serialize enum fiels using `as_json`
18
+ # Controls if the result of `as_json` will serialize enum fields using `as_json`
19
19
  # @return [Boolean]
20
20
  attr_accessor :serialize_enums_using_as_json
21
21
 
22
+ # Controls if parent tracking functionality is enabled.
23
+ # Default: true
24
+ # @return [Boolean]
25
+ attr_accessor :enable_parent_assignment
26
+
22
27
  def initialize
23
28
  @serialize_unknown_attributes = true
29
+ @enable_parent_assignment = true
24
30
  end
25
31
  end
26
32
  end
@@ -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)
@@ -7,8 +7,10 @@ module StoreModel # :nodoc:
7
7
  class Railtie < Rails::Railtie # :nodoc:
8
8
  config.to_prepare do |_app|
9
9
  ActiveSupport.on_load(:active_record) do
10
- ActiveModel::Attributes.prepend(Attributes)
11
- prepend(Base)
10
+ if StoreModel.config.enable_parent_assignment
11
+ ActiveModel::Attributes.prepend(Attributes)
12
+ prepend(Base)
13
+ end
12
14
  end
13
15
  end
14
16
  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
 
@@ -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.4.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.4.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-04-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord