Module: Xqsr3::Quality::ParameterChecking

Defined in:
lib/xqsr3/quality/parameter_checking.rb

Overview

Parameter-checking utilities

Defined Under Namespace

Modules: Util_

Class Method Summary collapse

Class Method Details

.check_parameter(value, name, options = {}, &block) ⇒ Object

Check a given parameter (value=value, name=name) for type and value

Parameters:

  • +value+

    the parameter whose value and type is to be checked

  • +name+

    the name of the parameter to be checked

  • +options+

    options

  • +:allow_nil+ (Hash)

    a customizable set of options

  • +:types+ (Hash)

    a customizable set of options

  • +:values+ (Hash)

    a customizable set of options

  • +:reject_empty+ (Hash)

    a customizable set of options

  • +:require_empty+ (Hash)

    a customizable set of options

  • +:nothrow+ (Hash)

    a customizable set of options

  • +:message+ (Hash)

    a customizable set of options

  • +:treat_as_option+ (Hash)

    a customizable set of options



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
# File 'lib/xqsr3/quality/parameter_checking.rb', line 97

def check_parameter value, name, options = {}, &block

	failed_check	=	false
	options			||=	{}
	message			=	options[:message]
	treat_as_option	=	options[:treat_as_option]
	return_value	=	value
	param_s			=	treat_as_option	? 'option' : 'parameter'

	warn "#{self}::check_parameter: invoked with non-string/non-symbol name: name.class=#{name.class}" unless name && [ ::String, ::Symbol ].any? { |c| name.is_a?(c) }

	name			=	name.to_s if name.is_a?(::Symbol)
	name			=	(':' + name.to_s) if treat_as_option and name.is_a?(::String)

	# nil check

	if value.nil? && !options[:allow_nil]

		failed_check	=	true

		unless options[:nothrow]

			unless message

				if name.nil?

					message	=	"#{param_s} may not be nil"
				else

					message	=	"#{param_s} '#{name}' may not be nil"
				end
			end

			raise ArgumentError, message
		end
	end

	# check type(s)

	unless value.nil?

		types		=	options[:types] || []
		types		=	[value.class] if types.empty?
		type_found	=	false

		warn "#{self}::check_parameter: options[:types] of type #{types.class} - should be #{::Array}" unless types.is_a?(Array)
		warn "#{self}::check_parameter: options[:types] should contain only classes or arrays of classes" if types.is_a?(::Array) && !types.all? { |c| ::Class === c || (::Array === c || c.all? { |c2| ::Class === c2 }) }

		unless types.any? do |t|

				case t
				when ::Class

					# the (presumed) scalar argument is of type t?
					value.is_a?(t)
				when ::Array

					# the (presumed) vector argument's elements are the
					# possible types
					value.all? { |v| t.any? { |t2| v.is_a?(t2) }}
				end
			end

			failed_check	=	true

			unless options[:nothrow]

				unless message

					s_name		=	name.is_a?(String) ? "'#{name}' " : ''
					s_be		=	'be' #::Array === value ? 'contain' : 'be'

					types_0		=	types.select { |t| ::Class === t }.uniq
					types_ar	=	types.select { |t| ::Array === t }.flatten.uniq

					if types_ar.empty?

						s_types_0	=	Util_.join_with_or types_0

						message		=	"#{param_s} #{s_name}(#{value.class}) must be an instance of #{s_types_0}"
					elsif types_0.empty?

						s_types_ar	=	Util_.join_with_or types_ar

						message		=	"#{param_s} #{s_name}(#{value.class}) must be an array containing instance(s) of #{s_types_ar}"
					else

						s_types_0	=	Util_.join_with_or types_0

						s_types_ar	=	Util_.join_with_or types_ar

						message		=	"#{param_s} #{s_name}(#{value.class}) must be an instance of #{s_types_0}, or an array containing instance(s) of #{s_types_ar}"
					end
				end

				raise TypeError, message
			end
		end
	end

	# reject/require empty?

	if options[:reject_empty]

		warn "#{self}::check_parameter: value '#{value}' of type #{value.class} does not respond to empty?" unless value.respond_to? :empty?

		if value.empty?

			failed_check	=	true

			unless options[:nothrow]

				unless message
					s_name		=	name.is_a?(String) ? "'#{name}' " : ''

					message		=	"#{param_s} #{s_name}must not be empty"
				end

				raise ArgumentError, message
			end
		end
	end

	if options[:require_empty]

		warn "#{self}::check_parameter: value '#{value}' of type #{value.class} does not respond to empty?" unless value.respond_to? :empty?

		unless value.empty?

			failed_check	=	true

			unless options[:nothrow]

				unless message
					s_name		=	name.is_a?(String) ? "'#{name}' " : ''

					message		=	"#{param_s} #{s_name}must be empty"
				end

				raise ArgumentError, message
			end
		end
	end

	# check value(s)

	unless value.nil?

		values	=	options[:values] || [ value ]

		warn "#{self}::check_parameter: options[:values] of type #{values.class} - should be #{::Array}" unless values.is_a?(Array)

		found	=	false

		values.each do |v|

			if ::Range === v && !(::Range === value) && v.cover?(value)

				found = true
				break
			end

			if value == v

				found = true
				break
			end
		end

		unless found

			failed_check	=	true

			unless options[:nothrow]

				unless message
					s_name		=	name.is_a?(String) ? "'#{name}' " : ''

					message		=	"#{param_s} #{s_name}value '#{value}' not found equal/within any of required values or ranges"
				end

				if value.is_a?(::Numeric)

					raise RangeError, message
				else

					raise ArgumentError, message
				end
			end
		end
	end

	# run block

	if value and block

		warn "#{self}::check_parameter: block arity must be 1" unless block.arity == 1

		r	=	nil

		begin

			r = block.call(value)

		rescue StandardError => x

			xmsg	=	x.message || ''

			if xmsg.empty?

				xmsg	||=	message

				if xmsg.empty?

					s_name	=	name.is_a?(String) ? "'#{name}' " : ''
					xmsg	=	"#{param_s} #{s_name}failed validation against caller-supplied block"
				end

				raise $!, xmsg, $!.backtrace
			end

			raise
		end

		if r.is_a?(::Exception)

			# An exception returned from the block, so raise it, with
			# its message or a custom message

			x		=	r
			xmsg	=	x.message || ''

			if xmsg.empty?

				xmsg	||=	message

				if xmsg.empty?

					s_name	=	name.is_a?(String) ? "'#{name}' " : ''
					xmsg	=	"#{param_s} #{s_name}failed validation against caller-supplied block"
				end

				raise x, xmsg
			end

			raise x

		elsif !r

			failed_check	=	true

			unless options[:nothrow]

				s_name	=	name.is_a?(String) ? "'#{name}' " : ''
				xmsg	=	"#{param_s} #{s_name}failed validation against caller-supplied block"

				if value.is_a?(::Numeric)

					raise RangeError, xmsg
				else

					raise ArgumentError, xmsg
				end
			end

		elsif r.is_a?(::TrueClass)

			;
		else

			return_value	=	r
		end
	end

	failed_check ? nil : return_value
end