xqsr3 0.39.1 → 0.39.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/examples/count_word_frequencies.rb +2 -0
- data/lib/xqsr3/array_utilities/join_with_or.rb +50 -53
- data/lib/xqsr3/command_line_utilities/map_option_string.rb +66 -69
- data/lib/xqsr3/containers/frequency_map.rb +408 -394
- data/lib/xqsr3/containers/multi_map.rb +464 -446
- data/lib/xqsr3/conversion/bool_parser.rb +49 -54
- data/lib/xqsr3/conversion/integer_parser.rb +84 -89
- data/lib/xqsr3/diagnostics/exception_utilities.rb +127 -132
- data/lib/xqsr3/diagnostics/exceptions/with_cause.rb +98 -102
- data/lib/xqsr3/diagnostics/inspect_builder.rb +83 -84
- data/lib/xqsr3/extensions/array/join_with_or.rb +2 -1
- data/lib/xqsr3/extensions/enumerable/collect_with_index.rb +1 -4
- data/lib/xqsr3/extensions/enumerable/unique.rb +1 -4
- data/lib/xqsr3/extensions/hash/deep_transform.rb +1 -0
- data/lib/xqsr3/extensions/hash/except.rb +1 -1
- data/lib/xqsr3/extensions/hash/has_match.rb +3 -2
- data/lib/xqsr3/extensions/hash/match.rb +3 -2
- data/lib/xqsr3/extensions/integer/to_s_grp.rb +1 -4
- data/lib/xqsr3/extensions/io/writelines.rb +4 -7
- data/lib/xqsr3/extensions/kernel/integer.rb +7 -9
- data/lib/xqsr3/extensions/kernel/raise_with_options.rb +1 -3
- data/lib/xqsr3/extensions/string/ends_with.rb +1 -0
- data/lib/xqsr3/extensions/string/map_option_string.rb +1 -0
- data/lib/xqsr3/extensions/string/nil_if_empty.rb +1 -0
- data/lib/xqsr3/extensions/string/nil_if_whitespace.rb +1 -0
- data/lib/xqsr3/extensions/string/quote_if.rb +1 -0
- data/lib/xqsr3/extensions/string/starts_with.rb +1 -0
- data/lib/xqsr3/extensions/string/to_bool.rb +1 -0
- data/lib/xqsr3/extensions/string/to_symbol.rb +1 -0
- data/lib/xqsr3/extensions/string/truncate.rb +1 -0
- data/lib/xqsr3/extensions/test/unit/assert_eql.rb +11 -9
- data/lib/xqsr3/extensions/test/unit/assert_false.rb +10 -8
- data/lib/xqsr3/extensions/test/unit/assert_not.rb +10 -8
- data/lib/xqsr3/extensions/test/unit/assert_not_eql.rb +11 -9
- data/lib/xqsr3/extensions/test/unit/assert_raise_with_message.rb +58 -55
- data/lib/xqsr3/extensions/test/unit/assert_subclass_of.rb +11 -9
- data/lib/xqsr3/extensions/test/unit/assert_superclass_of.rb +11 -9
- data/lib/xqsr3/extensions/test/unit/assert_true.rb +10 -8
- data/lib/xqsr3/extensions/test/unit/assert_type_has_instance_methods.rb +39 -37
- data/lib/xqsr3/hash_utilities/deep_transform.rb +63 -67
- data/lib/xqsr3/hash_utilities/key_matching.rb +72 -76
- data/lib/xqsr3/internal_/test_unit_version_.rb +10 -8
- data/lib/xqsr3/io/writelines.rb +12 -12
- data/lib/xqsr3/quality/parameter_checking.rb +406 -411
- data/lib/xqsr3/string_utilities/ends_with.rb +60 -64
- data/lib/xqsr3/string_utilities/nil_if_empty.rb +31 -35
- data/lib/xqsr3/string_utilities/nil_if_whitespace.rb +32 -36
- data/lib/xqsr3/string_utilities/quote_if.rb +48 -57
- data/lib/xqsr3/string_utilities/starts_with.rb +60 -64
- data/lib/xqsr3/string_utilities/to_symbol.rb +77 -81
- data/lib/xqsr3/string_utilities/truncate.rb +56 -60
- data/lib/xqsr3/version.rb +2 -2
- data/test/performance/frequency_map.rb +3 -0
- data/test/scratch/test_assert_raise_with_message.rb +2 -0
- data/test/unit/array_utilities/tc_join_with_or.rb +3 -1
- data/test/unit/command_line_utilities/tc_map_option_string.rb +3 -0
- data/test/unit/containers/tc_frequency_map.rb +5 -2
- data/test/unit/containers/tc_multi_map.rb +3 -0
- data/test/unit/conversion/tc_integer_parser.rb +18 -17
- data/test/unit/conversion/tc_to_bool.rb +2 -0
- data/test/unit/diagnostics/exceptions/tc_with_cause.rb +7 -5
- data/test/unit/diagnostics/tc_exception_utilities.rb +2 -0
- data/test/unit/extensions/enumerable/tc_collect_with_index.rb +2 -0
- data/test/unit/extensions/enumerable/tc_detect_map.rb +3 -2
- data/test/unit/extensions/enumerable/tc_unique.rb +2 -0
- data/test/unit/extensions/hash/tc_deep_transform.rb +2 -0
- data/test/unit/extensions/hash/tc_except.rb +3 -1
- data/test/unit/extensions/hash/tc_hash.rb +1 -0
- data/test/unit/extensions/hash/tc_slice.rb +2 -0
- data/test/unit/extensions/integer/tc_to_s_grp.rb +2 -0
- data/test/unit/extensions/io/tc_writelines.rb +86 -0
- data/test/unit/extensions/kernel/tc_integer.rb +10 -9
- data/test/unit/extensions/kernel/tc_raise_with_options.rb +2 -0
- data/test/unit/extensions/object/tc_inspect.rb +15 -13
- data/test/unit/extensions/string/tc_bool.tb +2 -0
- data/test/unit/extensions/string/tc_ends_with.rb +2 -0
- data/test/unit/extensions/string/tc_map_option_string.rb +2 -0
- data/test/unit/extensions/string/tc_nil_if_empty.rb +2 -0
- data/test/unit/extensions/string/tc_nil_if_whitespace.rb +2 -0
- data/test/unit/extensions/string/tc_quote_if.rb +2 -0
- data/test/unit/extensions/string/tc_starts_with.rb +2 -0
- data/test/unit/extensions/string/tc_to_symbol.rb +2 -0
- data/test/unit/extensions/string/tc_truncate.rb +2 -0
- data/test/unit/extensions/test/unit/tc_assert_raise_with_message.rb +2 -0
- data/test/unit/extensions/test/unit/tc_assert_subclass_of.rb +2 -0
- data/test/unit/extensions/test/unit/tc_assert_superclass_of.rb +2 -0
- data/test/unit/hash_utilities/tc_has_match.rb +1 -0
- data/test/unit/hash_utilities/tc_match.rb +1 -0
- data/test/unit/io/tc_writelines.rb +63 -0
- data/test/unit/quality/tc_parameter_checking.rb +1 -0
- data/test/unit/string_utilities/tc_truncate.rb +2 -0
- data/test/unit/tc_version.rb +2 -0
- metadata +3 -3
@@ -5,7 +5,7 @@
|
|
5
5
|
# Purpose: Definition of the ::Xqsr3::Conversion::BoolParser module
|
6
6
|
#
|
7
7
|
# Created: 3rd June 2017
|
8
|
-
# Updated:
|
8
|
+
# Updated: 12th April 2024
|
9
9
|
#
|
10
10
|
# Home: http://github.com/synesissoftware/xqsr3
|
11
11
|
#
|
@@ -45,69 +45,64 @@
|
|
45
45
|
# ######################################################################## #
|
46
46
|
|
47
47
|
|
48
|
-
# ##########################################################
|
49
|
-
# ::Xqsr3::Conversion::BoolParser
|
50
|
-
|
51
48
|
=begin
|
52
49
|
=end
|
53
50
|
|
54
51
|
module Xqsr3
|
55
52
|
module Conversion
|
56
53
|
|
57
|
-
# +include-able module that provides Boolean parsing
|
58
|
-
module BoolParser
|
54
|
+
# +include-able module that provides Boolean parsing
|
55
|
+
module BoolParser
|
59
56
|
|
60
|
-
|
61
|
-
|
57
|
+
private
|
58
|
+
def self.matches_to_ s, expr
|
62
59
|
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
60
|
+
case expr
|
61
|
+
when ::Regexp
|
62
|
+
return expr =~ s
|
63
|
+
else
|
64
|
+
return s == expr
|
65
|
+
end
|
68
66
|
end
|
69
|
-
end
|
70
|
-
|
71
|
-
public
|
72
|
-
|
73
|
-
# Recognised truey values
|
74
|
-
DEFAULT_TRUE_VALUES = [ /true/i, '1' ]
|
75
|
-
# Recognised falsey values
|
76
|
-
DEFAULT_FALSE_VALUES = [ /false/i, '0' ]
|
77
|
-
|
78
|
-
# Attempts to parse the given string +s+ to a Boolean value, based on the
|
79
|
-
# given +options+
|
80
|
-
#
|
81
|
-
# === Signature
|
82
|
-
#
|
83
|
-
# * *Parameters:*
|
84
|
-
# - +s+ The string to be parsed
|
85
|
-
# - +options+ An options hash, containing any of the following options
|
86
|
-
#
|
87
|
-
# * *Options:*
|
88
|
-
# - +:false_values+ (::Array) An array of strings or regular expressions against which to match for false value. Defaults to +DEFAULT_FALSE_VALUES+
|
89
|
-
# - +:true_values+ (::Array) An array of strings or regular expressions against which to match for true value. Defaults to +DEFAULT_TRUE_VALUES+
|
90
|
-
# - +:default_value+ An object to be returned if matching fails. Defaults to +nil+
|
91
|
-
# - +:false_value+ An object to be returned if matching succeeds to match against +:false_values+. Defaults to +false+
|
92
|
-
# - +:true_value+ An object to be returned if matching succeeds to match against +:true_values+. Defaults to +true+
|
93
|
-
def self.to_bool s, **options
|
94
|
-
|
95
|
-
true_values = options[:true_values] || DEFAULT_TRUE_VALUES
|
96
|
-
true_values = [ true_values ] unless true_values.is_a? ::Array
|
97
|
-
false_values = options[:false_values] || DEFAULT_FALSE_VALUES
|
98
|
-
false_values = [ false_values ] unless false_values.is_a? ::Array
|
99
|
-
default_value = options[:default] || nil
|
100
|
-
true_value = options[:true] || true
|
101
|
-
false_value = options[:false] || false
|
102
|
-
|
103
|
-
return true_value if true_values.any? { |v| self.matches_to_ s, v }
|
104
|
-
return false_value if false_values.any? { |v| self.matches_to_ s, v }
|
105
|
-
|
106
|
-
return default_value
|
107
|
-
end
|
108
|
-
|
109
|
-
end # module BoolParser
|
110
67
|
|
68
|
+
public
|
69
|
+
|
70
|
+
# Recognised truey values
|
71
|
+
DEFAULT_TRUE_VALUES = [ /true/i, '1' ]
|
72
|
+
# Recognised falsey values
|
73
|
+
DEFAULT_FALSE_VALUES = [ /false/i, '0' ]
|
74
|
+
|
75
|
+
# Attempts to parse the given string +s+ to a Boolean value, based on the
|
76
|
+
# given +options+
|
77
|
+
#
|
78
|
+
# === Signature
|
79
|
+
#
|
80
|
+
# * *Parameters:*
|
81
|
+
# - +s+ The string to be parsed;
|
82
|
+
# - +options+ (+Hash+) Options that control the behaviour of the method;
|
83
|
+
#
|
84
|
+
# * *Options:*
|
85
|
+
# - +:false_values+ (+Array+) An array of strings or regular expressions against which to match for false value. Defaults to +DEFAULT_FALSE_VALUES+;
|
86
|
+
# - +:true_values+ (+Array+) An array of strings or regular expressions against which to match for true value. Defaults to +DEFAULT_TRUE_VALUES+;
|
87
|
+
# - +:default_value+ An object to be returned if matching fails. Defaults to +nil+;
|
88
|
+
# - +:false_value+ An object to be returned if matching succeeds to match against +:false_values+. Defaults to +false+;
|
89
|
+
# - +:true_value+ An object to be returned if matching succeeds to match against +:true_values+. Defaults to +true+;
|
90
|
+
def self.to_bool s, **options
|
91
|
+
|
92
|
+
true_values = options[:true_values] || DEFAULT_TRUE_VALUES
|
93
|
+
true_values = [ true_values ] unless true_values.is_a? ::Array
|
94
|
+
false_values = options[:false_values] || DEFAULT_FALSE_VALUES
|
95
|
+
false_values = [ false_values ] unless false_values.is_a? ::Array
|
96
|
+
default_value = options[:default] || nil
|
97
|
+
true_value = options[:true] || true
|
98
|
+
false_value = options[:false] || false
|
99
|
+
|
100
|
+
return true_value if true_values.any? { |v| self.matches_to_ s, v }
|
101
|
+
return false_value if false_values.any? { |v| self.matches_to_ s, v }
|
102
|
+
|
103
|
+
return default_value
|
104
|
+
end
|
105
|
+
end # module BoolParser
|
111
106
|
end # module Conversion
|
112
107
|
end # module Xqsr3
|
113
108
|
|
@@ -5,7 +5,7 @@
|
|
5
5
|
# Purpose: Definition of the ::Xqsr3::Conversion::IntegerParser module
|
6
6
|
#
|
7
7
|
# Created: 21st November 2017
|
8
|
-
# Updated:
|
8
|
+
# Updated: 12th April 2024
|
9
9
|
#
|
10
10
|
# Home: http://github.com/synesissoftware/xqsr3
|
11
11
|
#
|
@@ -45,140 +45,135 @@
|
|
45
45
|
# ######################################################################## #
|
46
46
|
|
47
47
|
|
48
|
-
# ##########################################################
|
49
|
-
# ::Xqsr3::Conversion::IntegerParser
|
50
|
-
|
51
48
|
=begin
|
52
49
|
=end
|
53
50
|
|
54
51
|
module Xqsr3
|
55
52
|
module Conversion
|
56
53
|
|
57
|
-
# +include-able module that provides Integer parsing
|
58
|
-
module IntegerParser
|
54
|
+
# +include-able module that provides Integer parsing
|
55
|
+
module IntegerParser
|
59
56
|
|
60
|
-
|
61
|
-
|
62
|
-
|
57
|
+
private
|
58
|
+
# @!visibility private
|
59
|
+
module IntegerParser_Helper_ # :nodoc: all
|
63
60
|
|
64
|
-
|
61
|
+
if Kernel.respond_to?(:xqsr3_Integer_original_method)
|
65
62
|
|
66
|
-
|
63
|
+
def self.invoke_Integer_1(arg)
|
67
64
|
|
68
|
-
|
69
|
-
|
65
|
+
Kernel.xqsr3_Integer_original_method(arg)
|
66
|
+
end
|
70
67
|
|
71
|
-
|
68
|
+
def self.invoke_Integer_2(arg, base)
|
72
69
|
|
73
|
-
|
74
|
-
|
75
|
-
|
70
|
+
Kernel.xqsr3_Integer_original_method(arg, base)
|
71
|
+
end
|
72
|
+
else
|
76
73
|
|
77
|
-
|
74
|
+
def self.invoke_Integer_1(arg)
|
78
75
|
|
79
|
-
|
80
|
-
|
76
|
+
Kernel.Integer(arg)
|
77
|
+
end
|
81
78
|
|
82
|
-
|
79
|
+
def self.invoke_Integer_2(arg, base)
|
83
80
|
|
84
|
-
|
81
|
+
Kernel.Integer(arg, base)
|
82
|
+
end
|
85
83
|
end
|
86
|
-
end
|
87
84
|
|
88
|
-
|
85
|
+
def self.invoke_Integer(arg, base)
|
89
86
|
|
90
|
-
|
91
|
-
|
87
|
+
case arg
|
88
|
+
when ::String
|
92
89
|
|
93
|
-
|
94
|
-
|
90
|
+
self.invoke_Integer_2 arg, base
|
91
|
+
else
|
95
92
|
|
96
|
-
|
93
|
+
if $DEBUG
|
97
94
|
|
98
|
-
|
99
|
-
|
95
|
+
case base
|
96
|
+
when nil, 0
|
100
97
|
|
101
|
-
|
102
|
-
|
98
|
+
;
|
99
|
+
else
|
103
100
|
|
104
|
-
|
101
|
+
warn "WARNING: #{self}::#{__method__}: " + 'base specified for non string value'
|
102
|
+
end
|
105
103
|
end
|
106
|
-
end
|
107
104
|
|
108
|
-
|
105
|
+
self.invoke_Integer_1 arg
|
106
|
+
end
|
109
107
|
end
|
110
|
-
end
|
111
108
|
|
112
|
-
|
109
|
+
def self.to_integer_ arg, base, options, &block
|
113
110
|
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
111
|
+
case options
|
112
|
+
when ::Hash
|
113
|
+
;
|
114
|
+
else
|
118
115
|
|
119
|
-
|
120
|
-
|
116
|
+
raise TypeError, "options must be of type #{::Hash}, #{options.class} given"
|
117
|
+
end
|
121
118
|
|
122
|
-
|
119
|
+
if block_given?
|
123
120
|
|
124
|
-
|
121
|
+
begin
|
125
122
|
|
126
|
-
|
127
|
-
|
123
|
+
return self.invoke_Integer arg, base
|
124
|
+
rescue ArgumentError, TypeError => x
|
128
125
|
|
129
|
-
|
126
|
+
return yield x, arg, base, options
|
127
|
+
end
|
130
128
|
end
|
131
|
-
end
|
132
129
|
|
133
|
-
|
130
|
+
if options.has_key?(:default) || options[:nil]
|
134
131
|
|
135
|
-
|
132
|
+
unless arg.nil?
|
136
133
|
|
137
|
-
|
134
|
+
begin
|
138
135
|
|
139
|
-
|
140
|
-
|
136
|
+
return self.invoke_Integer arg, base
|
137
|
+
rescue ArgumentError, TypeError
|
138
|
+
end
|
141
139
|
end
|
142
|
-
end
|
143
140
|
|
144
|
-
|
141
|
+
return options[:default] if options.has_key? :default
|
145
142
|
|
146
|
-
|
147
|
-
|
143
|
+
return nil
|
144
|
+
else
|
148
145
|
|
149
|
-
|
146
|
+
self.invoke_Integer arg, base
|
147
|
+
end
|
150
148
|
end
|
149
|
+
end # module IntegerParser_Helper_
|
150
|
+
public
|
151
|
+
|
152
|
+
# Attempts to convert a variable to an integer, according to the given
|
153
|
+
# options and block
|
154
|
+
#
|
155
|
+
# === Signature
|
156
|
+
#
|
157
|
+
# * *Parameters:*
|
158
|
+
# - +arg+ The argument to be converted (to +Fixnum+ or +Bignum+);
|
159
|
+
# - +base+ A value of 0, or between 2 and 36. Defaults to 0;
|
160
|
+
# - +options+ (+Hash+) Options that control the behaviour of the method;
|
161
|
+
# - +block+ An optional caller-supplied 4-parameter block - taking the exception, +arg+, +base+, and +options+ - that will be invoked with the +ArgumentError+ exception, allowing the caller to take additional action. If the block returns then its return value will be returned to the caller;
|
162
|
+
#
|
163
|
+
# * *Options:*
|
164
|
+
# - +:default+ A default value to be used when +arg+ is +nil+ or cannot be converted by (the original) +Kernel#Integer+;
|
165
|
+
# - +:nil+ Returns +nil+ if +arg+ is +nil+ or cannot be converted by (the original) +Kernel#Integer+. Ignored if +:default+ is specified;
|
166
|
+
def self.to_integer arg, base = 0, **options, &block
|
167
|
+
|
168
|
+
IntegerParser_Helper_.to_integer_ arg, base, options, &block
|
151
169
|
end
|
152
|
-
end # module IntegerParser_Helper_
|
153
|
-
public
|
154
|
-
|
155
|
-
# Attempts to convert a variable to an integer, according to the given
|
156
|
-
# options and block
|
157
|
-
#
|
158
|
-
# === Signature
|
159
|
-
#
|
160
|
-
# * *Parameters:*
|
161
|
-
# - +arg+ The argument to be converted (to +Fixnum+ or +Bignum+)
|
162
|
-
# - +base+ A value of 0, or between 2 and 36. Defaults to 0
|
163
|
-
# - +options+ An options hash, containing any of the following options
|
164
|
-
# - +block+ An optional caller-supplied 4-parameter block - taking the exception, +arg+, +base+, and +options+ - that will be invoked with the +ArgumentError+ exception, allowing the caller to take additional action. If the block returns then its return value will be returned to the caller
|
165
|
-
#
|
166
|
-
# * *Options:*
|
167
|
-
# - +:default+ A default value to be used when +arg+ is +nil+ or cannot be converted by (the original) +Kernel#Integer+
|
168
|
-
# - +:nil+ Returns +nil+ if +arg+ is +nil+ or cannot be converted by (the original) +Kernel#Integer+. Ignored if +:default+ is specified
|
169
|
-
def self.to_integer arg, base = 0, **options, &block
|
170
|
-
|
171
|
-
IntegerParser_Helper_.to_integer_ arg, base, options, &block
|
172
|
-
end
|
173
|
-
|
174
|
-
# Instance form of ::Xqsr3::Conversion::IntegerParser.to_integer
|
175
|
-
def to_integer base = 0, **options, &block
|
176
|
-
|
177
|
-
IntegerParser_Helper_.to_integer_ self, base, options, &block
|
178
|
-
end
|
179
|
-
|
180
|
-
end # module IntegerParser
|
181
170
|
|
171
|
+
# Instance form of ::Xqsr3::Conversion::IntegerParser.to_integer
|
172
|
+
def to_integer base = 0, **options, &block
|
173
|
+
|
174
|
+
IntegerParser_Helper_.to_integer_ self, base, options, &block
|
175
|
+
end
|
176
|
+
end # module IntegerParser
|
182
177
|
end # module Conversion
|
183
178
|
end # module Xqsr3
|
184
179
|
|
@@ -5,7 +5,7 @@
|
|
5
5
|
# Purpose: Definition of the ExceptionUtilities module
|
6
6
|
#
|
7
7
|
# Created: 12th February 2015
|
8
|
-
# Updated:
|
8
|
+
# Updated: 12th April 2024
|
9
9
|
#
|
10
10
|
# Home: http://github.com/synesissoftware/xqsr3
|
11
11
|
#
|
@@ -45,174 +45,169 @@
|
|
45
45
|
# ######################################################################## #
|
46
46
|
|
47
47
|
|
48
|
-
# ##########################################################
|
49
|
-
# ::Xqsr3::Diagnostics::ExceptionUtilities
|
50
|
-
|
51
48
|
=begin
|
52
49
|
=end
|
53
50
|
|
54
51
|
module Xqsr3
|
55
52
|
module Diagnostics
|
56
53
|
|
57
|
-
module ExceptionUtilities
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
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
|
-
def self.raise_with_options *args, **options
|
123
|
-
|
124
|
-
# special handling in case called indirectly
|
125
|
-
|
126
|
-
called_indirectly = options[:called_indirectly_06d353cb_5a6c_47ca_8dbe_ff76359c7e96]
|
127
|
-
|
128
|
-
case called_indirectly
|
129
|
-
when nil, false
|
130
|
-
|
131
|
-
called_indirectly = 0
|
132
|
-
when true
|
133
|
-
|
134
|
-
called_indirectly = 1
|
135
|
-
when ::Integer
|
136
|
-
|
137
|
-
;
|
138
|
-
else
|
139
|
-
|
140
|
-
abort "indirect-call option (#{called_indirectly}) has invalid type (#{called_indirectly.class})"
|
141
|
-
end
|
54
|
+
module ExceptionUtilities
|
55
|
+
|
56
|
+
# Raises an instance of a named exception that takes options in its
|
57
|
+
# constructor, as in:
|
58
|
+
#
|
59
|
+
# class ArgumentErrorWithOptions < ArgumentError
|
60
|
+
#
|
61
|
+
# def initialize(message = nil, **options)
|
62
|
+
#
|
63
|
+
# . . .
|
64
|
+
# end
|
65
|
+
#
|
66
|
+
# attr_reader :options
|
67
|
+
# end
|
68
|
+
#
|
69
|
+
# begin
|
70
|
+
#
|
71
|
+
# raise_with_options ArgumentErrorWithOptions, "message-1", opt1: :val1, opt2: 'val2'
|
72
|
+
# rescue => x
|
73
|
+
#
|
74
|
+
# $stderr.puts x.options # => {:opt1=>:val1, :opt2=>"val2"}
|
75
|
+
# end
|
76
|
+
#
|
77
|
+
# It can also be used with full compatibility with <tt>Kernel#raise</tt>, as in:
|
78
|
+
#
|
79
|
+
# begin
|
80
|
+
#
|
81
|
+
# raise_with_options ArgumentError, "message-2"
|
82
|
+
# rescue => x
|
83
|
+
#
|
84
|
+
# $stderr.puts x.class # => ArgumenError
|
85
|
+
# $stderr.puts x.message # => "message-2"
|
86
|
+
# end
|
87
|
+
#
|
88
|
+
# and:
|
89
|
+
#
|
90
|
+
# begin
|
91
|
+
#
|
92
|
+
# raise_with_options "message-3"
|
93
|
+
# rescue => x
|
94
|
+
#
|
95
|
+
# $stderr.puts x.class # => RuntimeError
|
96
|
+
# $stderr.puts x.message # => "message-3"
|
97
|
+
# end
|
98
|
+
#
|
99
|
+
# === Parameters
|
100
|
+
#
|
101
|
+
# * +args+ 0 or more arguments
|
102
|
+
# * +options+ An options hash
|
103
|
+
#
|
104
|
+
# === Parameter Interpretation
|
105
|
+
#
|
106
|
+
# If the first argument is a class, it is assumed to be the exception
|
107
|
+
# type that will be raised; if not, RuntimeError is raised as is normal
|
108
|
+
# with Kernel#raise. The remaining arguments are passed - via the
|
109
|
+
# <tt>*</tt> operator - to the class's constructor.
|
110
|
+
#
|
111
|
+
# All options passed to this method are respread - by the
|
112
|
+
# <tt>**</tt> operator - and passed to the underlying
|
113
|
+
# <tt>Kernel#raise</tt> call.
|
114
|
+
#
|
115
|
+
# === Exceptions
|
116
|
+
# - An instance of <tt>arg[0]</tt> if <tt>args[0].is_a?(::Class)</tt>; or
|
117
|
+
# - An instance of +RuntimeError+
|
118
|
+
def self.raise_with_options *args, **options
|
142
119
|
|
143
|
-
|
120
|
+
# special handling in case called indirectly
|
144
121
|
|
122
|
+
called_indirectly = options[:called_indirectly_06d353cb_5a6c_47ca_8dbe_ff76359c7e96]
|
145
123
|
|
146
|
-
|
147
|
-
|
148
|
-
# 1. No options
|
149
|
-
# 2. Non-class and options
|
150
|
-
# 3. Class and options
|
151
|
-
# 3.a Class and options
|
152
|
-
# 3.b Class and options and message
|
153
|
-
# 3.c Class and options and message and call-stack
|
124
|
+
case called_indirectly
|
125
|
+
when nil, false
|
154
126
|
|
155
|
-
|
127
|
+
called_indirectly = 0
|
128
|
+
when true
|
156
129
|
|
157
|
-
|
130
|
+
called_indirectly = 1
|
131
|
+
when ::Integer
|
158
132
|
|
159
|
-
|
133
|
+
;
|
134
|
+
else
|
160
135
|
|
161
|
-
|
136
|
+
abort "indirect-call option (#{called_indirectly}) has invalid type (#{called_indirectly.class})"
|
137
|
+
end
|
138
|
+
|
139
|
+
options.delete :called_indirectly_06d353cb_5a6c_47ca_8dbe_ff76359c7e96 if called_indirectly
|
162
140
|
|
141
|
+
|
142
|
+
# Use cases:
|
143
|
+
#
|
144
|
+
# 1. No options
|
145
|
+
# 2. Non-class and options
|
146
|
+
# 3. Class and options
|
163
147
|
# 3.a Class and options
|
164
148
|
# 3.b Class and options and message
|
165
149
|
# 3.c Class and options and message and call-stack
|
166
150
|
|
151
|
+
class_given = args.size > 0 && args[0].is_a?(::Class)
|
167
152
|
|
168
|
-
|
153
|
+
if class_given && !options.empty?
|
169
154
|
|
170
|
-
|
155
|
+
exception_class_or_instance_or_message_string = args.shift
|
171
156
|
|
172
|
-
|
157
|
+
# 3. Class and options
|
173
158
|
|
174
|
-
|
175
|
-
|
176
|
-
|
159
|
+
# 3.a Class and options
|
160
|
+
# 3.b Class and options and message
|
161
|
+
# 3.c Class and options and message and call-stack
|
177
162
|
|
178
|
-
# now need to trim backtrace
|
179
163
|
|
180
|
-
|
164
|
+
xargs = []
|
181
165
|
|
182
|
-
|
183
|
-
rescue => x
|
166
|
+
xargs << args.shift unless args.empty?
|
184
167
|
|
185
|
-
|
186
|
-
|
187
|
-
|
168
|
+
x = exception_class_or_instance_or_message_string.new(*xargs, **options)
|
169
|
+
|
170
|
+
rargs = []
|
171
|
+
rargs << x
|
172
|
+
rargs += [ nil, args.shift ] unless args.empty?
|
173
|
+
|
174
|
+
# now need to trim backtrace
|
175
|
+
|
176
|
+
begin
|
177
|
+
|
178
|
+
Kernel.raise(*rargs)
|
179
|
+
rescue => x
|
180
|
+
|
181
|
+
bt = x.backtrace
|
182
|
+
(0..called_indirectly).each { bt.shift }
|
183
|
+
Kernel.raise x, x.message, bt
|
184
|
+
end
|
188
185
|
end
|
189
|
-
end
|
190
186
|
|
191
|
-
|
187
|
+
unless options.empty?
|
192
188
|
|
193
|
-
|
189
|
+
# 2. Non-class and options
|
194
190
|
|
195
|
-
|
196
|
-
|
191
|
+
warn "cannot utilise options in raise_with_options when first argument is non-class"
|
192
|
+
else
|
197
193
|
|
198
|
-
|
199
|
-
|
194
|
+
# 1. No options
|
195
|
+
end
|
200
196
|
|
201
197
|
|
202
|
-
|
198
|
+
# now need to trim backtrace
|
203
199
|
|
204
|
-
|
200
|
+
begin
|
205
201
|
|
206
|
-
|
207
|
-
|
202
|
+
Kernel.raise(*args)
|
203
|
+
rescue => x
|
208
204
|
|
209
|
-
|
210
|
-
|
211
|
-
|
205
|
+
bt = x.backtrace
|
206
|
+
(0..called_indirectly).each { bt.shift }
|
207
|
+
Kernel.raise x, x.message, bt
|
208
|
+
end
|
212
209
|
end
|
213
|
-
end
|
214
|
-
end # module ExceptionUtilities
|
215
|
-
|
210
|
+
end # module ExceptionUtilities
|
216
211
|
end # module Diagnostics
|
217
212
|
end # module Xqsr3
|
218
213
|
|