vident 0.3.1 → 0.4.0

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: 24963e09fb8ecd09d22f1254eecc8f2d8bf255123e92976f12d224531003b42a
4
- data.tar.gz: 3b280628d9aa0ee62ad7b92b5ea58e628d3817e74567cfafb4ef22eadedd707e
3
+ metadata.gz: d1ead61d39ae1b70058a4002d6b3ed7ad4baade0d03ccaa6f2501ba20ac028b5
4
+ data.tar.gz: bbebb2b863ee0e1b734dc50c6a03015e0375a28a43004f9f4954c681acd954ca
5
5
  SHA512:
6
- metadata.gz: fe03e658c1d0ca0e537b66cf465c9628566507053aaf7fbd2d03125a98c1f5f829a1a7c51fb2751314aa38648f3119337ebfa2df932d462dd4e2ab31a235b730
7
- data.tar.gz: 100fb3cbe9fce6cb5083ebd95237796f57d73f44a02385e274d0e433763c0037cbad8d257994aa059e5ab6e137fb781309ea24f2cf277e22ec59f79d3f370343
6
+ metadata.gz: 1ac922f2933bd655a2aa69ca5fbeb4bb05bb6ac8277a7bb0c7e6a85c6329aebe0dcc63b0fcdf5a03c29b775348044c536d9f460c6006a43b5190418cc47d23f2
7
+ data.tar.gz: 26f4db0079c735b6df0fa26aca20e96a564e24d07dca71b6a1396ea95a86993ef8a99209df6bdfd1be618dfa8f6933281e1506318ad2fb592ce42e47b0456ba7
@@ -13,6 +13,8 @@ if Gem.loaded_specs.has_key? "dry-struct"
13
13
  module Typed
14
14
  extend ActiveSupport::Concern
15
15
 
16
+ # TODO: better handling of when either class.schema is undefined (as no attributes configured) or when
17
+ # other methods ar called before prepare_attributes is called
16
18
  def prepare_attributes(attributes)
17
19
  @__attributes = self.class.schema.new(**attributes)
18
20
  end
@@ -58,32 +60,34 @@ if Gem.loaded_specs.has_key? "dry-struct"
58
60
 
59
61
  attr_reader :schema, :attribute_ivar_names
60
62
 
61
- def attribute(name, type = :any, **options)
62
- type_info = map_primitive_to_dry_type(type, !options[:convert])
63
- type_info = set_constraints(type_info, type, options)
63
+ def attribute(name, signature = :any, **options, &converter)
64
+ type_info = map_primitive_to_dry_type(signature, options, converter)
65
+ type_info = set_constraints(type_info, options)
66
+ type_info = set_metadata(type_info, signature, options)
64
67
  define_on_schema(name, type_info, options)
65
68
  end
66
69
 
67
70
  private
68
71
 
69
- def set_constraints(type_info, specified_type, options)
70
- member_klass = options[:type] || options[:sub_type]
71
- if member_klass && type_info.respond_to?(:of)
72
- # Sub types of collections currently can be nil - this should be an option
73
- type_info = type_info.of(
74
- map_primitive_to_dry_type(member_klass, !options[:convert]).optional.meta(required: false)
75
- )
72
+ def set_constraints(type_info, options)
73
+ if allows_nil?(options)
74
+ type_info = type_info.optional.meta(required: false)
75
+ end
76
+ unless allows_blank?(options)
77
+ type_info = type_info.constrained(filled: true)
76
78
  end
77
- type_info = type_info.optional.meta(required: false) if allows_nil?(options)
78
- type_info = type_info.constrained(filled: true) unless allows_blank?(options)
79
79
  if options[:default]&.is_a?(Proc)
80
80
  type_info = type_info.default(options[:default].freeze)
81
81
  elsif !options[:default].nil?
82
82
  type_info = type_info.default(->(_) { options[:default] }.freeze)
83
83
  end
84
- type_info = type_info.constrained(included_in: options[:in].freeze) if options[:in]
84
+ if options[:in]
85
+ type_info.constrained(included_in: options[:in].freeze)
86
+ end
87
+ type_info
88
+ end
85
89
 
86
- # Store adapter type info in the schema for use by typed form
90
+ def set_metadata(type_info, specified_type, options)
87
91
  metadata = {typed_attribute_type: specified_type, typed_attribute_options: options}
88
92
  type_info.meta(**metadata)
89
93
  end
@@ -106,7 +110,7 @@ if Gem.loaded_specs.has_key? "dry-struct"
106
110
  def #{attr_name}
107
111
  @__attributes.attributes[:#{attr_name}]
108
112
  end
109
-
113
+
110
114
  def #{attr_name}?
111
115
  send(:#{attr_name}).present?
112
116
  end
@@ -126,7 +130,29 @@ if Gem.loaded_specs.has_key? "dry-struct"
126
130
  allow_blank.nil? ? true : allow_blank
127
131
  end
128
132
 
129
- def map_primitive_to_dry_type(type, strict)
133
+ def map_primitive_to_dry_type(signature, options, converter)
134
+ strict = !options[:convert]
135
+ type, subtype = extract_member_type_and_subclass(signature, options)
136
+ dry_type = dry_type_from_primary_type(type, strict, converter)
137
+ if subtype && dry_type.respond_to?(:of)
138
+ # Sub types of collections currently can be nil - this should be an option
139
+ dry_type.of(
140
+ map_primitive_to_dry_type(subtype, options, converter).optional.meta(required: false)
141
+ )
142
+ else
143
+ dry_type
144
+ end
145
+ end
146
+
147
+ def extract_member_type_and_subclass(signature, options)
148
+ if signature.is_a?(Array)
149
+ [Array, signature.first]
150
+ else
151
+ [signature, options[:type] || options[:sub_type]]
152
+ end
153
+ end
154
+
155
+ def dry_type_from_primary_type(type, strict, converter)
130
156
  if type == :any
131
157
  Types::Nominal::Any
132
158
  elsif type == Integer
@@ -160,7 +186,7 @@ if Gem.loaded_specs.has_key? "dry-struct"
160
186
  # values using the default constructor, `new`.
161
187
  Types.Instance(type)
162
188
  else
163
- Types.Constructor(type) { |values| type.new(**values) }
189
+ Types.Constructor(type) { |values| converter ? converter.call(values) : type.new(**values) }
164
190
  end
165
191
  end
166
192
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Vident
4
- VERSION = "0.3.1"
4
+ VERSION = "0.4.0"
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.3.1
4
+ version: 0.4.0
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-01-11 00:00:00.000000000 Z
11
+ date: 2023-01-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport