serialize_attributes 0.3.1 → 0.4.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 +1 -0
- data/lib/serialize_attributes/store.rb +27 -20
- data/lib/serialize_attributes/version.rb +1 -1
- data/lib/serialize_attributes.rb +1 -1
- 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: c505eb52395e2c8fe3786c3ef97dd04e5a56f978072d839ba517c0069f9dce93
|
4
|
+
data.tar.gz: 5ea660579c45ebd3cca56da5c3ea7e07fe9af5fb38a05fd62d3133f118f3d8df
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 311846e88d0c92cf41bf5adbd32c1515fbd1d131df0d733cfd444963bedc8594620d05cb12cfe701b40fba851195931f6f53673dcbe25593bf257e2b28c183dc
|
7
|
+
data.tar.gz: c5e2e60ba000e0afe423cee6c37e20fae5b590a39bcadab76d65712db712359e1d4ae3c0532e44565d673cb1c8f55cd4d535b9fdc057f50d11d3e3744f0803e9
|
data/README.md
CHANGED
@@ -4,7 +4,8 @@ module SerializeAttributes
|
|
4
4
|
# SerializeAttributes::Store is the individual store, keyed by name. You can get a
|
5
5
|
# reference to the store by calling `Model.serialized_attributes_store(column_name)`.
|
6
6
|
class Store
|
7
|
-
def initialize(model_class, column_name, &block)
|
7
|
+
def initialize(model_class, column_name, &block)
|
8
|
+
# :nodoc:
|
8
9
|
@model_class = model_class
|
9
10
|
@column_name = column_name
|
10
11
|
@attributes = {}
|
@@ -19,30 +20,28 @@ module SerializeAttributes
|
|
19
20
|
# to filter attributes by their type.
|
20
21
|
#
|
21
22
|
# Model.serialize_attributes_store(:settings).attribute_names
|
22
|
-
# => [:user_name, :subscribed]
|
23
|
+
# => [:user_name, :subscribed, :subscriptions]
|
23
24
|
#
|
24
|
-
# Model.serialize_attributes_store(:settings).attribute_names(:string)
|
25
|
-
# => [:user_name]
|
26
|
-
|
25
|
+
# Model.serialize_attributes_store(:settings).attribute_names(type: :string)
|
26
|
+
# => [:user_name, :subscriptions]
|
27
|
+
#
|
28
|
+
# Model.serialize_attributes_store(:settings).attribute_names(type: :string, array: true)
|
29
|
+
# => [:subscriptions]
|
30
|
+
#
|
31
|
+
# Model.serialize_attributes_store(:settings).attribute_names(type: :string, array: false)
|
32
|
+
# => [:user_name]
|
33
|
+
#
|
34
|
+
#
|
35
|
+
def attribute_names(type: nil, array: nil)
|
36
|
+
attributes = @attributes
|
37
|
+
attributes = @attributes.select { |_, v| v.is_a?(ArrayWrapper) == array } unless array.nil?
|
27
38
|
if type
|
28
|
-
|
29
|
-
@attributes.select do |_, v|
|
30
|
-
v.is_a?(type)
|
31
|
-
end
|
39
|
+
attributes_for_type(attributes, type)
|
32
40
|
else
|
33
|
-
|
41
|
+
attributes
|
34
42
|
end.keys
|
35
43
|
end
|
36
44
|
|
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
|
45
|
-
|
46
45
|
# Cast a stored attribute against a given name
|
47
46
|
#
|
48
47
|
# Model.serialized_attributes_store(:settings).cast(:user_name, 42)
|
@@ -73,9 +72,17 @@ module SerializeAttributes
|
|
73
72
|
|
74
73
|
private
|
75
74
|
|
75
|
+
def attributes_for_type(attributes, type)
|
76
|
+
type = ActiveModel::Type.lookup(type)&.class if type.is_a?(Symbol)
|
77
|
+
attributes.select do |_, v|
|
78
|
+
v = v.__getobj__ if v.is_a?(ArrayWrapper)
|
79
|
+
v.is_a?(type)
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
76
83
|
def attribute(name, type, **options)
|
77
84
|
name = name.to_sym
|
78
|
-
arr
|
85
|
+
arr = options.delete(:array) { false }
|
79
86
|
type = ActiveModel::Type.lookup(type, **options.except(:default)) if type.is_a?(Symbol)
|
80
87
|
type = ArrayWrapper.new(type) if arr
|
81
88
|
|
data/lib/serialize_attributes.rb
CHANGED
@@ -37,7 +37,7 @@ module SerializeAttributes
|
|
37
37
|
# Person.serialized_attribute_names(:settings, :string)
|
38
38
|
# => [:user_name]
|
39
39
|
def serialized_attribute_names(column_name, type = nil)
|
40
|
-
serialized_attributes_store(column_name).attribute_names(type)
|
40
|
+
serialized_attributes_store(column_name).attribute_names(type: type)
|
41
41
|
end
|
42
42
|
end
|
43
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.4.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-
|
11
|
+
date: 2022-03-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|