xqsr3 0.8.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE +27 -0
- data/README.md +26 -0
- data/lib/xqsr3/command_line_utilities/map_option_string.rb +137 -0
- data/lib/xqsr3/containers/frequency_map.rb +473 -0
- data/lib/xqsr3/containers/multi_map.rb +383 -0
- data/lib/xqsr3/diagnostics/exception_utilities.rb +216 -0
- data/lib/xqsr3/doc_.rb +116 -0
- data/lib/xqsr3/extensions/enumerable.rb +4 -0
- data/lib/xqsr3/extensions/enumerable/collect_with_index.rb +71 -0
- data/lib/xqsr3/extensions/enumerable/unique.rb +102 -0
- data/lib/xqsr3/extensions/io.rb +3 -0
- data/lib/xqsr3/extensions/io/writelines.rb +66 -0
- data/lib/xqsr3/extensions/kernel.rb +3 -0
- data/lib/xqsr3/extensions/kernel/raise_with_options.rb +68 -0
- data/lib/xqsr3/extensions/string.rb +5 -0
- data/lib/xqsr3/extensions/string/ends_with.rb +8 -0
- data/lib/xqsr3/extensions/string/map_option_string.rb +8 -0
- data/lib/xqsr3/extensions/string/starts_with.rb +8 -0
- data/lib/xqsr3/extensions/string/to_symbol.rb +8 -0
- data/lib/xqsr3/extensions/test/unit.rb +5 -0
- data/lib/xqsr3/extensions/test/unit/assert_eql.rb +18 -0
- data/lib/xqsr3/extensions/test/unit/assert_not.rb +18 -0
- data/lib/xqsr3/extensions/test/unit/assert_not_eql.rb +18 -0
- data/lib/xqsr3/io/writelines.rb +192 -0
- data/lib/xqsr3/quality/parameter_checking.rb +343 -0
- data/lib/xqsr3/string_utilities/ends_with.rb +134 -0
- data/lib/xqsr3/string_utilities/starts_with.rb +127 -0
- data/lib/xqsr3/string_utilities/to_symbol.rb +145 -0
- data/lib/xqsr3/version.rb +68 -0
- data/test/unit/command_line_utilities/tc_map_option_string.rb +39 -0
- data/test/unit/command_line_utilities/ts_all.rb +13 -0
- data/test/unit/containers/tc_frequency_map.rb +738 -0
- data/test/unit/containers/tc_multi_map.rb +640 -0
- data/test/unit/containers/ts_all.rb +13 -0
- data/test/unit/diagnostics/tc_exception_utilities.rb +221 -0
- data/test/unit/diagnostics/ts_all.rb +13 -0
- data/test/unit/extensions/enumerable/tc_collect_with_index.rb +36 -0
- data/test/unit/extensions/enumerable/tc_unique.rb +47 -0
- data/test/unit/extensions/enumerable/ts_all.rb +13 -0
- data/test/unit/extensions/io/tc_writelines.rb +110 -0
- data/test/unit/extensions/io/ts_all.rb +13 -0
- data/test/unit/extensions/kernel/tc_raise_with_options.rb +239 -0
- data/test/unit/extensions/kernel/ts_all.rb +13 -0
- data/test/unit/extensions/string/tc_ends_with.rb +70 -0
- data/test/unit/extensions/string/tc_map_option_string.rb +37 -0
- data/test/unit/extensions/string/tc_starts_with.rb +70 -0
- data/test/unit/extensions/string/tc_to_symbol.rb +51 -0
- data/test/unit/extensions/string/ts_all.rb +13 -0
- data/test/unit/extensions/ts_all.rb +13 -0
- data/test/unit/io/tc_writelines.rb +200 -0
- data/test/unit/io/ts_all.rb +13 -0
- data/test/unit/quality/tc_parameter_checking.rb +181 -0
- data/test/unit/quality/ts_all.rb +13 -0
- data/test/unit/tc_version.rb +37 -0
- data/test/unit/ts_all.rb +13 -0
- metadata +101 -0
@@ -0,0 +1,18 @@
|
|
1
|
+
|
2
|
+
module Test
|
3
|
+
module Unit
|
4
|
+
|
5
|
+
module Assertions
|
6
|
+
|
7
|
+
unless respond_to? :assert_eql
|
8
|
+
|
9
|
+
def assert_eql(expected, actual, failure_message = '')
|
10
|
+
|
11
|
+
assert expected.eql?(actual), failure_message
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
end # class Assertions
|
16
|
+
end # module Unit
|
17
|
+
end # module Test
|
18
|
+
|
@@ -0,0 +1,18 @@
|
|
1
|
+
|
2
|
+
module Test
|
3
|
+
module Unit
|
4
|
+
|
5
|
+
module Assertions
|
6
|
+
|
7
|
+
unless respond_to? :assert_not
|
8
|
+
|
9
|
+
def assert_not(test, failure_message = '')
|
10
|
+
|
11
|
+
assert !(test), failure_message
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
end # class Assertions
|
16
|
+
end # module Unit
|
17
|
+
end # module Test
|
18
|
+
|
@@ -0,0 +1,18 @@
|
|
1
|
+
|
2
|
+
module Test
|
3
|
+
module Unit
|
4
|
+
|
5
|
+
module Assertions
|
6
|
+
|
7
|
+
unless respond_to? :assert_not_eql
|
8
|
+
|
9
|
+
def assert_not_eql(expected, actual, failure_message = '')
|
10
|
+
|
11
|
+
assert !(expected.eql?(actual)), failure_message
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
end # class Assertions
|
16
|
+
end # module Unit
|
17
|
+
end # module Test
|
18
|
+
|
@@ -0,0 +1,192 @@
|
|
1
|
+
|
2
|
+
# ######################################################################## #
|
3
|
+
# File: lib/xqsr3/io/writelines.rb
|
4
|
+
#
|
5
|
+
# Purpose: Adds a writelines() method to the IO module
|
6
|
+
#
|
7
|
+
# Created: 13th April 2007
|
8
|
+
# Updated: 10th June 2016
|
9
|
+
#
|
10
|
+
# Home: http://github.com/synesissoftware/xqsr3
|
11
|
+
#
|
12
|
+
# Author: Matthew Wilson
|
13
|
+
#
|
14
|
+
# Copyright (c) 2007-2016, Matthew Wilson and Synesis Software
|
15
|
+
# All rights reserved.
|
16
|
+
#
|
17
|
+
# Redistribution and use in source and binary forms, with or without
|
18
|
+
# modification, are permitted provided that the following conditions are
|
19
|
+
# met:
|
20
|
+
#
|
21
|
+
# * Redistributions of source code must retain the above copyright
|
22
|
+
# notice, this list of conditions and the following disclaimer.
|
23
|
+
#
|
24
|
+
# * Redistributions in binary form must reproduce the above copyright
|
25
|
+
# notice, this list of conditions and the following disclaimer in the
|
26
|
+
# documentation and/or other materials provided with the distribution.
|
27
|
+
#
|
28
|
+
# * Neither the names of the copyright holder nor the names of its
|
29
|
+
# contributors may be used to endorse or promote products derived from
|
30
|
+
# this software without specific prior written permission.
|
31
|
+
#
|
32
|
+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
|
33
|
+
# IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
34
|
+
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
35
|
+
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
36
|
+
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
37
|
+
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
38
|
+
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
39
|
+
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
40
|
+
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
41
|
+
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
42
|
+
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
43
|
+
#
|
44
|
+
# ######################################################################## #
|
45
|
+
|
46
|
+
|
47
|
+
require 'xqsr3/quality/parameter_checking'
|
48
|
+
|
49
|
+
=begin
|
50
|
+
=end
|
51
|
+
|
52
|
+
module Xqsr3
|
53
|
+
module IO
|
54
|
+
|
55
|
+
private
|
56
|
+
module WriteLine_Constants_ #:nodoc:
|
57
|
+
|
58
|
+
NUMBER_OF_LINES_TO_EXAMINE = 20
|
59
|
+
|
60
|
+
end # module WriteLine_Constants_
|
61
|
+
|
62
|
+
private
|
63
|
+
|
64
|
+
def self.write_to_target_ target, contents, line_separator, column_separator
|
65
|
+
$stderr.puts "#{self.class}.write_to_target_(target(#{target.class})='#{target}', contents(#{contents.class})='#{contents}', line_separator(#{line_separator.class})='#{line_separator}', column_separator=(#{column_separator.class})='#{column_separator}')" if $DEBUG
|
66
|
+
if contents.instance_of? ::Hash
|
67
|
+
|
68
|
+
contents.each do |k, v|
|
69
|
+
|
70
|
+
target << "#{k}#{column_separator}#{v}#{line_separator}"
|
71
|
+
end
|
72
|
+
else
|
73
|
+
|
74
|
+
contents.each do |element|
|
75
|
+
|
76
|
+
target << "#{element}#{line_separator}"
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
contents.size
|
81
|
+
end
|
82
|
+
|
83
|
+
# This function checks to see if any part of the entries contains an
|
84
|
+
# embedded eol, in which case the empty string is returned to force no
|
85
|
+
# (additional) separator will be used. Otherwise, it returns "\n" to
|
86
|
+
# ensure that that is used.
|
87
|
+
def self.deduce_line_separator_ contents, eol_lookahead_limit
|
88
|
+
|
89
|
+
if contents.instance_of? ::Hash
|
90
|
+
|
91
|
+
contents.each_with_index do |k, v, index|
|
92
|
+
|
93
|
+
if eol_lookahead_limit && eol_lookahead_limit == index
|
94
|
+
|
95
|
+
break
|
96
|
+
else
|
97
|
+
|
98
|
+
return '' if v.to_s.include? "\n"
|
99
|
+
return '' if k.to_s.include? "\n"
|
100
|
+
end
|
101
|
+
end
|
102
|
+
else
|
103
|
+
|
104
|
+
contents.each_with_index do |element, index|
|
105
|
+
|
106
|
+
if eol_lookahead_limit && eol_lookahead_limit == index
|
107
|
+
|
108
|
+
break
|
109
|
+
else
|
110
|
+
|
111
|
+
return '' if element.to_s.include? "\n"
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
"\n"
|
117
|
+
end
|
118
|
+
|
119
|
+
public
|
120
|
+
|
121
|
+
# Writes the contents to the target, subject to the options
|
122
|
+
#
|
123
|
+
# === Signature
|
124
|
+
#
|
125
|
+
# * *Parameters*:
|
126
|
+
# - +target+:: The target of the write, which may be a string containing the path or a stream instance that supports write
|
127
|
+
# - +contents+:: The contents to be write, which may be a +Hash+, or an +Array+, or a +String+ containing delimited fields
|
128
|
+
# - +options+:: An options hash, containing any of the following options
|
129
|
+
#
|
130
|
+
# * *Options*:
|
131
|
+
# - +:column_separator+:: {optional} The column separator, to be applied between each field in the case where +contents+ is a +Hash+.
|
132
|
+
# - +:eol_lookahead_limit+:: {optional} The number of content elements (line/pair) to inspect to determine whether element has a terminating end-of-line sequence. Defaults to 20. If 0, and +:line_separator+ is not specified, then will default to <tt>"\n"</tt>. If +nil+, then every line will be inspected.
|
133
|
+
# - +:line_separator+:: {optional} The line separator, to be applied to the end of line created from each entry. When not specified, it will be deduced by inspecting +contents+ (according to +eol_lookahead_limit+).
|
134
|
+
#
|
135
|
+
# === Return
|
136
|
+
#
|
137
|
+
# The number of entries in +contents+
|
138
|
+
def self.writelines target, contents, options = {}
|
139
|
+
|
140
|
+
# validate parameters
|
141
|
+
|
142
|
+
::Xqsr3::Quality::ParameterChecking.check_parameter(target, 'target', allow_nil: false) do |v|
|
143
|
+
|
144
|
+
raise TypeError, "#{self}#writeline() 'target' parameter must be a #{::String} or respond to <<" unless ::String === v || v.respond_to?(:<<)
|
145
|
+
true
|
146
|
+
end
|
147
|
+
::Xqsr3::Quality::ParameterChecking.check_parameter(contents, 'contents', allow_nil: false, types: [ ::String, ::Hash, ::Array ])
|
148
|
+
|
149
|
+
# process parameters
|
150
|
+
|
151
|
+
if contents.instance_of? String
|
152
|
+
|
153
|
+
if contents.include? "\n"
|
154
|
+
|
155
|
+
contents = contents.split(/\r?\n/, -1)
|
156
|
+
else
|
157
|
+
|
158
|
+
contents = [ contents ]
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
options ||= {}
|
163
|
+
eol_lookahead_limit = options[:eol_lookahead_limit] || WriteLine_Constants_::NUMBER_OF_LINES_TO_EXAMINE
|
164
|
+
column_separator = options[:column_separator] || ''
|
165
|
+
line_separator = nil
|
166
|
+
line_separator ||= options[:line_separator]
|
167
|
+
line_separator ||= self.deduce_line_separator_(contents, eol_lookahead_limit) unless !eol_lookahead_limit.kind_of?(::Integer) || 0 == eol_lookahead_limit
|
168
|
+
line_separator ||= "\n"
|
169
|
+
|
170
|
+
if not contents.kind_of? ::Enumerable and not contents.instance_of? ::Hash
|
171
|
+
|
172
|
+
raise ArgumentError, "writelines() must be passed a #{::String}, or a #{::Hash}, or an #{::Enumerable} (or derived)"
|
173
|
+
end
|
174
|
+
|
175
|
+
# do the writing
|
176
|
+
|
177
|
+
if ::String === target
|
178
|
+
|
179
|
+
File.open(target, "w") do |io|
|
180
|
+
|
181
|
+
self.write_to_target_ io, contents, line_separator, column_separator
|
182
|
+
end
|
183
|
+
else
|
184
|
+
|
185
|
+
self.write_to_target_ target, contents, line_separator, column_separator
|
186
|
+
end
|
187
|
+
end # writelines
|
188
|
+
end # module IO
|
189
|
+
end # module Xqsr3
|
190
|
+
|
191
|
+
# ############################## end of file ############################# #
|
192
|
+
|
@@ -0,0 +1,343 @@
|
|
1
|
+
|
2
|
+
# ######################################################################## #
|
3
|
+
# File: lib/xqsr3/quality/parameter_checking.rb
|
4
|
+
#
|
5
|
+
# Purpose: Definition of the ParameterChecking module
|
6
|
+
#
|
7
|
+
# Created: 12th February 2015
|
8
|
+
# Updated: 10th June 2016
|
9
|
+
#
|
10
|
+
# Home: http://github.com/synesissoftware/xqsr3
|
11
|
+
#
|
12
|
+
# Author: Matthew Wilson
|
13
|
+
#
|
14
|
+
# Copyright (c) 2015-2016, Matthew Wilson and Synesis Software
|
15
|
+
# All rights reserved.
|
16
|
+
#
|
17
|
+
# Redistribution and use in source and binary forms, with or without
|
18
|
+
# modification, are permitted provided that the following conditions are
|
19
|
+
# met:
|
20
|
+
#
|
21
|
+
# * Redistributions of source code must retain the above copyright
|
22
|
+
# notice, this list of conditions and the following disclaimer.
|
23
|
+
#
|
24
|
+
# * Redistributions in binary form must reproduce the above copyright
|
25
|
+
# notice, this list of conditions and the following disclaimer in the
|
26
|
+
# documentation and/or other materials provided with the distribution.
|
27
|
+
#
|
28
|
+
# * Neither the names of the copyright holder nor the names of its
|
29
|
+
# contributors may be used to endorse or promote products derived from
|
30
|
+
# this software without specific prior written permission.
|
31
|
+
#
|
32
|
+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
|
33
|
+
# IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
34
|
+
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
35
|
+
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
36
|
+
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
37
|
+
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
38
|
+
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
39
|
+
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
40
|
+
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
41
|
+
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
42
|
+
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
43
|
+
#
|
44
|
+
# ######################################################################## #
|
45
|
+
|
46
|
+
|
47
|
+
=begin
|
48
|
+
=end
|
49
|
+
|
50
|
+
module Xqsr3
|
51
|
+
module Quality
|
52
|
+
|
53
|
+
# Parameter-checking utilities
|
54
|
+
#
|
55
|
+
module ParameterChecking
|
56
|
+
|
57
|
+
# Check a given parameter (value=+value+, name=+name+) for type and value
|
58
|
+
#
|
59
|
+
# @param +value+ the parameter whose value and type is to be checked
|
60
|
+
# @param +name+ the name of the parameter to be checked
|
61
|
+
# @param +options+ options
|
62
|
+
#
|
63
|
+
# @option +:allow_nil+ the +value+ must not be +nil+ unless this option
|
64
|
+
# is true
|
65
|
+
# @option +:types+ an array of types one of which +value+ must be (or
|
66
|
+
# must be derived from)
|
67
|
+
# @option +:values+ an array of values one of which +value+ must be
|
68
|
+
# @option +:reject_empty+ requires value to respond to +empty?+
|
69
|
+
# message and to do so with false, unless +nil+
|
70
|
+
# @option +:require_empty+ requires value to respond to +empty?+
|
71
|
+
# message and to do so with true, unless +nil+
|
72
|
+
# @option +:nothrow+ causes failure to be indicated by a +nil+ return
|
73
|
+
# rather than a thrown exception
|
74
|
+
# @option +:message+ specifies a message to be used in any thrown
|
75
|
+
# exception, which suppresses internal message preparation
|
76
|
+
# @option +:treat_as_option+ if true, the value will be treated as an
|
77
|
+
# option when reporting check failure
|
78
|
+
def check_parameter value, name, options = {}, &block
|
79
|
+
|
80
|
+
failed_check = false
|
81
|
+
options ||= {}
|
82
|
+
message = options[:message]
|
83
|
+
treat_as_option = options[:treat_as_option]
|
84
|
+
return_value = value
|
85
|
+
param_s = treat_as_option ? 'option' : 'parameter'
|
86
|
+
|
87
|
+
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) }
|
88
|
+
|
89
|
+
name = name.to_s if name.is_a?(::Symbol)
|
90
|
+
name = (':' + name.to_s) if treat_as_option and name.is_a?(::String)
|
91
|
+
|
92
|
+
# nil check
|
93
|
+
|
94
|
+
if value.nil? && !options[:allow_nil]
|
95
|
+
|
96
|
+
failed_check = true
|
97
|
+
|
98
|
+
unless options[:nothrow]
|
99
|
+
|
100
|
+
unless message
|
101
|
+
|
102
|
+
if name.nil?
|
103
|
+
|
104
|
+
message = "#{param_s} may not be nil"
|
105
|
+
else
|
106
|
+
|
107
|
+
message = "#{param_s} '#{name}' may not be nil"
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
raise ArgumentError, message
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
# check type(s)
|
116
|
+
|
117
|
+
unless value.nil?
|
118
|
+
|
119
|
+
types = options[:types] || []
|
120
|
+
types = [value.class] if types.empty?
|
121
|
+
type_found = false
|
122
|
+
|
123
|
+
warn "#{self}::check_parameter: options[:types] of type #{types.class} - should be #{::Array}" unless types.is_a?(Array)
|
124
|
+
warn "#{self}::check_parameter: options[:types] should contain only classes" if types.is_a?(Array) && !types.all? { |c| ::Class === c }
|
125
|
+
|
126
|
+
unless types.any? { |t| value.is_a?(t) }
|
127
|
+
|
128
|
+
failed_check = true
|
129
|
+
|
130
|
+
unless options[:nothrow]
|
131
|
+
|
132
|
+
unless message
|
133
|
+
|
134
|
+
s_name = name.is_a?(String) ? "'#{name}' " : ''
|
135
|
+
|
136
|
+
case types.size
|
137
|
+
when 1
|
138
|
+
|
139
|
+
s_types = "#{types[0]}"
|
140
|
+
when 2
|
141
|
+
|
142
|
+
s_types = "#{types[0]} or #{types[1]}"
|
143
|
+
else
|
144
|
+
|
145
|
+
s_types = "#{types[0...-1].join(', ')}, or #{types[-1]}"
|
146
|
+
end
|
147
|
+
|
148
|
+
message = "#{param_s} #{s_name}(#{value.class}) must be an instance of #{s_types}"
|
149
|
+
end
|
150
|
+
|
151
|
+
raise TypeError, message
|
152
|
+
end
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
156
|
+
# reject/require empty?
|
157
|
+
|
158
|
+
if options[:reject_empty]
|
159
|
+
|
160
|
+
warn "#{self}::check_parameter: value '#{value}' of type #{value.class} does not respond to empty?" unless value.respond_to? :empty?
|
161
|
+
|
162
|
+
if value.empty?
|
163
|
+
|
164
|
+
failed_check = true
|
165
|
+
|
166
|
+
unless options[:nothrow]
|
167
|
+
|
168
|
+
unless message
|
169
|
+
s_name = name.is_a?(String) ? "'#{name}' " : ''
|
170
|
+
|
171
|
+
message = "#{param_s} #{s_name}must not be empty"
|
172
|
+
end
|
173
|
+
|
174
|
+
raise ArgumentError, message
|
175
|
+
end
|
176
|
+
end
|
177
|
+
end
|
178
|
+
|
179
|
+
if options[:require_empty]
|
180
|
+
|
181
|
+
warn "#{self}::check_parameter: value '#{value}' of type #{value.class} does not respond to empty?" unless value.respond_to? :empty?
|
182
|
+
|
183
|
+
unless value.empty?
|
184
|
+
|
185
|
+
failed_check = true
|
186
|
+
|
187
|
+
unless options[:nothrow]
|
188
|
+
|
189
|
+
unless message
|
190
|
+
s_name = name.is_a?(String) ? "'#{name}' " : ''
|
191
|
+
|
192
|
+
message = "#{param_s} #{s_name}must be empty"
|
193
|
+
end
|
194
|
+
|
195
|
+
raise ArgumentError, message
|
196
|
+
end
|
197
|
+
end
|
198
|
+
end
|
199
|
+
|
200
|
+
# check value(s)
|
201
|
+
|
202
|
+
unless value.nil?
|
203
|
+
|
204
|
+
values = options[:values] || [ value ]
|
205
|
+
|
206
|
+
warn "#{self}::check_parameter: options[:values] of type #{values.class} - should be #{::Array}" unless values.is_a?(Array)
|
207
|
+
|
208
|
+
found = false
|
209
|
+
|
210
|
+
values.each do |v|
|
211
|
+
|
212
|
+
if ::Range === v && !(::Range === value) && v.cover?(value)
|
213
|
+
|
214
|
+
found = true
|
215
|
+
break
|
216
|
+
end
|
217
|
+
|
218
|
+
if value == v
|
219
|
+
|
220
|
+
found = true
|
221
|
+
break
|
222
|
+
end
|
223
|
+
end
|
224
|
+
|
225
|
+
unless found
|
226
|
+
|
227
|
+
failed_check = true
|
228
|
+
|
229
|
+
unless options[:nothrow]
|
230
|
+
|
231
|
+
unless message
|
232
|
+
s_name = name.is_a?(String) ? "'#{name}' " : ''
|
233
|
+
|
234
|
+
message = "#{param_s} #{s_name}value '#{value}' not found equal/within any of required values or ranges"
|
235
|
+
end
|
236
|
+
|
237
|
+
if value.is_a?(::Numeric)
|
238
|
+
|
239
|
+
raise RangeError, message
|
240
|
+
else
|
241
|
+
|
242
|
+
raise ArgumentError, message
|
243
|
+
end
|
244
|
+
end
|
245
|
+
end
|
246
|
+
end
|
247
|
+
|
248
|
+
# run block
|
249
|
+
|
250
|
+
if value and block
|
251
|
+
|
252
|
+
warn "#{self}::check_parameter: block arity must be 1" unless block.arity == 1
|
253
|
+
|
254
|
+
r = nil
|
255
|
+
|
256
|
+
begin
|
257
|
+
|
258
|
+
r = block.call(value)
|
259
|
+
|
260
|
+
rescue StandardError => x
|
261
|
+
|
262
|
+
xmsg = x.message || ''
|
263
|
+
|
264
|
+
if xmsg.empty?
|
265
|
+
|
266
|
+
xmsg ||= message
|
267
|
+
|
268
|
+
if xmsg.empty?
|
269
|
+
|
270
|
+
s_name = name.is_a?(String) ? "'#{name}' " : ''
|
271
|
+
xmsg = "#{param_s} #{s_name}failed validation against caller-supplied block"
|
272
|
+
end
|
273
|
+
|
274
|
+
raise $!, xmsg, $!.backtrace
|
275
|
+
end
|
276
|
+
|
277
|
+
raise
|
278
|
+
end
|
279
|
+
|
280
|
+
if r.is_a?(::Exception)
|
281
|
+
|
282
|
+
# An exception returned from the block, so raise it, with
|
283
|
+
# its message or a custom message
|
284
|
+
|
285
|
+
x = r
|
286
|
+
xmsg = x.message || ''
|
287
|
+
|
288
|
+
if xmsg.empty?
|
289
|
+
|
290
|
+
xmsg ||= message
|
291
|
+
|
292
|
+
if xmsg.empty?
|
293
|
+
|
294
|
+
s_name = name.is_a?(String) ? "'#{name}' " : ''
|
295
|
+
xmsg = "#{param_s} #{s_name}failed validation against caller-supplied block"
|
296
|
+
end
|
297
|
+
|
298
|
+
raise x, xmsg
|
299
|
+
end
|
300
|
+
|
301
|
+
raise x
|
302
|
+
|
303
|
+
elsif !r
|
304
|
+
|
305
|
+
failed_check = true
|
306
|
+
|
307
|
+
unless options[:nothrow]
|
308
|
+
|
309
|
+
s_name = name.is_a?(String) ? "'#{name}' " : ''
|
310
|
+
xmsg = "#{param_s} #{s_name}failed validation against caller-supplied block"
|
311
|
+
|
312
|
+
if value.is_a?(::Numeric)
|
313
|
+
|
314
|
+
raise RangeError, xmsg
|
315
|
+
else
|
316
|
+
|
317
|
+
raise ArgumentError, xmsg
|
318
|
+
end
|
319
|
+
end
|
320
|
+
|
321
|
+
elsif r.is_a?(::TrueClass)
|
322
|
+
|
323
|
+
;
|
324
|
+
else
|
325
|
+
|
326
|
+
return_value = r
|
327
|
+
end
|
328
|
+
end
|
329
|
+
|
330
|
+
failed_check ? nil : return_value
|
331
|
+
end
|
332
|
+
|
333
|
+
alias check_param check_parameter
|
334
|
+
|
335
|
+
module_function :check_parameter
|
336
|
+
|
337
|
+
end # module ParameterChecking
|
338
|
+
|
339
|
+
end # module Quality
|
340
|
+
end # module Xqsr3
|
341
|
+
|
342
|
+
# ############################## end of file ############################# #
|
343
|
+
|