serdes 0.1.2 → 0.1.3

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: d3fda61b6d5f8b491822d777eb171a34ccaa130e001634a4328a81113def4183
4
- data.tar.gz: 6151fcc98e9ed477fde75a49f30df2b26d74ea14722a3ca1705e73a4e7bd84bb
3
+ metadata.gz: 00cf00141d9d3b1ed522f32261e653b73a7cd0dea98654974bc4e08844474c3b
4
+ data.tar.gz: ee2953c0dd44a798eb9ee2bb61e0b7652ed4f1a3d4d24ab3897159b12ef51fca
5
5
  SHA512:
6
- metadata.gz: dfb5ef4378c8c2ac18fad467e76c26aea7d6a67709ecf9ff206ce378c08ebaebba1dbe384c4bacd342c7f2f550a6ce35b44d67cdedc30fc4ef47b5ae0a03b255
7
- data.tar.gz: 1a90bbb57045ecccdb0e7282d5ce29e082917cf64033bb7f3b46a286c492d86914657f8b9459d3fe0ff40a7478ebf38ea2ee4e8879e35e86bb6c958dfc5e4be1
6
+ metadata.gz: 354f6647e6323ccd23967429d07649da43ddcde185fcbe8a5f9503c4d319096f10cc43784d91da78a4734ec4f368e57e19b29fa59dc7a4fb52f110092e14763a
7
+ data.tar.gz: bbf2db71e91a585a89a06ccaff47fa050e688ec9af9048c1d042e60d4e9cfe671f6ca42deb396786de18495d5d76022d2555ecf8fe76e42734e3c20178d4f951
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.1.3] - 2025-02-12
4
+
5
+ - Add `only` option which adds value constraint.
6
+
3
7
  ## [0.1.2] - 2025-02-09
4
8
 
5
9
  - Fix `Boolean` type not working.
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.2"
4
+ VERSION = "0.1.3"
5
5
  end
data/lib/serdes.rb CHANGED
@@ -104,6 +104,18 @@ module Serdes
104
104
  @options = options
105
105
  end
106
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
+
107
119
  def serialized_name(rename_strategy = nil)
108
120
  if rename_strategy && rename_strategy != :snake_case
109
121
  Functions._serde_rename_strategy_func_fetch(:snake_case, rename_strategy).call(@name)
@@ -266,27 +278,11 @@ module Serdes
266
278
  end
267
279
 
268
280
  def validate_type!(attr, value)
269
- valid = attr.attr_type.permit?(value)
270
- return if valid
271
-
272
- actual_type = humanize_type(value)
281
+ return if attr.permit?(value)
273
282
 
274
283
  raise TypeError, "Wrong type for #{attr.class_name}##{attr.name}. Expected type #{attr.attr_type}, got #{value.class} (val: '#{value}')."
275
284
  end
276
285
 
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
286
  def skip_to_hash?(value)
291
287
  case value
292
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.2
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-09 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: