store_model 0.10.0 → 0.13.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: 1c612c06744ac1427fd3c3d718e414592ce983734f6aa547e7ffe052b6ac0ef3
4
- data.tar.gz: 349b6f3625d2d94a585c119a3e2080519f9509ee738633062a95364ffd47eb98
3
+ metadata.gz: f02467ba044729f1a4196b520ae35b90157cd09b8de8a41db53673f74bd24481
4
+ data.tar.gz: f893737770e749558b124154c81d3400b2f7a6defcfccee7164ad7fc667ba797
5
5
  SHA512:
6
- metadata.gz: d1df9be55ae81ae2648a1d1c9dc287731ae2954cbfbe9ea618d27aa50b954e5c867900b147aa7ede58382ecf21fcba8e62e6f1b2571700e1e64e76c834c16f69
7
- data.tar.gz: 9224ce8f181e58f02e0fec9864acf1ba4c22e8a575d40ad6e3a66e354178c4c2b4478cb19e8290304da98fe06b0d862fd8da558f71f91ae4dc577d1f2a827000
6
+ metadata.gz: 8b3357e13f694420ebc0e58018fe247ed28cf7cab32345b24208c49751e119561a4413f11bb39cf80bab99f54cbfbd818dbcd54a4f6e801c16dd9cf22f7c5392
7
+ data.tar.gz: 9679bf7228ac2156f6704a4ac457db2d39f8837d428a04607bbb9a09e07dfeaa843ff91d929e04690ef6c1b65e1d2b318b4a98cc4beaba125f48e16659b82c16
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
@@ -29,7 +29,7 @@ module StoreModel
29
29
  #
30
30
  # @return [Hash]
31
31
  def as_json(options = {})
32
- attributes.with_indifferent_access.as_json(options)
32
+ attributes.with_indifferent_access.merge(unknown_attributes).as_json(options)
33
33
  end
34
34
 
35
35
  # Compares two StoreModel::Model instances
@@ -43,6 +43,13 @@ module StoreModel
43
43
  attributes.all? { |name, value| value == other.attributes[name] }
44
44
  end
45
45
 
46
+ # Returns hash for a StoreModel::Model instance based on attributes hash
47
+ #
48
+ # @return [Integer]
49
+ def hash
50
+ attributes.hash
51
+ end
52
+
46
53
  # Allows to call :presence validation on the association itself.
47
54
  #
48
55
  # @return [Boolean]
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module StoreModel # :nodoc:
4
- VERSION = "0.10.0"
4
+ VERSION = "0.13.0"
5
5
  end
data/lib/store_model.rb CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  require "store_model/model"
4
4
  require "store_model/configuration"
5
- require "store_model/railtie"
5
+ require "store_model/railtie" if defined?(::Rails::Railtie)
6
6
  require "active_model/validations/store_model_validator"
7
7
 
8
8
  module StoreModel # :nodoc:
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.10.0
4
+ version: 0.13.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: 2021-07-06 00:00:00.000000000 Z
11
+ date: 2022-02-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -120,7 +120,7 @@ homepage: https://github.com/DmitryTsepelev/store_model
120
120
  licenses:
121
121
  - MIT
122
122
  metadata: {}
123
- post_install_message:
123
+ post_install_message:
124
124
  rdoc_options: []
125
125
  require_paths:
126
126
  - lib
@@ -128,15 +128,15 @@ 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
139
- signing_key:
138
+ rubygems_version: 3.0.3.1
139
+ signing_key:
140
140
  specification_version: 4
141
141
  summary: Gem for working with JSON-backed attributes as ActiveRecord models
142
142
  test_files: []