store_model 0.10.0 → 0.13.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +5 -0
- data/lib/store_model/enum.rb +12 -3
- data/lib/store_model/model.rb +8 -1
- data/lib/store_model/version.rb +1 -1
- data/lib/store_model.rb +1 -1
- metadata +7 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f02467ba044729f1a4196b520ae35b90157cd09b8de8a41db53673f74bd24481
|
4
|
+
data.tar.gz: f893737770e749558b124154c81d3400b2f7a6defcfccee7164ad7fc667ba797
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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)
|
data/lib/store_model/enum.rb
CHANGED
@@ -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,
|
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
|
data/lib/store_model/model.rb
CHANGED
@@ -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]
|
data/lib/store_model/version.rb
CHANGED
data/lib/store_model.rb
CHANGED
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.
|
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:
|
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.
|
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
|
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: []
|