store_model 0.11.1 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 066a53b97ac50d61d82c10376abe79d3dac452ff2ff33f3a5d650e7e119e356f
4
- data.tar.gz: '0892deb0ac4827ad431ea76c0d1a121bacfbb0fe66204164fba1c6baec685cad'
3
+ metadata.gz: 4da1ca43da439ca2634d69307669020e77ac7349c3ab8fc61ac0386a6e980715
4
+ data.tar.gz: 2f88d2308bfb76be228eebdb7e6ecd1df5f63a4c8b751f0d41a289e5a30cfb1a
5
5
  SHA512:
6
- metadata.gz: 0333e12118a8a445a22ed591e1e887dfc3912f6e02f41e0cc63d87d8dde76a001abd30e5860afba0bde49a4f6163b7092e4fc9c931865586344959c52ddf2cdc
7
- data.tar.gz: 9eaffc2c1660ea778b5360410e1498ff3dd4add451cbcdc1cf6ee32a83fb65bd4286b76929c9cddf7d4c693811bee83cc5669391fa0ea84dcdd0254e5fc44a25
6
+ metadata.gz: 4dd14031ebb8949192d31cfcbcc48609a7670c023d3968e12ee32a7db686081bdc67390cb6538519eb79d1ba3f4cc4ee1ef9f5496d4f06991fae1ee24eb9bb8f
7
+ data.tar.gz: 32e2ae5a3c1a5286cb299039de8b435a919e0618d9251904b3c0c32b439601da7847d9d33e2f039d56831621df4692013d345b8ac946f2c7c59a99d927f53f85
data/README.md CHANGED
@@ -79,6 +79,11 @@ end
79
79
  product.save
80
80
  ```
81
81
 
82
+ _Usage note: Rails and assigning Arrays/Hashes to records_
83
+
84
+ - Assigned attributes must be a String, Hash, Array of Hashes, or StoreModel. For example, if the attributes are coming from a controller, be sure to convert any ActionController::Parameters as needed.
85
+ - Any changes made to a StoreModel instance requires the attribute be re-assigned; Rails doesn't track mutations of objects. For example: `self.my_stored_models = my_stored_models.map(&:as_json)`
86
+
82
87
  ## Documentation
83
88
 
84
89
  1. [Installation](./docs/installation.md)
@@ -10,14 +10,15 @@ 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
14
 
14
15
  ensure_hash(values).tap do |mapping|
15
- define_attribute(name, mapping, kwargs[:default])
16
+ define_attribute(name, mapping, options[:default])
16
17
  define_reader(name, mapping)
17
18
  define_writer(name, mapping)
18
19
  define_method("#{name}_value") { attributes[name.to_s] }
19
20
  define_method("#{name}_values") { mapping }
20
- define_predicate_methods(name, mapping)
21
+ define_predicate_methods(name, mapping, options)
21
22
  end
22
23
  end
23
24
 
@@ -36,8 +37,9 @@ module StoreModel
36
37
  define_method("#{name}=") { |value| super type.cast_value(value) }
37
38
  end
38
39
 
39
- def define_predicate_methods(name, mapping)
40
+ def define_predicate_methods(name, mapping, options)
40
41
  mapping.each do |label, value|
42
+ label = affixed_label(label, name, options[:_prefix], options[:_suffix])
41
43
  define_method("#{label}?") { send(name) == mapping.key(value).to_s }
42
44
  end
43
45
  end
@@ -51,5 +53,12 @@ module StoreModel
51
53
 
52
54
  values.zip(0...values.size).to_h
53
55
  end
56
+
57
+ def affixed_label(label, name, prefix = nil, suffix = nil)
58
+ prefix = prefix == true ? "#{name}_" : "#{prefix}_" if prefix
59
+ suffix = suffix == true ? "_#{name}" : "_#{suffix}" if suffix
60
+
61
+ "#{prefix}#{label}#{suffix}"
62
+ end
54
63
  end
55
64
  end
@@ -15,9 +15,17 @@ module StoreModel
15
15
  end
16
16
  end
17
17
 
18
- def write_attribute(*)
19
- super.tap do |value|
20
- assign_parent_to_store_model_relation(value)
18
+ if Rails::VERSION::MAJOR >= 7
19
+ def _write_attribute(*)
20
+ super.tap do |value|
21
+ assign_parent_to_store_model_relation(value)
22
+ end
23
+ end
24
+ else
25
+ def write_attribute(*)
26
+ super.tap do |value|
27
+ assign_parent_to_store_model_relation(value)
28
+ end
21
29
  end
22
30
  end
23
31
  end
@@ -11,6 +11,7 @@ module StoreModel
11
11
  def self.included(base) # :nodoc:
12
12
  base.include ActiveModel::Model
13
13
  base.include ActiveModel::Attributes
14
+ base.include ActiveRecord::AttributeMethods::BeforeTypeCast
14
15
  base.include ActiveModel::AttributeMethods
15
16
  base.include StoreModel::NestedAttributes
16
17
 
@@ -29,7 +30,7 @@ module StoreModel
29
30
  #
30
31
  # @return [Hash]
31
32
  def as_json(options = {})
32
- attributes.with_indifferent_access.as_json(options)
33
+ attributes.with_indifferent_access.merge(unknown_attributes).as_json(options)
33
34
  end
34
35
 
35
36
  # Compares two StoreModel::Model instances
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module StoreModel # :nodoc:
4
- VERSION = "0.11.1"
4
+ VERSION = "1.0.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.11.1
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - DmitryTsepelev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-09-09 00:00:00.000000000 Z
11
+ date: 2022-06-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -128,14 +128,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
128
128
  requirements:
129
129
  - - ">="
130
130
  - !ruby/object:Gem::Version
131
- version: '2.3'
131
+ version: '2.6'
132
132
  required_rubygems_version: !ruby/object:Gem::Requirement
133
133
  requirements:
134
134
  - - ">="
135
135
  - !ruby/object:Gem::Version
136
136
  version: '0'
137
137
  requirements: []
138
- rubygems_version: 3.1.2
138
+ rubygems_version: 3.3.3
139
139
  signing_key:
140
140
  specification_version: 4
141
141
  summary: Gem for working with JSON-backed attributes as ActiveRecord models