serialize_attributes 0.2.0 → 0.3.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 +4 -4
- data/README.md +7 -0
- data/lib/serialize_attributes/store.rb +27 -2
- data/lib/serialize_attributes/version.rb +1 -1
- data/lib/serialize_attributes.rb +4 -3
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 28fc6ce168d3889baa7be09c4e1a18c794ffda743d8966ba3b763b50c35e96c0
|
4
|
+
data.tar.gz: cc7f1eb8f24138ecd4eaa12e8253d192ae3e35bb6b83c63c791490b9fc544202
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 755dc02f7b3483632cbbbfc13f0f5faec360729015f2aa07a6bb707e3cee7ecc2f018f69da71b5230bea70cbee75993e11dd8cf625d532372ceb119aed2a115e
|
7
|
+
data.tar.gz: d20e235ed061cf26773ba03fe047e692ab7c57e03d68742beb30c48d9d533c70be8b4fd84a913ee95a37329b2cc2cf8ac6fc817c0a06643131d22a7bbed4bccb
|
data/README.md
CHANGED
@@ -83,6 +83,13 @@ MyModel.serialized_attribute_names(:settings)
|
|
83
83
|
#=> [:user_name, :subscribed]
|
84
84
|
```
|
85
85
|
|
86
|
+
You can also get a list of the attributes filtered by a type specifier:
|
87
|
+
|
88
|
+
```ruby
|
89
|
+
MyModel.serialized_attribute_names(:settings, :boolean)
|
90
|
+
#=> [:subscribed]
|
91
|
+
```
|
92
|
+
|
86
93
|
### Complex types
|
87
94
|
|
88
95
|
Underneath, we use the `ActiveModel::Type` mechanism for type coercion, which means
|
@@ -15,8 +15,33 @@ module SerializeAttributes
|
|
15
15
|
[self, @attributes, @defaults].each(&:freeze)
|
16
16
|
end
|
17
17
|
|
18
|
-
# Get a list of the attributes managed by this store
|
19
|
-
|
18
|
+
# Get a list of the attributes managed by this store. Pass an optional `type` argument
|
19
|
+
# to filter attributes by their type.
|
20
|
+
#
|
21
|
+
# Model.serialize_attributes_store(:settings).attribute_names
|
22
|
+
# => [:user_name, :subscribed]
|
23
|
+
#
|
24
|
+
# Model.serialize_attributes_store(:settings).attribute_names(:string)
|
25
|
+
# => [:user_name]
|
26
|
+
def attribute_names(type = nil)
|
27
|
+
if type
|
28
|
+
type = ActiveModel::Type.lookup(type)&.class if type.is_a?(Symbol)
|
29
|
+
@attributes.select do |_, v|
|
30
|
+
v.is_a?(type)
|
31
|
+
end
|
32
|
+
else
|
33
|
+
@attributes
|
34
|
+
end.keys
|
35
|
+
end
|
36
|
+
|
37
|
+
# Get a list of attributes of a certain type
|
38
|
+
#
|
39
|
+
# Model.serialize_attributes_store(:settings).attributes_of_type(:string)
|
40
|
+
# => [:user_name]
|
41
|
+
def attributes_of_type(type)
|
42
|
+
type = ActiveModel::Type.lookup(type) if type.is_a?(Symbol)
|
43
|
+
@attributes.select { |_, v| v.is_a?(type) }.keys
|
44
|
+
end
|
20
45
|
|
21
46
|
# Cast a stored attribute against a given name
|
22
47
|
#
|
data/lib/serialize_attributes.rb
CHANGED
@@ -34,9 +34,10 @@ module SerializeAttributes
|
|
34
34
|
|
35
35
|
# Get a list of the attributes registered in a given store
|
36
36
|
#
|
37
|
-
# Person.serialized_attribute_names(:settings)
|
38
|
-
|
39
|
-
|
37
|
+
# Person.serialized_attribute_names(:settings, :string)
|
38
|
+
# => [:user_name]
|
39
|
+
def serialized_attribute_names(column_name, type = nil)
|
40
|
+
serialized_attributes_store(column_name).attribute_names(type)
|
40
41
|
end
|
41
42
|
end
|
42
43
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: serialize_attributes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Zaikio
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-01-
|
11
|
+
date: 2022-01-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|