serdes 0.1.1 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8bf9df8e819833cb4f8681e04251c49eced97ab46908dcb0312566f90f02fa38
4
- data.tar.gz: baa10bbd6a6c1d580f2a4738f2d4ef7b5714d67ecb7429b9a4748ddcffa21e4e
3
+ metadata.gz: 00cf00141d9d3b1ed522f32261e653b73a7cd0dea98654974bc4e08844474c3b
4
+ data.tar.gz: ee2953c0dd44a798eb9ee2bb61e0b7652ed4f1a3d4d24ab3897159b12ef51fca
5
5
  SHA512:
6
- metadata.gz: 7d7b88eabfff7f7379290a4459f0335e7a832dd8959b46c52a18f60ba044feeacda46b95cd738618ffcba58c9981d7bd22efb65a3514d4fe8896b18052f1f0c0
7
- data.tar.gz: 0e534ceb8ee52f6314386cf0c9286a7fa0a6b2d6263c7cd7d6b5dd01d1327ac61a256570fd60d0a5472efe6a1ea64daac144c8f2261f189c551631c09ab5133f
6
+ metadata.gz: 354f6647e6323ccd23967429d07649da43ddcde185fcbe8a5f9503c4d319096f10cc43784d91da78a4734ec4f368e57e19b29fa59dc7a4fb52f110092e14763a
7
+ data.tar.gz: bbf2db71e91a585a89a06ccaff47fa050e688ec9af9048c1d042e60d4e9cfe671f6ca42deb396786de18495d5d76022d2555ecf8fe76e42734e3c20178d4f951
data/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.1.3] - 2025-02-12
4
+
5
+ - Add `only` option which adds value constraint.
6
+
7
+ ## [0.1.2] - 2025-02-09
8
+
9
+ - Fix `Boolean` type not working.
10
+
3
11
  ## [0.1.1] - 2025-02-05
4
12
 
5
13
  - Added `symbolized_all_keys` macro.
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 = {
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Serdes
4
- VERSION = "0.1.1"
4
+ VERSION = "0.1.3"
5
5
  end
data/lib/serdes.rb CHANGED
@@ -30,7 +30,11 @@ module Serdes
30
30
  end
31
31
 
32
32
  def permit?(value)
33
- @exact_type == value.class
33
+ if @exact_type == Boolean
34
+ value == true || value == false
35
+ else
36
+ @exact_type == value.class
37
+ end
34
38
  end
35
39
 
36
40
  def to_s
@@ -100,6 +104,18 @@ module Serdes
100
104
  @options = options
101
105
  end
102
106
 
107
+ def permit?(value)
108
+ return false if !attr_type.permit?(value)
109
+
110
+ return true if @options[:only].nil?
111
+
112
+ if @options[:only].include?(value)
113
+ true
114
+ else
115
+ raise TypeError, "Wrong value for #{class_name}##{name}. Expected value is #{@options[:only]}, got '#{value}'."
116
+ end
117
+ end
118
+
103
119
  def serialized_name(rename_strategy = nil)
104
120
  if rename_strategy && rename_strategy != :snake_case
105
121
  Functions._serde_rename_strategy_func_fetch(:snake_case, rename_strategy).call(@name)
@@ -262,27 +278,11 @@ module Serdes
262
278
  end
263
279
 
264
280
  def validate_type!(attr, value)
265
- valid = attr.attr_type.permit?(value)
266
- return if valid
267
-
268
- actual_type = humanize_type(value)
281
+ return if attr.permit?(value)
269
282
 
270
283
  raise TypeError, "Wrong type for #{attr.class_name}##{attr.name}. Expected type #{attr.attr_type}, got #{value.class} (val: '#{value}')."
271
284
  end
272
285
 
273
- def humanize_type(value)
274
- if value.is_a?(Array)
275
- "array(" + value.map { |el| humanize_type(el) }.join(", ") + ")"
276
- else
277
- case value
278
- when NilClass then "nil"
279
- when TrueClass then "boolean"
280
- when FalseClass then "boolean"
281
- else value.class.to_s
282
- end
283
- end
284
- end
285
-
286
286
  def skip_to_hash?(value)
287
287
  case value
288
288
  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.1
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shia
8
8
  bindir: bin
9
9
  cert_chain: []
10
- date: 2025-02-05 00:00:00.000000000 Z
10
+ date: 2025-02-12 00:00:00.000000000 Z
11
11
  dependencies: []
12
12
  description: Serdes is a tool for serializing and deserializing class.
13
13
  email: