serdes 0.1.2 → 0.1.4
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/CHANGELOG.md +8 -0
- data/README.md +12 -0
- data/lib/serdes/version.rb +1 -1
- data/lib/serdes.rb +23 -17
- 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: bc5a0f39e261348c4f4d222f795e09de9dc6f5166d070214b0ffc48ab5615da6
|
4
|
+
data.tar.gz: f0c40e4eb6fec6227754dedb5ab984a87a35e9bbdba1f8dd0e72e77e3a7145fe
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5607b8aac8a10e6251c530cf39447dd5a9da5754b30b96b5d52f9e87db899085561c2533891183d2d9c3b8137fb22cc29efd69f137c1c19eca569b9fc8f3fe62
|
7
|
+
data.tar.gz: bd25c27df789a9361d9fb8f35cb34dfc14d6181e763682f1af3ed2a18ba52e01aaf482ccdc1b7309b83ae26d404689ea95857b49809d034b28bec90a1c1f7464
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -34,6 +34,7 @@ class User
|
|
34
34
|
attribute :profile, optional(String)
|
35
35
|
attribute :tags, array(String)
|
36
36
|
attribute :has_pet, Boolean
|
37
|
+
attribute :plan, String, only: ["free", "premium"]
|
37
38
|
end
|
38
39
|
|
39
40
|
user_hash = {
|
@@ -71,10 +72,21 @@ User.from(user_hash) # => raise Serdes::TypeError
|
|
71
72
|
|
72
73
|
### Macro
|
73
74
|
|
75
|
+
#### Global macro
|
76
|
+
|
74
77
|
- `rename_all_attributes`: Rename all attributes when serializing and deserializing.
|
75
78
|
- Supported: `:snake_case`, `:PascalCase`
|
76
79
|
- `symbolize_all_keys`: Symbolize all keys when serializing and deserializing Hash, and vice versa.
|
77
80
|
|
81
|
+
#### Attribute macro
|
82
|
+
|
83
|
+
You can also use macro for each attribute by specifying 3rd argument of `attribute`.
|
84
|
+
|
85
|
+
- `only`: Only allow given values.
|
86
|
+
- `skip_serializing`: Skip serializing attribute by setting truthy value. it will ignore `skip_serializing_if`, `skip_serializing_if_nil` when this set.
|
87
|
+
- `skip_serializing_if`: Skip serializing if given proc call returns true.
|
88
|
+
- `skip_serializing_if_nil`: alias of `skip_serializing_if: ->(v) { v.nil? }`. It will ignore `skip_serializing_if` when this set.
|
89
|
+
|
78
90
|
## Development
|
79
91
|
|
80
92
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
data/lib/serdes/version.rb
CHANGED
data/lib/serdes.rb
CHANGED
@@ -102,6 +102,23 @@ module Serdes
|
|
102
102
|
@name = name
|
103
103
|
@attr_type = attr_type
|
104
104
|
@options = options
|
105
|
+
|
106
|
+
if @options[:skip_serializing_if_nil]
|
107
|
+
@options.delete(:skip_serializing_if_nil)
|
108
|
+
@options[:skip_serializing_if] = ->(v) { v.nil? }
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
def permit?(value)
|
113
|
+
return false if !attr_type.permit?(value)
|
114
|
+
|
115
|
+
return true if @options[:only].nil?
|
116
|
+
|
117
|
+
if @options[:only].include?(value)
|
118
|
+
true
|
119
|
+
else
|
120
|
+
raise TypeError, "Wrong value for #{class_name}##{name}. Expected value is #{@options[:only]}, got '#{value}'."
|
121
|
+
end
|
105
122
|
end
|
106
123
|
|
107
124
|
def serialized_name(rename_strategy = nil)
|
@@ -202,8 +219,13 @@ module Serdes
|
|
202
219
|
self.class.__send__(:_serde_attrs).each_with_object({}) do |(name, attr), hash|
|
203
220
|
key = attr.serialized_name(self.class.__send__(:_serde_rename_strategy))
|
204
221
|
key = key.to_sym if self.class.__send__(:_serde_symbolized_all_keys)
|
222
|
+
|
223
|
+
next if attr.options[:skip_serializing]
|
224
|
+
|
205
225
|
value = __send__(name)
|
206
226
|
|
227
|
+
next if attr.options[:skip_serializing_if] && attr.options[:skip_serializing_if].call(value)
|
228
|
+
|
207
229
|
hash[key] =
|
208
230
|
if value.respond_to?(:to_hash)
|
209
231
|
value.to_hash
|
@@ -266,27 +288,11 @@ module Serdes
|
|
266
288
|
end
|
267
289
|
|
268
290
|
def validate_type!(attr, value)
|
269
|
-
|
270
|
-
return if valid
|
271
|
-
|
272
|
-
actual_type = humanize_type(value)
|
291
|
+
return if attr.permit?(value)
|
273
292
|
|
274
293
|
raise TypeError, "Wrong type for #{attr.class_name}##{attr.name}. Expected type #{attr.attr_type}, got #{value.class} (val: '#{value}')."
|
275
294
|
end
|
276
295
|
|
277
|
-
def humanize_type(value)
|
278
|
-
if value.is_a?(Array)
|
279
|
-
"array(" + value.map { |el| humanize_type(el) }.join(", ") + ")"
|
280
|
-
else
|
281
|
-
case value
|
282
|
-
when NilClass then "nil"
|
283
|
-
when TrueClass then "boolean"
|
284
|
-
when FalseClass then "boolean"
|
285
|
-
else value.class.to_s
|
286
|
-
end
|
287
|
-
end
|
288
|
-
end
|
289
|
-
|
290
296
|
def skip_to_hash?(value)
|
291
297
|
case value
|
292
298
|
when NilClass, String, Numeric, TrueClass, FalseClass
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: serdes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Shia
|
8
8
|
bindir: bin
|
9
9
|
cert_chain: []
|
10
|
-
date: 2025-02
|
10
|
+
date: 2025-03-02 00:00:00.000000000 Z
|
11
11
|
dependencies: []
|
12
12
|
description: Serdes is a tool for serializing and deserializing class.
|
13
13
|
email:
|