serdes 0.1.3 → 0.1.5
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 -1
- data/lib/serdes/version.rb +1 -1
- data/lib/serdes.rb +33 -3
- metadata +3 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 28edbdb79c30f1f680e78f89b179372e2c947f755e192b4fb4e4e1fc1de88815
|
|
4
|
+
data.tar.gz: 61b27b8fa680feefe6d90e217cb26f729c36dcdb4c3703ab2b788824af6ffce1
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 57776409c4ebc410ec94d3a07723f280fa029d3f5950dcf132f220e153406cd1eaef2e717249bc691aebf8252de087f051844b27ee974582def946b859d29949
|
|
7
|
+
data.tar.gz: 94a768e64bb467d8acc7fa88184dff9e576d0dfb65600e0f5e95eef2f8c3e58c294d79c4e3ecd3c0a8527bf2c51f8f9dc3905abe1ed4b8a52f4e1a4f50eb5fe5
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
|
@@ -61,7 +61,7 @@ User.from(user_hash) # => raise Serdes::TypeError
|
|
|
61
61
|
- `<class>.from(obj)`: Deserialize object to <class> instance.
|
|
62
62
|
- `from` will call `from_<obj.class>` method if it exists. if not, it returns obj as it is.
|
|
63
63
|
- `<class>#to_hash`: Serialize <class> instance to Hash.
|
|
64
|
-
- There is no support for
|
|
64
|
+
- There is no support for serialization, as only you need to do is just implement `to_<class>` method where you want.
|
|
65
65
|
|
|
66
66
|
### Types
|
|
67
67
|
|
|
@@ -72,10 +72,21 @@ User.from(user_hash) # => raise Serdes::TypeError
|
|
|
72
72
|
|
|
73
73
|
### Macro
|
|
74
74
|
|
|
75
|
+
#### Global macro
|
|
76
|
+
|
|
75
77
|
- `rename_all_attributes`: Rename all attributes when serializing and deserializing.
|
|
76
78
|
- Supported: `:snake_case`, `:PascalCase`
|
|
77
79
|
- `symbolize_all_keys`: Symbolize all keys when serializing and deserializing Hash, and vice versa.
|
|
78
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
|
+
|
|
79
90
|
## Development
|
|
80
91
|
|
|
81
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,25 @@ 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
|
+
|
|
111
|
+
# Validate default value type if provided
|
|
112
|
+
if @options.key?(:default)
|
|
113
|
+
default_value = @options[:default]
|
|
114
|
+
# Use permit? to validate both type and only constraint
|
|
115
|
+
# permit? will raise TypeError if only constraint is violated
|
|
116
|
+
unless @attr_type.permit?(default_value)
|
|
117
|
+
raise TypeError, "Wrong type for default value of #{class_name}##{name}. Expected type #{@attr_type}, got #{default_value.class} (val: '#{default_value}')."
|
|
118
|
+
end
|
|
119
|
+
# Check only constraint
|
|
120
|
+
if @options[:only] && !@options[:only].include?(default_value)
|
|
121
|
+
raise TypeError, "Wrong value for #{class_name}##{name}. Expected value is #{@options[:only]}, got '#{default_value}'."
|
|
122
|
+
end
|
|
123
|
+
end
|
|
105
124
|
end
|
|
106
125
|
|
|
107
126
|
def permit?(value)
|
|
@@ -183,9 +202,15 @@ module Serdes
|
|
|
183
202
|
key = attr.serialized_name(_serde_rename_strategy)
|
|
184
203
|
key = key.to_sym if _serde_symbolized_all_keys
|
|
185
204
|
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
205
|
+
if hash.key?(key)
|
|
206
|
+
serialized_value = hash[key]
|
|
207
|
+
value = Functions.from__proxy(attr.attr_type, serialized_value)
|
|
208
|
+
elsif attr.options.key?(:default)
|
|
209
|
+
value = attr.options[:default]
|
|
210
|
+
else
|
|
211
|
+
serialized_value = hash[key]
|
|
212
|
+
value = Functions.from__proxy(attr.attr_type, serialized_value)
|
|
213
|
+
end
|
|
189
214
|
|
|
190
215
|
instance.__send__("#{attr.name}=", value)
|
|
191
216
|
end
|
|
@@ -214,8 +239,13 @@ module Serdes
|
|
|
214
239
|
self.class.__send__(:_serde_attrs).each_with_object({}) do |(name, attr), hash|
|
|
215
240
|
key = attr.serialized_name(self.class.__send__(:_serde_rename_strategy))
|
|
216
241
|
key = key.to_sym if self.class.__send__(:_serde_symbolized_all_keys)
|
|
242
|
+
|
|
243
|
+
next if attr.options[:skip_serializing]
|
|
244
|
+
|
|
217
245
|
value = __send__(name)
|
|
218
246
|
|
|
247
|
+
next if attr.options[:skip_serializing_if] && attr.options[:skip_serializing_if].call(value)
|
|
248
|
+
|
|
219
249
|
hash[key] =
|
|
220
250
|
if value.respond_to?(:to_hash)
|
|
221
251
|
value.to_hash
|
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.5
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Shia
|
|
8
8
|
bindir: bin
|
|
9
9
|
cert_chain: []
|
|
10
|
-
date:
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
11
|
dependencies: []
|
|
12
12
|
description: Serdes is a tool for serializing and deserializing class.
|
|
13
13
|
email:
|
|
@@ -43,7 +43,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
43
43
|
- !ruby/object:Gem::Version
|
|
44
44
|
version: '0'
|
|
45
45
|
requirements: []
|
|
46
|
-
rubygems_version: 3.6.
|
|
46
|
+
rubygems_version: 3.6.9
|
|
47
47
|
specification_version: 4
|
|
48
48
|
summary: Serdes is a tool for serializing and deserializing class.
|
|
49
49
|
test_files: []
|