serialize_attributes 0.1.0 → 0.2.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 +20 -0
- data/lib/serialize_attributes/store.rb +33 -5
- data/lib/serialize_attributes/version.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: 336ab0663fe21a1f3750663a41aec451f209bf1141f233f3043c914aefd0ebf9
|
4
|
+
data.tar.gz: c80f05ff696c4292571c5ae613343687377bb10e1235790ea332dc80e6e15230
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c3f9f7d020b7002f2b83e816452f4166a26cb4a1f2fdfaf3271977bfb8c80d8f075456ebeeeec042a1c544786f5fa2a5f80a16f7353596eb4817cdc76e768b95
|
7
|
+
data.tar.gz: 6c0133edf56b9a396cfe011f4abda01d3ac61b1c95dca5329dcaa1d8a29b2bd366ac28f691b8e1d6aa4c565398781131921a6645f8b69a8051565bfabc9b1676
|
data/README.md
CHANGED
@@ -117,6 +117,26 @@ class MyModel
|
|
117
117
|
end
|
118
118
|
```
|
119
119
|
|
120
|
+
### Array types
|
121
|
+
|
122
|
+
By default, `ActiveModel::Attribute` does not support Array types, however this library
|
123
|
+
does. The syntax is the same as `ActiveRecord::Attribute` when using the Postgres adapter:
|
124
|
+
|
125
|
+
```ruby
|
126
|
+
class MyModel
|
127
|
+
serialize_attributes :settings do
|
128
|
+
attribute :emails, :string, array: true
|
129
|
+
end
|
130
|
+
end
|
131
|
+
```
|
132
|
+
|
133
|
+
Please note that the default value for an array attribute is always `[]`, unless you
|
134
|
+
specify a `default` attribute yourself explicitly:
|
135
|
+
|
136
|
+
```ruby
|
137
|
+
attribute :emails, :string, array: true, default: ["unknown@example.com"]
|
138
|
+
```
|
139
|
+
|
120
140
|
### Usage with ActiveModel alone
|
121
141
|
|
122
142
|
It's also possible to use this library without `ActiveRecord`:
|
@@ -50,10 +50,17 @@ module SerializeAttributes
|
|
50
50
|
|
51
51
|
def attribute(name, type, **options)
|
52
52
|
name = name.to_sym
|
53
|
+
arr = options.delete(:array) { false }
|
53
54
|
type = ActiveModel::Type.lookup(type, **options.except(:default)) if type.is_a?(Symbol)
|
55
|
+
type = ArrayWrapper.new(type) if arr
|
54
56
|
|
55
57
|
@attributes[name] = type
|
56
|
-
|
58
|
+
|
59
|
+
if options.key?(:default)
|
60
|
+
@defaults[name] = options[:default]
|
61
|
+
elsif arr
|
62
|
+
@defaults[name] = []
|
63
|
+
end
|
57
64
|
|
58
65
|
@model_class.module_eval <<~RUBY, __FILE__, __LINE__ + 1
|
59
66
|
def #{name} # def user_name
|
@@ -72,14 +79,35 @@ module SerializeAttributes
|
|
72
79
|
.serialized_attributes_store(:#{@column_name}) # .serialized_attributes_store(:settings)
|
73
80
|
.cast(:#{name}, value) # .cast(:user_name, value)
|
74
81
|
store = public_send(:#{@column_name}) # store = public_send(:settings)
|
75
|
-
|
76
|
-
|
77
|
-
store.
|
78
|
-
|
82
|
+
#
|
83
|
+
if #{arr} && cast_value == ArrayWrapper::EMPTY # if false && cast_value == ArrayWrapper::EMPTY
|
84
|
+
store.delete("#{name}") # store.delete("user_name")
|
85
|
+
else # else
|
86
|
+
store.merge!("#{name}" => cast_value) # store.merge!("user_name" => cast_value)
|
87
|
+
end # end
|
88
|
+
#
|
89
|
+
self.public_send(:#{@column_name}=, store) # self.public_send(:settings=, store)
|
79
90
|
end # end
|
80
91
|
RUBY
|
81
92
|
end
|
82
93
|
|
94
|
+
class ArrayWrapper < SimpleDelegator # :nodoc:
|
95
|
+
EMPTY = Object.new
|
96
|
+
|
97
|
+
def cast(value)
|
98
|
+
# We don't want to store the null value, because array types _always_ have a default
|
99
|
+
# configured. So we return this special object here, and check it again before
|
100
|
+
# updating the underlying store.
|
101
|
+
return EMPTY unless value
|
102
|
+
|
103
|
+
Array(value)
|
104
|
+
end
|
105
|
+
|
106
|
+
def deserialize(value)
|
107
|
+
value.map { __getobj__.deserialize(_1) }
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
83
111
|
class StoreColumnWrapper < SimpleDelegator # :nodoc:
|
84
112
|
def initialize(original, store) # rubocop:disable Lint/MissingSuper
|
85
113
|
__setobj__(original)
|
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.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Zaikio
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-01-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|