xqsr3 0.38.2 → 0.39.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +15 -6
- data/examples/count_word_frequencies.md +12 -12
- data/examples/count_word_frequencies.rb +4 -4
- data/lib/xqsr3/array_utilities/join_with_or.rb +47 -47
- data/lib/xqsr3/array_utilities.rb +2 -2
- data/lib/xqsr3/command_line_utilities/map_option_string.rb +60 -60
- data/lib/xqsr3/command_line_utilities.rb +2 -2
- data/lib/xqsr3/containers/frequency_map.rb +399 -399
- data/lib/xqsr3/containers/multi_map.rb +448 -448
- data/lib/xqsr3/containers.rb +3 -3
- data/lib/xqsr3/conversion/bool_parser.rb +51 -51
- data/lib/xqsr3/conversion/integer_parser.rb +87 -87
- data/lib/xqsr3/conversion.rb +3 -3
- data/lib/xqsr3/diagnostics/exception_utilities.rb +145 -145
- data/lib/xqsr3/diagnostics/exceptions/with_cause.rb +90 -90
- data/lib/xqsr3/diagnostics/inspect_builder.rb +80 -80
- data/lib/xqsr3/diagnostics.rb +3 -3
- data/lib/xqsr3/doc_.rb +130 -130
- data/lib/xqsr3/extensions/array/join_with_or.rb +9 -9
- data/lib/xqsr3/extensions/enumerable/collect_with_index.rb +12 -12
- data/lib/xqsr3/extensions/enumerable/detect_map.rb +35 -35
- data/lib/xqsr3/extensions/enumerable/unique.rb +29 -28
- data/lib/xqsr3/extensions/hash/deep_transform.rb +1 -1
- data/lib/xqsr3/extensions/hash/except.rb +16 -16
- data/lib/xqsr3/extensions/hash/has_match.rb +10 -10
- data/lib/xqsr3/extensions/hash/match.rb +10 -10
- data/lib/xqsr3/extensions/hash/slice.rb +11 -11
- data/lib/xqsr3/extensions/hash.rb +1 -1
- data/lib/xqsr3/extensions/integer/to_s_grp.rb +118 -0
- data/lib/xqsr3/extensions/integer.rb +3 -0
- data/lib/xqsr3/extensions/io/writelines.rb +28 -28
- data/lib/xqsr3/extensions/kernel/integer.rb +20 -20
- data/lib/xqsr3/extensions/kernel/raise_with_options.rb +8 -8
- data/lib/xqsr3/extensions/kernel.rb +1 -0
- data/lib/xqsr3/extensions/string/ends_with.rb +1 -1
- data/lib/xqsr3/extensions/string/map_option_string.rb +4 -4
- data/lib/xqsr3/extensions/string/nil_if_empty.rb +1 -1
- data/lib/xqsr3/extensions/string/nil_if_whitespace.rb +1 -1
- data/lib/xqsr3/extensions/string/quote_if.rb +1 -1
- data/lib/xqsr3/extensions/string/starts_with.rb +1 -1
- data/lib/xqsr3/extensions/string/to_bool.rb +8 -8
- data/lib/xqsr3/extensions/string/to_symbol.rb +1 -1
- data/lib/xqsr3/extensions/string/truncate.rb +1 -1
- data/lib/xqsr3/extensions/test/unit/assert_eql.rb +10 -7
- data/lib/xqsr3/extensions/test/unit/assert_false.rb +9 -6
- data/lib/xqsr3/extensions/test/unit/assert_not.rb +9 -6
- data/lib/xqsr3/extensions/test/unit/assert_not_eql.rb +10 -7
- data/lib/xqsr3/extensions/test/unit/assert_raise_with_message.rb +61 -61
- data/lib/xqsr3/extensions/test/unit/assert_subclass_of.rb +10 -7
- data/lib/xqsr3/extensions/test/unit/assert_superclass_of.rb +10 -7
- data/lib/xqsr3/extensions/test/unit/assert_true.rb +9 -6
- data/lib/xqsr3/extensions/test/unit/assert_type_has_instance_methods.rb +39 -36
- data/lib/xqsr3/extensions.rb +5 -5
- data/lib/xqsr3/hash_utilities/deep_transform.rb +65 -65
- data/lib/xqsr3/hash_utilities/key_matching.rb +77 -77
- data/lib/xqsr3/hash_utilities.rb +3 -3
- data/lib/xqsr3/internal_/test_unit_version_.rb +83 -83
- data/lib/xqsr3/io/writelines.rb +122 -120
- data/lib/xqsr3/quality/parameter_checking.rb +445 -445
- data/lib/xqsr3/quality.rb +2 -2
- data/lib/xqsr3/string_utilities/ends_with.rb +59 -59
- data/lib/xqsr3/string_utilities/nil_if_empty.rb +28 -28
- data/lib/xqsr3/string_utilities/nil_if_whitespace.rb +29 -29
- data/lib/xqsr3/string_utilities/quote_if.rb +50 -50
- data/lib/xqsr3/string_utilities/starts_with.rb +59 -59
- data/lib/xqsr3/string_utilities/to_symbol.rb +75 -75
- data/lib/xqsr3/string_utilities/truncate.rb +55 -55
- data/lib/xqsr3/string_utilities.rb +8 -8
- data/lib/xqsr3/version.rb +12 -12
- data/test/performance/frequency_map.rb +12 -12
- data/test/scratch/test_assert_raise_with_message.rb +11 -11
- data/test/unit/array_utilities/tc_join_with_or.rb +140 -140
- data/test/unit/command_line_utilities/tc_map_option_string.rb +18 -18
- data/test/unit/containers/tc_frequency_map.rb +591 -591
- data/test/unit/containers/tc_multi_map.rb +558 -558
- data/test/unit/conversion/tc_integer_parser.rb +72 -72
- data/test/unit/conversion/tc_to_bool.rb +25 -25
- data/test/unit/diagnostics/exceptions/tc_with_cause.rb +165 -165
- data/test/unit/diagnostics/tc_exception_utilities.rb +156 -156
- data/test/unit/extensions/enumerable/tc_collect_with_index.rb +14 -14
- data/test/unit/extensions/enumerable/tc_detect_map.rb +20 -20
- data/test/unit/extensions/enumerable/tc_unique.rb +34 -34
- data/test/unit/extensions/hash/tc_deep_transform.rb +22 -22
- data/test/unit/extensions/hash/tc_except.rb +28 -28
- data/test/unit/extensions/hash/tc_slice.rb +13 -13
- data/test/unit/extensions/integer/tc_to_s_grp.rb +60 -0
- data/test/unit/extensions/integer/ts_all.rb +12 -0
- data/test/unit/extensions/io/tc_writelines.rb +149 -77
- data/test/unit/extensions/kernel/tc_integer.rb +75 -75
- data/test/unit/extensions/kernel/tc_raise_with_options.rb +155 -155
- data/test/unit/extensions/object/tc_inspect.rb +50 -50
- data/test/unit/extensions/string/tc_bool.tb +24 -24
- data/test/unit/extensions/string/tc_ends_with.rb +53 -53
- data/test/unit/extensions/string/tc_map_option_string.rb +15 -15
- data/test/unit/extensions/string/tc_nil_if_empty.rb +8 -8
- data/test/unit/extensions/string/tc_nil_if_whitespace.rb +8 -8
- data/test/unit/extensions/string/tc_quote_if.rb +18 -18
- data/test/unit/extensions/string/tc_starts_with.rb +53 -53
- data/test/unit/extensions/string/tc_to_symbol.rb +26 -26
- data/test/unit/extensions/string/tc_truncate.rb +18 -18
- data/test/unit/extensions/test/unit/tc_assert_raise_with_message.rb +16 -16
- data/test/unit/extensions/test/unit/tc_assert_subclass_of.rb +9 -9
- data/test/unit/extensions/test/unit/tc_assert_superclass_of.rb +9 -9
- data/test/unit/hash_utilities/tc_has_match.rb +70 -70
- data/test/unit/hash_utilities/tc_match.rb +83 -83
- data/test/unit/io/tc_writelines.rb +166 -106
- data/test/unit/quality/tc_parameter_checking.rb +389 -389
- data/test/unit/string_utilities/tc_truncate.rb +27 -27
- data/test/unit/tc_version.rb +15 -15
- metadata +6 -2
data/lib/xqsr3/io/writelines.rb
CHANGED
@@ -5,13 +5,13 @@
|
|
5
5
|
# Purpose: Adds a writelines() method to the IO module
|
6
6
|
#
|
7
7
|
# Created: 13th April 2007
|
8
|
-
# Updated:
|
8
|
+
# Updated: 12th April 2024
|
9
9
|
#
|
10
10
|
# Home: http://github.com/synesissoftware/xqsr3
|
11
11
|
#
|
12
12
|
# Author: Matthew Wilson
|
13
13
|
#
|
14
|
-
# Copyright (c) 2019-
|
14
|
+
# Copyright (c) 2019-2024, Matthew Wilson and Synesis Information Systems
|
15
15
|
# Copyright (c) 2007-2019, Matthew Wilson and Synesis Software
|
16
16
|
# All rights reserved.
|
17
17
|
#
|
@@ -56,174 +56,176 @@ require 'xqsr3/quality/parameter_checking'
|
|
56
56
|
module Xqsr3
|
57
57
|
module IO
|
58
58
|
|
59
|
-
|
60
|
-
|
61
|
-
|
59
|
+
private
|
60
|
+
# @!visibility private
|
61
|
+
module WriteLine_Constants_ #:nodoc: all
|
62
62
|
|
63
|
-
|
64
|
-
|
63
|
+
NUMBER_OF_LINES_TO_EXAMINE = 20
|
64
|
+
end # module WriteLine_Constants_
|
65
65
|
|
66
|
-
|
66
|
+
private
|
67
67
|
|
68
|
-
|
69
|
-
|
68
|
+
# @!visibility private
|
69
|
+
def self.write_to_target_ target, contents, line_separator, column_separator, no_last_eol # :nodoc:
|
70
70
|
|
71
|
-
|
71
|
+
=begin
|
72
|
+
$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}', no_last_eol(#{no_last_eol.class})=#{no_last_eol})" if $DEBUG
|
73
|
+
=end
|
72
74
|
|
73
|
-
|
75
|
+
if no_last_eol
|
74
76
|
|
75
|
-
|
77
|
+
first = true
|
76
78
|
|
77
|
-
|
79
|
+
if contents.instance_of? ::Hash
|
78
80
|
|
79
|
-
|
81
|
+
contents.each do |k, v|
|
80
82
|
|
81
|
-
|
83
|
+
target << line_separator unless first
|
82
84
|
|
83
|
-
|
85
|
+
target << "#{k}#{column_separator}#{v}"
|
84
86
|
|
85
|
-
|
86
|
-
|
87
|
-
|
87
|
+
first = false
|
88
|
+
end
|
89
|
+
else
|
88
90
|
|
89
|
-
|
91
|
+
contents.each do |element|
|
90
92
|
|
91
|
-
|
93
|
+
target << line_separator unless first
|
92
94
|
|
93
|
-
|
95
|
+
target << "#{element}"
|
94
96
|
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
97
|
+
first = false
|
98
|
+
end
|
99
|
+
end
|
100
|
+
else
|
99
101
|
|
100
|
-
|
102
|
+
if contents.instance_of? ::Hash
|
101
103
|
|
102
|
-
|
104
|
+
contents.each do |k, v|
|
103
105
|
|
104
|
-
|
105
|
-
|
106
|
-
|
106
|
+
target << "#{k}#{column_separator}#{v}#{line_separator}"
|
107
|
+
end
|
108
|
+
else
|
107
109
|
|
108
|
-
|
110
|
+
contents.each do |element|
|
109
111
|
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
112
|
+
target << "#{element}#{line_separator}"
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
114
116
|
|
115
|
-
|
116
|
-
|
117
|
+
contents.size
|
118
|
+
end
|
117
119
|
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
120
|
+
# This function checks to see if any part of the entries contains an
|
121
|
+
# embedded eol, in which case the empty string is returned to force no
|
122
|
+
# (additional) separator will be used. Otherwise, it returns "\n" to
|
123
|
+
# ensure that that is used.
|
124
|
+
#
|
125
|
+
# @!visibility private
|
126
|
+
def self.deduce_line_separator_ contents, eol_lookahead_limit # :nodoc:
|
125
127
|
|
126
|
-
|
128
|
+
if contents.instance_of? ::Hash
|
127
129
|
|
128
|
-
|
130
|
+
contents.each_with_index do |k, v, index|
|
129
131
|
|
130
|
-
|
132
|
+
if eol_lookahead_limit && eol_lookahead_limit == index
|
131
133
|
|
132
|
-
|
133
|
-
|
134
|
+
break
|
135
|
+
else
|
134
136
|
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
137
|
+
return '' if v.to_s.include? "\n"
|
138
|
+
return '' if k.to_s.include? "\n"
|
139
|
+
end
|
140
|
+
end
|
141
|
+
else
|
140
142
|
|
141
|
-
|
143
|
+
contents.each_with_index do |element, index|
|
142
144
|
|
143
|
-
|
145
|
+
if eol_lookahead_limit && eol_lookahead_limit == index
|
144
146
|
|
145
|
-
|
146
|
-
|
147
|
+
break
|
148
|
+
else
|
147
149
|
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
150
|
+
return '' if element.to_s.include? "\n"
|
151
|
+
end
|
152
|
+
end
|
153
|
+
end
|
152
154
|
|
153
|
-
|
154
|
-
|
155
|
+
"\n"
|
156
|
+
end
|
155
157
|
|
156
|
-
|
158
|
+
public
|
157
159
|
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
160
|
+
# Writes the contents to the target, subject to the options
|
161
|
+
#
|
162
|
+
# === Signature
|
163
|
+
#
|
164
|
+
# * *Parameters:*
|
165
|
+
# - +target+ The target of the write, which may be a string containing the path or a stream instance that supports write
|
166
|
+
# - +contents+ The contents to be write, which may be a +Hash+, or an +Array+, or a +String+ containing delimited fields
|
167
|
+
# - +options+ An options hash, containing any of the following options
|
168
|
+
#
|
169
|
+
# * *Options:*
|
170
|
+
# - +:column_separator+ {optional} The column separator, to be applied between each field in the case where +contents+ is a +Hash+.
|
171
|
+
# - +: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.
|
172
|
+
# - +: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+).
|
173
|
+
# - +:no_last_eol+ {optional} If present and _truey_, causes suppression of the addition of the +:line_separator+ on the last line.
|
174
|
+
#
|
175
|
+
# === Return
|
176
|
+
#
|
177
|
+
# The number of entries in +contents+
|
178
|
+
def self.writelines target, contents, options = {}
|
177
179
|
|
178
|
-
|
180
|
+
# validate parameters
|
179
181
|
|
180
|
-
|
182
|
+
::Xqsr3::Quality::ParameterChecking.check_parameter(target, 'target', allow_nil: false) do |v|
|
181
183
|
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
184
|
+
raise TypeError, "#{self}#writeline() 'target' parameter must be a #{::String} or respond to <<" unless ::String === v || v.respond_to?(:<<)
|
185
|
+
true
|
186
|
+
end
|
187
|
+
::Xqsr3::Quality::ParameterChecking.check_parameter(contents, 'contents', allow_nil: false, types: [ ::String, ::Hash, ::Array ])
|
186
188
|
|
187
|
-
|
189
|
+
# process parameters
|
188
190
|
|
189
|
-
|
191
|
+
if contents.instance_of? String
|
190
192
|
|
191
|
-
|
193
|
+
if contents.include? "\n"
|
192
194
|
|
193
|
-
|
194
|
-
|
195
|
+
contents = contents.split(/\r?\n/, -1)
|
196
|
+
else
|
195
197
|
|
196
|
-
|
197
|
-
|
198
|
-
|
198
|
+
contents = [ contents ]
|
199
|
+
end
|
200
|
+
end
|
199
201
|
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
202
|
+
options ||= {}
|
203
|
+
eol_lookahead_limit = options[:eol_lookahead_limit] || WriteLine_Constants_::NUMBER_OF_LINES_TO_EXAMINE
|
204
|
+
column_separator = options[:column_separator] || ''
|
205
|
+
no_last_eol = options[:no_last_eol] || false
|
206
|
+
line_separator = nil
|
207
|
+
line_separator ||= options[:line_separator]
|
208
|
+
line_separator ||= self.deduce_line_separator_(contents, eol_lookahead_limit) unless !eol_lookahead_limit.kind_of?(::Integer) || 0 == eol_lookahead_limit
|
209
|
+
line_separator ||= "\n"
|
208
210
|
|
209
|
-
|
211
|
+
if not contents.kind_of? ::Enumerable and not contents.instance_of? ::Hash
|
210
212
|
|
211
|
-
|
212
|
-
|
213
|
+
raise ArgumentError, "writelines() must be passed a #{::String}, or a #{::Hash}, or an #{::Enumerable} (or derived)"
|
214
|
+
end
|
213
215
|
|
214
|
-
|
216
|
+
# do the writing
|
215
217
|
|
216
|
-
|
218
|
+
if ::String === target
|
217
219
|
|
218
|
-
|
220
|
+
File.open(target, "w") do |io|
|
219
221
|
|
220
|
-
|
221
|
-
|
222
|
-
|
222
|
+
self.write_to_target_ io, contents, line_separator, column_separator, no_last_eol
|
223
|
+
end
|
224
|
+
else
|
223
225
|
|
224
|
-
|
225
|
-
|
226
|
-
|
226
|
+
self.write_to_target_ target, contents, line_separator, column_separator, no_last_eol
|
227
|
+
end
|
228
|
+
end # writelines
|
227
229
|
end # module IO
|
228
230
|
end # module Xqsr3
|
229
231
|
|