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 +4 -4
- data/lib/vident/attributes/typed.rb +43 -17
- data/lib/vident/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d1ead61d39ae1b70058a4002d6b3ed7ad4baade0d03ccaa6f2501ba20ac028b5
|
4
|
+
data.tar.gz: bbebb2b863ee0e1b734dc50c6a03015e0375a28a43004f9f4954c681acd954ca
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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,
|
62
|
-
type_info = map_primitive_to_dry_type(
|
63
|
-
type_info = set_constraints(type_info,
|
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,
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
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
|
-
|
84
|
+
if options[:in]
|
85
|
+
type_info.constrained(included_in: options[:in].freeze)
|
86
|
+
end
|
87
|
+
type_info
|
88
|
+
end
|
85
89
|
|
86
|
-
|
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(
|
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
|
data/lib/vident/version.rb
CHANGED
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.
|
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
|
+
date: 2023-01-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|