vident 0.5.0 → 0.5.1

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: ba2054320ceb13a2646f5f88f4dcef219ad4a5482118f1245472b07d8848f313
4
- data.tar.gz: 7e0af03ee04bc52c6b7f35cd0da6490116428581bae587249380df7c71c8d863
3
+ metadata.gz: a81f442775cc3413603ea1770059483978615f37809502245cca2f1ac8ddd868
4
+ data.tar.gz: 37ce0a04d8a8e37cfdc10f5b5174f771e93ecb2c8dd8909eb58ef842866d0ae2
5
5
  SHA512:
6
- metadata.gz: 8cd4c1933065215f12d8640a4274164ddb869f9b013891f6ef569293b84dbcafea761c8442ebee81c0656c3c2d0e560f7d7ee4a36b5390cba2bc2148d5bc62b0
7
- data.tar.gz: bd3549f2eaf0157bea9b8489c2fc9ea83f71d11afa998b6a013ef65ea86ff4f41cbb39caafb94b4ea5049c00b36e3be3a3b67c3cb9ec24020f842cda85476523
6
+ metadata.gz: ec38a1d6550386705dd6de2374d650ee890c147c9cf97cca299b01bf27616f2becccdebe273efa51153ad74ace43faef8006464a5a2418e9991ca3642fdb8dca
7
+ data.tar.gz: a1b01b01507a70e276ec14b6a358c520f1bc11aca8c713fe32bb5a0091230caf9fd38e8f4ef22beed50ed8a1f72c0bb48c40cd5e10cdfaa546dafe80780af337
@@ -61,9 +61,11 @@ if Gem.loaded_specs.has_key? "dry-struct"
61
61
  attr_reader :schema, :attribute_ivar_names
62
62
 
63
63
  def attribute(name, signature = :any, **options, &converter)
64
- type_info = map_primitive_to_dry_type(signature, options, converter)
64
+ strict = !options[:convert]
65
+ signatures = extract_member_type_and_subclass(signature, options)
66
+ type_info = map_primitive_to_dry_type(signatures, strict, converter)
65
67
  type_info = set_constraints(type_info, options)
66
- type_info = set_metadata(type_info, signature, options)
68
+ type_info = set_metadata(type_info, signatures, options)
67
69
  define_on_schema(name, type_info, options)
68
70
  end
69
71
 
@@ -87,8 +89,8 @@ if Gem.loaded_specs.has_key? "dry-struct"
87
89
  type_info
88
90
  end
89
91
 
90
- def set_metadata(type_info, specified_type, options)
91
- metadata = {typed_attribute_type: specified_type, typed_attribute_options: options}
92
+ def set_metadata(type_info, signatures, options)
93
+ metadata = {typed_attribute_type: signatures, typed_attribute_options: options}
92
94
  type_info.meta(**metadata)
93
95
  end
94
96
 
@@ -100,7 +102,7 @@ if Gem.loaded_specs.has_key? "dry-struct"
100
102
  @attribute_ivar_names ||= {}
101
103
  @attribute_ivar_names[attribute_name] = :"@#{attribute_name}"
102
104
  define_attribute_delegate(attribute_name) if delegates?(options)
103
- @schema ||= const_set("TypedSchema", Class.new(Vident::Attributes::TypedNilingStruct))
105
+ @schema ||= const_set(:TypedSchema, Class.new(Vident::Attributes::TypedNilingStruct))
104
106
  @schema.attribute attribute_name, type_info
105
107
  end
106
108
 
@@ -130,9 +132,7 @@ if Gem.loaded_specs.has_key? "dry-struct"
130
132
  allow_blank.nil? ? true : allow_blank
131
133
  end
132
134
 
133
- def map_primitive_to_dry_type(signature, options, converter)
134
- strict = !options[:convert]
135
- signatures = extract_member_type_and_subclass(signature, options)
135
+ def map_primitive_to_dry_type(signatures, strict, converter)
136
136
  types = signatures.map do |type, subtype|
137
137
  dry_type = dry_type_from_primary_type(type, strict, converter)
138
138
  if subtype && dry_type.respond_to?(:of)
@@ -158,6 +158,19 @@ if Gem.loaded_specs.has_key? "dry-struct"
158
158
  end
159
159
 
160
160
  def dry_type_from_primary_type(type, strict, converter)
161
+ # If a converter is provided, we should use it to coerce the value
162
+ if converter && !strict && !type.is_a?(Symbol)
163
+ return Types.Constructor(type) do |value|
164
+ next value if value.is_a?(type)
165
+
166
+ converter.call(value).tap do |new_value|
167
+ unless new_value.is_a?(type)
168
+ raise ArgumentError, "Type conversion proc did not convert #{value} to #{type}"
169
+ end
170
+ end
171
+ end
172
+ end
173
+
161
174
  if type == :any
162
175
  Types::Nominal::Any
163
176
  elsif type == Integer
@@ -191,15 +204,11 @@ if Gem.loaded_specs.has_key? "dry-struct"
191
204
  # values using the default constructor, `new`.
192
205
  Types.Instance(type)
193
206
  else
194
- # dry calls this when initialising the Type. Check if type of input is correct or coerce
207
+ # dry calls this when initialising the Type. Check if type of input is correct or create new instance
195
208
  Types.Constructor(type) do |value|
196
209
  next value if value.is_a?(type)
197
210
 
198
- if converter
199
- converter.call(value)
200
- else
201
- type.new(**value)
202
- end
211
+ type.new(**value)
203
212
  end
204
213
  end
205
214
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Vident
4
- VERSION = "0.5.0"
4
+ VERSION = "0.5.1"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vident
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stephen Ierodiaconou
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-02-04 00:00:00.000000000 Z
11
+ date: 2023-02-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport