serdes 0.1.4 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bc5a0f39e261348c4f4d222f795e09de9dc6f5166d070214b0ffc48ab5615da6
4
- data.tar.gz: f0c40e4eb6fec6227754dedb5ab984a87a35e9bbdba1f8dd0e72e77e3a7145fe
3
+ metadata.gz: 28edbdb79c30f1f680e78f89b179372e2c947f755e192b4fb4e4e1fc1de88815
4
+ data.tar.gz: 61b27b8fa680feefe6d90e217cb26f729c36dcdb4c3703ab2b788824af6ffce1
5
5
  SHA512:
6
- metadata.gz: 5607b8aac8a10e6251c530cf39447dd5a9da5754b30b96b5d52f9e87db899085561c2533891183d2d9c3b8137fb22cc29efd69f137c1c19eca569b9fc8f3fe62
7
- data.tar.gz: bd25c27df789a9361d9fb8f35cb34dfc14d6181e763682f1af3ed2a18ba52e01aaf482ccdc1b7309b83ae26d404689ea95857b49809d034b28bec90a1c1f7464
6
+ metadata.gz: 57776409c4ebc410ec94d3a07723f280fa029d3f5950dcf132f220e153406cd1eaef2e717249bc691aebf8252de087f051844b27ee974582def946b859d29949
7
+ data.tar.gz: 94a768e64bb467d8acc7fa88184dff9e576d0dfb65600e0f5e95eef2f8c3e58c294d79c4e3ecd3c0a8527bf2c51f8f9dc3905abe1ed4b8a52f4e1a4f50eb5fe5
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.1.5]
4
+
5
+ - Add `default` option for attribute definitions.
6
+
3
7
  ## [0.1.4]
4
8
 
5
9
  - Add skip serializing attribute feature.
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 serializaion, as only you need to do is just implement `to_<class>` method where you want.
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
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Serdes
4
- VERSION = "0.1.4"
4
+ VERSION = "0.1.5"
5
5
  end
data/lib/serdes.rb CHANGED
@@ -107,6 +107,20 @@ module Serdes
107
107
  @options.delete(:skip_serializing_if_nil)
108
108
  @options[:skip_serializing_if] = ->(v) { v.nil? }
109
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
110
124
  end
111
125
 
112
126
  def permit?(value)
@@ -188,9 +202,15 @@ module Serdes
188
202
  key = attr.serialized_name(_serde_rename_strategy)
189
203
  key = key.to_sym if _serde_symbolized_all_keys
190
204
 
191
- serialized_value = hash[key]
192
-
193
- value = Functions.from__proxy(attr.attr_type, serialized_value)
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
194
214
 
195
215
  instance.__send__("#{attr.name}=", value)
196
216
  end
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
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: 2025-03-02 00:00:00.000000000 Z
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.2
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: []