data_model 0.3.0 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +11 -2
- data/.shadowenv.d/.gitignore +2 -0
- data/.shadowenv.d/550-ruby.lisp +37 -0
- data/.solargraph.yml +22 -0
- data/Gemfile.lock +38 -3
- data/Rakefile +0 -6
- data/Steepfile +27 -0
- data/data_model.gemspec +2 -2
- data/lib/data_model/boolean.rb +0 -2
- data/lib/data_model/builtin/array.rb +32 -25
- data/lib/data_model/builtin/big_decimal.rb +15 -14
- data/lib/data_model/builtin/boolean.rb +10 -7
- data/lib/data_model/builtin/date.rb +15 -12
- data/lib/data_model/builtin/float.rb +14 -13
- data/lib/data_model/builtin/hash.rb +100 -35
- data/lib/data_model/builtin/integer.rb +14 -13
- data/lib/data_model/builtin/numeric.rb +35 -0
- data/lib/data_model/builtin/object.rb +28 -0
- data/lib/data_model/builtin/or.rb +73 -0
- data/lib/data_model/builtin/string.rb +15 -16
- data/lib/data_model/builtin/symbol.rb +14 -13
- data/lib/data_model/builtin/time.rb +17 -14
- data/lib/data_model/builtin.rb +9 -9
- data/lib/data_model/error.rb +33 -33
- data/lib/data_model/errors.rb +107 -143
- data/lib/data_model/fixtures/array.rb +22 -9
- data/lib/data_model/fixtures/big_decimal.rb +9 -7
- data/lib/data_model/fixtures/boolean.rb +5 -5
- data/lib/data_model/fixtures/date.rb +13 -11
- data/lib/data_model/fixtures/example.rb +7 -7
- data/lib/data_model/fixtures/float.rb +9 -7
- data/lib/data_model/fixtures/hash.rb +22 -10
- data/lib/data_model/fixtures/integer.rb +9 -7
- data/lib/data_model/fixtures/numeric.rb +31 -0
- data/lib/data_model/fixtures/object.rb +27 -0
- data/lib/data_model/fixtures/or.rb +29 -0
- data/lib/data_model/fixtures/string.rb +15 -32
- data/lib/data_model/fixtures/symbol.rb +9 -7
- data/lib/data_model/fixtures/time.rb +13 -11
- data/lib/data_model/logging.rb +5 -8
- data/lib/data_model/model.rb +11 -8
- data/lib/data_model/registry.rb +129 -0
- data/lib/data_model/scanner.rb +24 -29
- data/lib/data_model/struct.rb +112 -0
- data/lib/data_model/testing/minitest.rb +33 -9
- data/lib/data_model/testing.rb +0 -2
- data/lib/data_model/type.rb +39 -23
- data/lib/data_model/version.rb +1 -3
- data/lib/data_model.rb +10 -19
- metadata +24 -21
- data/lib/data_model/type_registry.rb +0 -68
- data/sorbet/config +0 -4
- data/sorbet/rbi/annotations/rainbow.rbi +0 -269
- data/sorbet/rbi/gems/minitest@5.18.0.rbi +0 -1491
- data/sorbet/rbi/gems/zeitwerk.rbi +0 -196
- data/sorbet/rbi/gems/zeitwerk@2.6.7.rbi +0 -966
- data/sorbet/rbi/todo.rbi +0 -5
- data/sorbet/tapioca/config.yml +0 -13
- data/sorbet/tapioca/require.rb +0 -4
data/lib/data_model/error.rb
CHANGED
@@ -1,33 +1,37 @@
|
|
1
|
-
# typed: strict
|
2
|
-
|
3
1
|
module DataModel
|
4
|
-
# Error is a class that holds errors.
|
2
|
+
# Error is a class that holds errors. Errors are a tuple of [name, ctx]
|
3
|
+
# - name is a symbol that identifies the error
|
4
|
+
# - ctx is contextual information about the error which can be used to build an error message
|
5
|
+
#
|
6
|
+
# The error object is a structured way to store, modify, and add errors in that intermediary format.
|
7
|
+
# To turn an error into a human readable message, use #to_messages, which delegates to a registry
|
8
|
+
#
|
9
|
+
# Base errors are errors that are related to the object as a whole, and not to any specific child
|
10
|
+
# Child errors are errors that are related to a specific child of the object, which may or may not apply depending on the type
|
5
11
|
class Error
|
6
|
-
|
7
|
-
|
8
|
-
TErrorList = T.type_alias { T::Array[TError] }
|
9
|
-
TErrorMap = T.type_alias { T::Hash[Symbol, TErrorList] }
|
12
|
+
include Errors
|
10
13
|
|
11
|
-
|
14
|
+
# Create a new error Object
|
15
|
+
# @return [Error] the new error object
|
12
16
|
def initialize
|
13
|
-
@base =
|
14
|
-
@children =
|
17
|
+
@base = []
|
18
|
+
@children = {}
|
15
19
|
end
|
16
20
|
|
17
21
|
# errors related to the object as a whole
|
18
|
-
|
22
|
+
# @return [Array<Array(Symbol, untyped)>] the base errors
|
19
23
|
def base
|
20
24
|
return @base
|
21
25
|
end
|
22
26
|
|
23
27
|
# errors related children
|
24
|
-
|
28
|
+
# @return [Hash{Symbol => Array<Array(Symbol, untyped)>}] the child errors
|
25
29
|
def children
|
26
30
|
return @children
|
27
31
|
end
|
28
32
|
|
29
33
|
# all errors
|
30
|
-
|
34
|
+
# @return [Hash{Symbol => Array<Array(Symbol, untyped)>}] all errors
|
31
35
|
def all
|
32
36
|
return children.merge(base:)
|
33
37
|
end
|
@@ -35,7 +39,8 @@ module DataModel
|
|
35
39
|
alias to_h all
|
36
40
|
|
37
41
|
# Returns true if any errors are present.
|
38
|
-
|
42
|
+
# @param blk [Proc] an optional block to filter errors, takes an Array(Symbol, untyped) and returns boolean
|
43
|
+
# @return [Boolean] true if any errors are present
|
39
44
|
def any?(&blk)
|
40
45
|
if !blk
|
41
46
|
return !@base.empty? || !@children.empty?
|
@@ -53,13 +58,16 @@ module DataModel
|
|
53
58
|
return any
|
54
59
|
end
|
55
60
|
|
56
|
-
|
61
|
+
# Returns true if no errors are present.
|
62
|
+
# @return [Boolean] true if no errors are present
|
57
63
|
def empty?
|
58
64
|
!any?
|
59
65
|
end
|
60
66
|
|
61
67
|
# Add an error to the error list.
|
62
|
-
|
68
|
+
# @param err [Array(Symbol, untyped)] the error to add
|
69
|
+
# @param child [Symbol, Array(Symbol)] the child to add the error to. child can be an array of symbols to specify a path if nested
|
70
|
+
# @return [void]
|
63
71
|
def add(err, child: nil)
|
64
72
|
if child.is_a?(Array)
|
65
73
|
child = child.join(".").to_sym
|
@@ -73,7 +81,10 @@ module DataModel
|
|
73
81
|
errs.push(err)
|
74
82
|
end
|
75
83
|
|
76
|
-
|
84
|
+
# Merge another error object into this one for child Errors
|
85
|
+
# @param name [Symbol] the name of the child
|
86
|
+
# @param child [Error] the child error object
|
87
|
+
# @return [void]
|
77
88
|
def merge_child(name, child)
|
78
89
|
if !child.any?
|
79
90
|
return
|
@@ -86,22 +97,11 @@ module DataModel
|
|
86
97
|
end
|
87
98
|
end
|
88
99
|
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
end
|
95
|
-
end
|
96
|
-
|
97
|
-
sig { params(blk: T.proc.params(context: Object, type: Symbol).returns(Object)).void }
|
98
|
-
def transform_child_context(&blk)
|
99
|
-
for error_list in @children.values
|
100
|
-
for error in error_list
|
101
|
-
key, context = error
|
102
|
-
error[1] = blk.call(context, key)
|
103
|
-
end
|
104
|
-
end
|
100
|
+
# Get human readable error messages from error tuples
|
101
|
+
# @param registry [Registry] the registry to use to get error messages
|
102
|
+
# @return [Hash{Symbol => Array[String]}] the error messages
|
103
|
+
def to_messages(registry: Registry.instance)
|
104
|
+
return registry.error_messages(self)
|
105
105
|
end
|
106
106
|
end
|
107
107
|
end
|
data/lib/data_model/errors.rb
CHANGED
@@ -1,83 +1,96 @@
|
|
1
|
-
# typed: strict
|
2
|
-
|
3
1
|
module DataModel
|
4
2
|
# Provide Error building functionality as a mixin
|
5
3
|
module Errors
|
6
|
-
|
7
|
-
extend T::Sig
|
8
|
-
|
9
|
-
TTemporal = T.type_alias { T.any(::Date, ::Time, ::DateTime) }
|
4
|
+
extend self
|
10
5
|
|
11
6
|
## Constructors
|
12
7
|
|
13
8
|
# Type error applies when a value is not of the expected type
|
14
|
-
|
9
|
+
# @param cls [String, Array(String)] the expected class
|
10
|
+
# @param value [Object] the value that failed
|
11
|
+
# @return [Array(Symbol, untyped)] the error
|
15
12
|
def type_error(cls, value)
|
16
13
|
[:type, [cls, value]]
|
17
14
|
end
|
18
15
|
|
19
16
|
# Coerce error applies when a value cannot be coerced to the expected type
|
20
|
-
|
17
|
+
# @param cls [String] the expected class
|
18
|
+
# @param value [Object] the value that failed
|
19
|
+
# @return [Array(Symbol, untyped)] the error
|
21
20
|
def coerce_error(cls, value)
|
22
21
|
[:coerce, [cls, value]]
|
23
22
|
end
|
24
23
|
|
25
24
|
# Missing error applies when a value is missing
|
26
|
-
|
25
|
+
# @param cls [String, Array<String>] the expected class
|
26
|
+
# @return [Array(Symbol, untyped)] the error
|
27
27
|
def missing_error(cls)
|
28
28
|
[:missing, cls]
|
29
29
|
end
|
30
30
|
|
31
31
|
# Inclusion error applies when a value is not in a set of allowed values
|
32
|
-
|
32
|
+
# @param set [Array<Symbol, String>] the set of allowed values
|
33
|
+
# @return [Array(Symbol, untyped)] the error
|
33
34
|
def inclusion_error(set)
|
34
35
|
[:inclusion, set]
|
35
36
|
end
|
36
37
|
|
37
38
|
# Exclusive error applies when a value is in a set of disallowed values
|
38
|
-
|
39
|
+
# @param set [Array<Symbol, String>] the set of disallowed values
|
40
|
+
# @return [Array(Symbol, untyped)] the error
|
39
41
|
def exclusion_error(set)
|
40
42
|
[:exclusion, set]
|
41
43
|
end
|
42
44
|
|
43
45
|
# Blank error applies when a value is blank
|
44
|
-
|
46
|
+
# @return [Array(Symbol, untyped)] the error
|
45
47
|
def blank_error
|
46
48
|
[:blank, nil]
|
47
49
|
end
|
48
50
|
|
49
51
|
# Extra keys error applies when a hash has extra keys
|
50
|
-
|
52
|
+
# @param keys [Array<Symbol>] the extra keys
|
53
|
+
# @return [Array(Symbol, untyped)] the error
|
51
54
|
def extra_keys_error(keys)
|
52
55
|
[:extra_keys, keys]
|
53
56
|
end
|
54
57
|
|
55
58
|
# Min applies when value is less then the minimum
|
56
|
-
|
59
|
+
# @param min [Numeric] the minimum value
|
60
|
+
# @param val [Numeric] the value that failed
|
61
|
+
# @return [Array(Symbol, untyped)] the error
|
57
62
|
def min_error(min, val)
|
58
63
|
[:min, [min, val]]
|
59
64
|
end
|
60
65
|
|
61
66
|
# Max applies when value is less then the minimum
|
62
|
-
|
67
|
+
# @param min [Numeric] the minimum value
|
68
|
+
# @param val [Numeric] the value that failed
|
69
|
+
# @return [Array(Symbol, untyped)] the error
|
63
70
|
def max_error(min, val)
|
64
71
|
[:max, [min, val]]
|
65
72
|
end
|
66
73
|
|
67
74
|
# Earliest applies when value is earlier then earliest
|
68
|
-
|
75
|
+
# @param earliest [Date, Time] the earliest value
|
76
|
+
# @param val [Date, Time] the value that failed
|
77
|
+
# @return [Array(Symbol, untyped)] the error
|
69
78
|
def earliest_error(earliest, val)
|
70
79
|
[:earliest, [earliest, val]]
|
71
80
|
end
|
72
81
|
|
73
82
|
# Latest applies when value is earlier then earliest
|
74
|
-
|
83
|
+
# @param latest [Date, Time] the latest value
|
84
|
+
# @param val [Date, Time] the value that failed
|
85
|
+
# @return [Array(Symbol, untyped)] the error
|
75
86
|
def latest_error(latest, val)
|
76
87
|
[:latest, [latest, val]]
|
77
88
|
end
|
78
89
|
|
79
90
|
# Format applies when value does not match a format
|
80
|
-
|
91
|
+
# @param format [Object] the format
|
92
|
+
# @param val [String] the value that failed
|
93
|
+
# @return [Array(Symbol, untyped)] the error
|
81
94
|
def format_error(format, val)
|
82
95
|
[:format, [format, val]]
|
83
96
|
end
|
@@ -85,212 +98,163 @@ module DataModel
|
|
85
98
|
## Messages
|
86
99
|
|
87
100
|
# Generate a message for a type error
|
88
|
-
|
101
|
+
# @param cls [String] the expected class
|
102
|
+
# @param value [Object] the value that failed
|
103
|
+
# @return [String] the message
|
89
104
|
def type_error_message(cls, value)
|
90
|
-
|
105
|
+
names = Array(cls).join(" or ")
|
106
|
+
"#{value.inspect} is not a #{names}, it is a #{value.class.name}"
|
91
107
|
end
|
92
108
|
|
93
109
|
# Generate a message for a coerce error
|
94
|
-
|
110
|
+
# @param cls [String] the expected class
|
111
|
+
# @param value [Object] the value that failed
|
112
|
+
# @return [String] the message
|
95
113
|
def coerce_error_message(cls, value)
|
96
|
-
|
114
|
+
names = Array(cls).join(" or ")
|
115
|
+
"cannot be coerced to #{names}, it is a #{value.class.name}"
|
97
116
|
end
|
98
117
|
|
99
118
|
# Generate a message for a missing error
|
100
|
-
|
119
|
+
# @param cls [String, Array<String>] the expected class
|
120
|
+
# @return [String] the message
|
101
121
|
def missing_error_message(cls)
|
102
|
-
|
122
|
+
names = Array(cls).join(" or ")
|
123
|
+
"missing value, expected a #{names}"
|
103
124
|
end
|
104
125
|
|
105
126
|
# Generate a message for an inclusion error
|
106
|
-
|
127
|
+
# @param set [Array<Symbol, String>] the set of allowed values
|
128
|
+
# @return [String] the message
|
107
129
|
def inclusion_error_message(set)
|
108
130
|
"must be one of #{set.join(', ')}"
|
109
131
|
end
|
110
132
|
|
111
133
|
# Generate a message for an exclusion error
|
112
|
-
|
134
|
+
# @param set [Array<Symbol, String>] the set of disallowed values
|
135
|
+
# @return [String] the message
|
113
136
|
def exclusion_error_message(set)
|
114
137
|
"must not be one of #{set.join(', ')}"
|
115
138
|
end
|
116
139
|
|
117
140
|
# Generate a message for a blank error
|
118
|
-
|
141
|
+
# @return [String] the message
|
119
142
|
def blank_error_message
|
120
143
|
"cannot be blank"
|
121
144
|
end
|
122
145
|
|
123
146
|
# Generate a message for an extra keys error
|
124
|
-
|
147
|
+
# @param keys [Array<Symbol>] the extra keys
|
148
|
+
# @return [String] the message
|
125
149
|
def extra_keys_error_message(keys)
|
126
150
|
"more elements found in closed hash then specified children: #{keys.join(', ')}"
|
127
151
|
end
|
128
152
|
|
129
153
|
# Generate a message for a min error
|
130
|
-
|
154
|
+
# @param min [Numeric] the minimum value
|
155
|
+
# @param val [Numeric] the value that failed
|
156
|
+
# @return [String] the message
|
131
157
|
def min_error_message(min, val)
|
132
158
|
"value is less than the minimum of #{min}, it is #{val}"
|
133
159
|
end
|
134
160
|
|
135
161
|
# Generate a message for a min error
|
136
|
-
|
162
|
+
# @param max [Numeric] the maximum value
|
163
|
+
# @param val [Numeric] the value that failed
|
164
|
+
# @return [String] the message
|
137
165
|
def max_error_message(max, val)
|
138
166
|
"value is more than the maximum of #{max}, it is #{val}"
|
139
167
|
end
|
140
168
|
|
141
169
|
# Generate a message for a value that occurs earlier then the specified earliest point
|
142
|
-
|
170
|
+
# @param earliest [Date, Time] the earliest value
|
171
|
+
# @param val [Date, Time] the value that failed
|
172
|
+
# @return [String] the message
|
143
173
|
def early_error_message(earliest, val)
|
144
174
|
"value #{val} is before #{earliest}"
|
145
175
|
end
|
146
176
|
|
147
177
|
# Generate a message for a value that occurs later then the specified latest point
|
148
|
-
|
178
|
+
# @param latest [Date, Time] the latest value
|
179
|
+
# @param val [Date, Time] the value that failed
|
180
|
+
# @return [String] the message
|
149
181
|
def late_error_message(latest, val)
|
150
182
|
"value #{val} is after #{latest}"
|
151
183
|
end
|
152
184
|
|
153
185
|
# Generate a message for a value that does not match the format
|
154
|
-
|
186
|
+
# @param format [Object] the format
|
187
|
+
# @param val [String] the value that failed
|
188
|
+
# @return [String] the message
|
155
189
|
def format_error_message(format, val)
|
156
190
|
"value #{val} does not match format #{format}"
|
157
191
|
end
|
158
192
|
|
159
|
-
|
160
|
-
# TODO: split this file
|
161
|
-
|
162
|
-
TErrorMessageBuilder = T.type_alias { T.proc.params(ctx: T.untyped).returns(String) }
|
163
|
-
|
164
|
-
# Register a custom error message for use with custom errors
|
165
|
-
sig { params(type: Symbol, block: TErrorMessageBuilder).void }
|
166
|
-
def register_error_message(type, &block)
|
167
|
-
error_message_builders[type] = block
|
168
|
-
end
|
169
|
-
|
170
|
-
TErrorMessages = T.type_alias { T::Hash[Symbol, TErrorMessageBuilder] }
|
171
|
-
TClassValueCtx = T.type_alias { [T.class_of(Object), Object] }
|
172
|
-
TClassCtx = T.type_alias { T.class_of(Object) }
|
173
|
-
TSetCtx = T.type_alias { T::Array[Symbol] }
|
174
|
-
TWithinCtx = T.type_alias { [Numeric, Numeric] }
|
175
|
-
TWithinTemporalCtx = T.type_alias { [TTemporal, TTemporal] }
|
176
|
-
TFormatCtx = T.type_alias { [Object, String] }
|
193
|
+
# Builders
|
177
194
|
|
178
195
|
# Get the error message builders
|
179
|
-
|
180
|
-
def
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
# wire up defaults
|
185
|
-
|
186
|
-
register_error_message(:type) do |ctx|
|
187
|
-
cls, val = T.let(ctx, TClassValueCtx)
|
196
|
+
# @return [Hash{Symbol => Proc}] the error message builders
|
197
|
+
def error_messages
|
198
|
+
return {
|
199
|
+
type: lambda do |ctx|
|
200
|
+
cls, val = ctx
|
188
201
|
type_error_message(cls, val)
|
189
|
-
end
|
202
|
+
end,
|
190
203
|
|
191
|
-
|
192
|
-
cls, val =
|
193
|
-
|
194
|
-
end
|
204
|
+
coerce: lambda do |ctx|
|
205
|
+
cls, val = ctx
|
206
|
+
type_error_message(cls, val)
|
207
|
+
end,
|
195
208
|
|
196
|
-
|
197
|
-
cls =
|
209
|
+
missing: lambda do |ctx|
|
210
|
+
cls = ctx
|
198
211
|
missing_error_message(cls)
|
199
|
-
end
|
212
|
+
end,
|
200
213
|
|
201
|
-
|
202
|
-
set =
|
214
|
+
inclusion: lambda do |ctx|
|
215
|
+
set = ctx
|
203
216
|
inclusion_error_message(set)
|
204
|
-
end
|
217
|
+
end,
|
205
218
|
|
206
|
-
|
207
|
-
set =
|
219
|
+
exclusion: lambda do |ctx|
|
220
|
+
set = ctx
|
208
221
|
exclusion_error_message(set)
|
209
|
-
end
|
222
|
+
end,
|
210
223
|
|
211
|
-
|
212
|
-
set =
|
224
|
+
extra_keys: lambda do |ctx|
|
225
|
+
set = ctx
|
213
226
|
extra_keys_error_message(set)
|
214
|
-
end
|
227
|
+
end,
|
215
228
|
|
216
|
-
|
217
|
-
min, val =
|
229
|
+
min: lambda do |ctx|
|
230
|
+
min, val = ctx
|
218
231
|
min_error_message(min, val)
|
219
|
-
end
|
232
|
+
end,
|
220
233
|
|
221
|
-
|
222
|
-
max, val =
|
234
|
+
max: lambda do |ctx|
|
235
|
+
max, val = ctx
|
223
236
|
max_error_message(max, val)
|
224
|
-
end
|
237
|
+
end,
|
225
238
|
|
226
|
-
|
227
|
-
earliest, val =
|
239
|
+
earliest: lambda do |ctx|
|
240
|
+
earliest, val = ctx
|
228
241
|
early_error_message(earliest, val)
|
229
|
-
end
|
242
|
+
end,
|
230
243
|
|
231
|
-
|
232
|
-
latest, val =
|
244
|
+
latest: lambda do |ctx|
|
245
|
+
latest, val = ctx
|
233
246
|
late_error_message(latest, val)
|
234
|
-
end
|
247
|
+
end,
|
235
248
|
|
236
|
-
|
249
|
+
blank: lambda do
|
237
250
|
blank_error_message
|
238
|
-
end
|
251
|
+
end,
|
239
252
|
|
240
|
-
|
241
|
-
format, val =
|
253
|
+
format: lambda do |ctx|
|
254
|
+
format, val = ctx
|
242
255
|
format_error_message(format, val)
|
243
256
|
end
|
244
|
-
|
245
|
-
|
246
|
-
@error_messages
|
247
|
-
end
|
248
|
-
|
249
|
-
# Build the error message for a given error
|
250
|
-
sig { params(error: TError).returns(String) }
|
251
|
-
def error_message(error)
|
252
|
-
type = T.let(error[0], Symbol)
|
253
|
-
ctx = T.let(error[1], T.untyped)
|
254
|
-
|
255
|
-
builder = error_message_builders[type]
|
256
|
-
|
257
|
-
if builder.nil?
|
258
|
-
raise "no error message builder for #{type}"
|
259
|
-
end
|
260
|
-
|
261
|
-
builder.call(ctx)
|
262
|
-
end
|
263
|
-
|
264
|
-
# TODO: separate builders from other use cases for this mixin
|
265
|
-
# Build error messages from error object
|
266
|
-
sig { params(error: Error).returns(T::Hash[Symbol, T::Array[String]]) }
|
267
|
-
def error_messages(error)
|
268
|
-
error.to_h.transform_values do |error_list|
|
269
|
-
error_list.map { |e| error_message(e) }
|
270
|
-
end
|
271
|
-
end
|
272
|
-
|
273
|
-
sig { params(error: Error, from: T.class_of(Object), to: T.class_of(Object)).void }
|
274
|
-
def set_error_class(error, from, to)
|
275
|
-
error.transform_context do |ctx, type|
|
276
|
-
case type
|
277
|
-
when :type, :coerce
|
278
|
-
cls, val = T.cast(ctx, TClassValueCtx)
|
279
|
-
if cls == from
|
280
|
-
[to, val]
|
281
|
-
else
|
282
|
-
[cls, val]
|
283
|
-
end
|
284
|
-
when :missing
|
285
|
-
if ctx == from
|
286
|
-
[to, val]
|
287
|
-
else
|
288
|
-
[cls, val]
|
289
|
-
end
|
290
|
-
else
|
291
|
-
[cls, val]
|
292
|
-
end
|
293
|
-
end
|
257
|
+
}
|
294
258
|
end
|
295
259
|
end
|
296
260
|
end
|
@@ -1,12 +1,22 @@
|
|
1
|
-
# typed: strict
|
2
|
-
|
3
1
|
module DataModel
|
2
|
+
# test fixtures for array type
|
4
3
|
module Fixtures::Array
|
5
4
|
extend self
|
6
|
-
extend T::Sig
|
7
5
|
include Fixtures
|
8
6
|
|
9
|
-
|
7
|
+
# a simple array of any types
|
8
|
+
# @return [Example] the example
|
9
|
+
def any_array
|
10
|
+
Example.new(
|
11
|
+
[:array],
|
12
|
+
variants: {
|
13
|
+
mixed: ["a", 2, [], ::Object.new]
|
14
|
+
},
|
15
|
+
)
|
16
|
+
end
|
17
|
+
|
18
|
+
# a simple array of strings example
|
19
|
+
# @return [Example] the example
|
10
20
|
def string_array
|
11
21
|
Example.new(
|
12
22
|
[:array, :string],
|
@@ -16,12 +26,13 @@ module DataModel
|
|
16
26
|
number: [1, ["1"]],
|
17
27
|
missing: nil,
|
18
28
|
numbers: [[1, 2, 3], ["1", "2", "3"]],
|
19
|
-
other_type: Object.new
|
29
|
+
other_type: ::Object.new
|
20
30
|
},
|
21
31
|
)
|
22
32
|
end
|
23
33
|
|
24
|
-
|
34
|
+
# a simple array of strings that wraps single values
|
35
|
+
# @return [Example] the example
|
25
36
|
def wrapping_string_array
|
26
37
|
Example.new(
|
27
38
|
[:array, { wrap_single_value: true }, :string],
|
@@ -31,12 +42,13 @@ module DataModel
|
|
31
42
|
number: [1, ["1"]],
|
32
43
|
missing: nil,
|
33
44
|
numbers: [[1, 2, 3], ["1", "2", "3"]],
|
34
|
-
other_type: Object.new
|
45
|
+
other_type: ::Object.new
|
35
46
|
},
|
36
47
|
)
|
37
48
|
end
|
38
49
|
|
39
|
-
|
50
|
+
# an optional example
|
51
|
+
# @return [Example] the example
|
40
52
|
def optional_string_array
|
41
53
|
Example.new(
|
42
54
|
[:array, { optional: true }, :string],
|
@@ -47,7 +59,8 @@ module DataModel
|
|
47
59
|
)
|
48
60
|
end
|
49
61
|
|
50
|
-
|
62
|
+
# an array of optional strings
|
63
|
+
# @return [Example] the example
|
51
64
|
def array_optional_string
|
52
65
|
Example.new(
|
53
66
|
[:array, [:string, { optional: true }]],
|
@@ -1,14 +1,13 @@
|
|
1
|
-
# typed: strict
|
2
|
-
|
3
1
|
require "bigdecimal/util"
|
4
2
|
|
5
3
|
module DataModel
|
4
|
+
# test fixtures for BigDecimal
|
6
5
|
module Fixtures::BigDecimal
|
7
6
|
include Fixtures
|
8
|
-
extend T::Sig
|
9
7
|
extend self
|
10
8
|
|
11
|
-
|
9
|
+
# a simple decimal example
|
10
|
+
# @return [Example] the example
|
12
11
|
def simple
|
13
12
|
Example.new(
|
14
13
|
[:decimal],
|
@@ -20,7 +19,8 @@ module DataModel
|
|
20
19
|
)
|
21
20
|
end
|
22
21
|
|
23
|
-
|
22
|
+
# a decimal example that is optional
|
23
|
+
# @return [Example] the example
|
24
24
|
def optional
|
25
25
|
Example.new(
|
26
26
|
[:decimal, { optional: true }],
|
@@ -30,7 +30,8 @@ module DataModel
|
|
30
30
|
)
|
31
31
|
end
|
32
32
|
|
33
|
-
|
33
|
+
# a decimal example that has a restriction on the minimum value
|
34
|
+
# @return [Example] the example
|
34
35
|
def min
|
35
36
|
Example.new(
|
36
37
|
[:decimal, { min: 5 }],
|
@@ -41,7 +42,8 @@ module DataModel
|
|
41
42
|
)
|
42
43
|
end
|
43
44
|
|
44
|
-
|
45
|
+
# a decimal example that has a restriction on the maximum value
|
46
|
+
# @return [Example] the example
|
45
47
|
def max
|
46
48
|
Example.new(
|
47
49
|
[:decimal, { max: 5 }],
|
@@ -1,12 +1,11 @@
|
|
1
|
-
# typed: strict
|
2
|
-
|
3
1
|
module DataModel
|
2
|
+
# test fixtures for boolean type
|
4
3
|
module Fixtures::Boolean
|
5
|
-
extend T::Sig
|
6
4
|
extend self
|
7
5
|
include Fixtures
|
8
6
|
|
9
|
-
|
7
|
+
# a simple boolean example
|
8
|
+
# @return [Example] the example
|
10
9
|
def simple
|
11
10
|
Example.new(
|
12
11
|
[:boolean],
|
@@ -19,7 +18,8 @@ module DataModel
|
|
19
18
|
)
|
20
19
|
end
|
21
20
|
|
22
|
-
|
21
|
+
# a boolean example that is optional
|
22
|
+
# @return [Example] the example
|
23
23
|
def optional
|
24
24
|
Example.new(
|
25
25
|
[:boolean, { optional: true }],
|