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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 336ab0663fe21a1f3750663a41aec451f209bf1141f233f3043c914aefd0ebf9
4
- data.tar.gz: c80f05ff696c4292571c5ae613343687377bb10e1235790ea332dc80e6e15230
3
+ metadata.gz: 28fc6ce168d3889baa7be09c4e1a18c794ffda743d8966ba3b763b50c35e96c0
4
+ data.tar.gz: cc7f1eb8f24138ecd4eaa12e8253d192ae3e35bb6b83c63c791490b9fc544202
5
5
  SHA512:
6
- metadata.gz: c3f9f7d020b7002f2b83e816452f4166a26cb4a1f2fdfaf3271977bfb8c80d8f075456ebeeeec042a1c544786f5fa2a5f80a16f7353596eb4817cdc76e768b95
7
- data.tar.gz: 6c0133edf56b9a396cfe011f4abda01d3ac61b1c95dca5329dcaa1d8a29b2bd366ac28f691b8e1d6aa4c565398781131921a6645f8b69a8051565bfabc9b1676
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
- def attribute_names = @attributes.keys
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
  #
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SerializeAttributes
4
- VERSION = "0.2.0"
4
+ VERSION = "0.3.0"
5
5
  end
@@ -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
- def serialized_attribute_names(column_name)
39
- serialized_attributes_store(column_name).attribute_names
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.2.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-12 00:00:00.000000000 Z
11
+ date: 2022-01-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel