Module: DataModel::Errors

Overview

Provide Error building functionality as a mixin

Instance Method Summary collapse

Instance Method Details

#blank_errorArray(Symbol, untyped)

Blank error applies when a value is blank

Returns:

  • (Array(Symbol, untyped))

    the error



47
48
49
# File 'lib/data_model/errors.rb', line 47

def blank_error
	[:blank, nil]
end

#blank_error_messageString

Generate a message for a blank error

Returns:

  • (String)

    the message



142
143
144
# File 'lib/data_model/errors.rb', line 142

def blank_error_message
	"cannot be blank"
end

#coerce_error(cls, value) ⇒ Array(Symbol, untyped)

Coerce error applies when a value cannot be coerced to the expected type

Parameters:

  • cls (String)

    the expected class

  • value (Object)

    the value that failed

Returns:

  • (Array(Symbol, untyped))

    the error



20
21
22
# File 'lib/data_model/errors.rb', line 20

def coerce_error(cls, value)
	[:coerce, [cls, value]]
end

#coerce_error_message(cls, value) ⇒ String

Generate a message for a coerce error

Parameters:

  • cls (String)

    the expected class

  • value (Object)

    the value that failed

Returns:

  • (String)

    the message



113
114
115
116
# File 'lib/data_model/errors.rb', line 113

def coerce_error_message(cls, value)
	names = Array(cls).join(" or ")
	"cannot be coerced to #{names}, it is a #{value.class.name}"
end

#earliest_error(earliest, val) ⇒ Array(Symbol, untyped)

Earliest applies when value is earlier then earliest

Parameters:

  • earliest (Date, Time)

    the earliest value

  • val (Date, Time)

    the value that failed

Returns:

  • (Array(Symbol, untyped))

    the error



78
79
80
# File 'lib/data_model/errors.rb', line 78

def earliest_error(earliest, val)
	[:earliest, [earliest, val]]
end

#early_error_message(earliest, val) ⇒ String

Generate a message for a value that occurs earlier then the specified earliest point

Parameters:

  • earliest (Date, Time)

    the earliest value

  • val (Date, Time)

    the value that failed

Returns:

  • (String)

    the message



173
174
175
# File 'lib/data_model/errors.rb', line 173

def early_error_message(earliest, val)
	"value #{val} is before #{earliest}"
end

#error_messagesHash{Symbol => Proc}

Get the error message builders

Returns:

  • (Hash{Symbol => Proc})

    the error message builders



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/data_model/errors.rb', line 197

def error_messages
	return {
		type: lambda do |ctx|
			cls, val = ctx
			type_error_message(cls, val)
		end,

		coerce: lambda do |ctx|
			cls, val = ctx
			type_error_message(cls, val)
		end,

		missing: lambda do |ctx|
			cls = ctx
			missing_error_message(cls)
		end,

		inclusion: lambda do |ctx|
			set = ctx
			inclusion_error_message(set)
		end,

		exclusion: lambda do |ctx|
			set = ctx
			exclusion_error_message(set)
		end,

		extra_keys: lambda do |ctx|
			set = ctx
			extra_keys_error_message(set)
		end,

		min: lambda do |ctx|
			min, val = ctx
			min_error_message(min, val)
		end,

		max: lambda do |ctx|
			max, val = ctx
			max_error_message(max, val)
		end,

		earliest: lambda do |ctx|
			earliest, val = ctx
			early_error_message(earliest, val)
		end,

		latest: lambda do |ctx|
			latest, val = ctx
			late_error_message(latest, val)
		end,

		blank: lambda do
			blank_error_message
		end,

		format: lambda do |ctx|
			format, val = ctx
			format_error_message(format, val)
		end
	}
end

#exclusion_error(set) ⇒ Array(Symbol, untyped)

Exclusive error applies when a value is in a set of disallowed values

Parameters:

  • set (Array<Symbol, String>)

    the set of disallowed values

Returns:

  • (Array(Symbol, untyped))

    the error



41
42
43
# File 'lib/data_model/errors.rb', line 41

def exclusion_error(set)
	[:exclusion, set]
end

#exclusion_error_message(set) ⇒ String

Generate a message for an exclusion error

Parameters:

  • set (Array<Symbol, String>)

    the set of disallowed values

Returns:

  • (String)

    the message



136
137
138
# File 'lib/data_model/errors.rb', line 136

def exclusion_error_message(set)
	"must not be one of #{set.join(', ')}"
end

#extra_keys_error(keys) ⇒ Array(Symbol, untyped)

Extra keys error applies when a hash has extra keys

Parameters:

  • keys (Array<Symbol>)

    the extra keys

Returns:

  • (Array(Symbol, untyped))

    the error



54
55
56
# File 'lib/data_model/errors.rb', line 54

def extra_keys_error(keys)
	[:extra_keys, keys]
end

#extra_keys_error_message(keys) ⇒ String

Generate a message for an extra keys error

Parameters:

  • keys (Array<Symbol>)

    the extra keys

Returns:

  • (String)

    the message



149
150
151
# File 'lib/data_model/errors.rb', line 149

def extra_keys_error_message(keys)
	"more elements found in closed hash then specified children: #{keys.join(', ')}"
end

#format_error(format, val) ⇒ Array(Symbol, untyped)

Format applies when value does not match a format

Parameters:

  • format (Object)

    the format

  • val (String)

    the value that failed

Returns:

  • (Array(Symbol, untyped))

    the error



94
95
96
# File 'lib/data_model/errors.rb', line 94

def format_error(format, val)
	[:format, [format, val]]
end

#format_error_message(format, val) ⇒ String

Generate a message for a value that does not match the format

Parameters:

  • format (Object)

    the format

  • val (String)

    the value that failed

Returns:

  • (String)

    the message



189
190
191
# File 'lib/data_model/errors.rb', line 189

def format_error_message(format, val)
	"value #{val} does not match format #{format}"
end

#inclusion_error(set) ⇒ Array(Symbol, untyped)

Inclusion error applies when a value is not in a set of allowed values

Parameters:

  • set (Array<Symbol, String>)

    the set of allowed values

Returns:

  • (Array(Symbol, untyped))

    the error



34
35
36
# File 'lib/data_model/errors.rb', line 34

def inclusion_error(set)
	[:inclusion, set]
end

#inclusion_error_message(set) ⇒ String

Generate a message for an inclusion error

Parameters:

  • set (Array<Symbol, String>)

    the set of allowed values

Returns:

  • (String)

    the message



129
130
131
# File 'lib/data_model/errors.rb', line 129

def inclusion_error_message(set)
	"must be one of #{set.join(', ')}"
end

#late_error_message(latest, val) ⇒ String

Generate a message for a value that occurs later then the specified latest point

Parameters:

  • latest (Date, Time)

    the latest value

  • val (Date, Time)

    the value that failed

Returns:

  • (String)

    the message



181
182
183
# File 'lib/data_model/errors.rb', line 181

def late_error_message(latest, val)
	"value #{val} is after #{latest}"
end

#latest_error(latest, val) ⇒ Array(Symbol, untyped)

Latest applies when value is earlier then earliest

Parameters:

  • latest (Date, Time)

    the latest value

  • val (Date, Time)

    the value that failed

Returns:

  • (Array(Symbol, untyped))

    the error



86
87
88
# File 'lib/data_model/errors.rb', line 86

def latest_error(latest, val)
	[:latest, [latest, val]]
end

#max_error(min, val) ⇒ Array(Symbol, untyped)

Max applies when value is less then the minimum

Parameters:

  • min (Numeric)

    the minimum value

  • val (Numeric)

    the value that failed

Returns:

  • (Array(Symbol, untyped))

    the error



70
71
72
# File 'lib/data_model/errors.rb', line 70

def max_error(min, val)
	[:max, [min, val]]
end

#max_error_message(max, val) ⇒ String

Generate a message for a min error

Parameters:

  • max (Numeric)

    the maximum value

  • val (Numeric)

    the value that failed

Returns:

  • (String)

    the message



165
166
167
# File 'lib/data_model/errors.rb', line 165

def max_error_message(max, val)
	"value is more than the maximum of #{max}, it is #{val}"
end

#min_error(min, val) ⇒ Array(Symbol, untyped)

Min applies when value is less then the minimum

Parameters:

  • min (Numeric)

    the minimum value

  • val (Numeric)

    the value that failed

Returns:

  • (Array(Symbol, untyped))

    the error



62
63
64
# File 'lib/data_model/errors.rb', line 62

def min_error(min, val)
	[:min, [min, val]]
end

#min_error_message(min, val) ⇒ String

Generate a message for a min error

Parameters:

  • min (Numeric)

    the minimum value

  • val (Numeric)

    the value that failed

Returns:

  • (String)

    the message



157
158
159
# File 'lib/data_model/errors.rb', line 157

def min_error_message(min, val)
	"value is less than the minimum of #{min}, it is #{val}"
end

#missing_error(cls) ⇒ Array(Symbol, untyped)

Missing error applies when a value is missing

Parameters:

  • cls (String, Array<String>)

    the expected class

Returns:

  • (Array(Symbol, untyped))

    the error



27
28
29
# File 'lib/data_model/errors.rb', line 27

def missing_error(cls)
	[:missing, cls]
end

#missing_error_message(cls) ⇒ String

Generate a message for a missing error

Parameters:

  • cls (String, Array<String>)

    the expected class

Returns:

  • (String)

    the message



121
122
123
124
# File 'lib/data_model/errors.rb', line 121

def missing_error_message(cls)
	names = Array(cls).join(" or ")
	"missing value, expected a #{names}"
end

#type_error(cls, value) ⇒ Array(Symbol, untyped)

Type error applies when a value is not of the expected type

Parameters:

  • cls (String, Array(String))

    the expected class

  • value (Object)

    the value that failed

Returns:

  • (Array(Symbol, untyped))

    the error



12
13
14
# File 'lib/data_model/errors.rb', line 12

def type_error(cls, value)
	[:type, [cls, value]]
end

#type_error_message(cls, value) ⇒ String

Generate a message for a type error

Parameters:

  • cls (String)

    the expected class

  • value (Object)

    the value that failed

Returns:

  • (String)

    the message



104
105
106
107
# File 'lib/data_model/errors.rb', line 104

def type_error_message(cls, value)
	names = Array(cls).join(" or ")
	"#{value.inspect} is not a #{names}, it is a #{value.class.name}"
end