xqsr3 0.11.1 → 0.12.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b23c1bd7f01f0540f8cd11ba2a619fa6d2fb334c
4
- data.tar.gz: 7b79521e19d9f76bc869a83c8003cbddec3ad576
3
+ metadata.gz: eed03c5b9c2f91c7de5bd5296e8f62a8476b076f
4
+ data.tar.gz: 8c7dca9f8d9ded07ef666f6b648db4772830edc1
5
5
  SHA512:
6
- metadata.gz: 1c53ef621526a41f9f47afe099d1ff8df7dc775700a2974ecab2834a317f31fd012c98c679aa1499fb2ce696daf9f257ddf64424fa232517a6df0cff3383c212
7
- data.tar.gz: bc62c09071304cb569e896217f719337038c5e888882b35ecdec4df67eb4c89202e8f6d896d12d9788918428888297fc87895b2b21dd998a737fb369008e56e1
6
+ metadata.gz: fb1f52836cc8d7daf89b2b04bbd9034ab643a635bf898abec355274d707acd1634161150ed0094fb03c217eb0fb6be219c942f45b6fb35af190bff5948e0916b
7
+ data.tar.gz: d840b9ee19383973b2f5251319f301e70ed1066197d71551e88030b1f0d50863eb39cbcf65c68737dc853926e6cec6f338023cc1ea364cebd8e4e55c6ff78eeb
@@ -0,0 +1,120 @@
1
+
2
+ # ######################################################################## #
3
+ # File: lib/xqsr3/conversion/bool_parser.rb
4
+ #
5
+ # Purpose: Definition of the ::Xqsr3::Conversion::BoolParser
6
+ # module
7
+ #
8
+ # Created: 3rd June 2017
9
+ # Updated: 7th June 2017
10
+ #
11
+ # Home: http://github.com/synesissoftware/xqsr3
12
+ #
13
+ # Author: Matthew Wilson
14
+ #
15
+ # Copyright (c) 2017, Matthew Wilson and Synesis Software
16
+ # All rights reserved.
17
+ #
18
+ # Redistribution and use in source and binary forms, with or without
19
+ # modification, are permitted provided that the following conditions are
20
+ # met:
21
+ #
22
+ # * Redistributions of source code must retain the above copyright notice,
23
+ # this list of conditions and the following disclaimer.
24
+ #
25
+ # * Redistributions in binary form must reproduce the above copyright
26
+ # notice, this list of conditions and the following disclaimer in the
27
+ # documentation and/or other materials provided with the distribution.
28
+ #
29
+ # * Neither the names of the copyright holder nor the names of its
30
+ # contributors may be used to endorse or promote products derived from
31
+ # this software without specific prior written permission.
32
+ #
33
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
34
+ # IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
35
+ # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
36
+ # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
37
+ # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
38
+ # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
39
+ # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
40
+ # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
41
+ # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
42
+ # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
43
+ # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44
+ #
45
+ # ######################################################################## #
46
+
47
+
48
+ # ##########################################################
49
+ # ::Xqsr3::Conversion::ToBool
50
+
51
+ =begin
52
+ =end
53
+
54
+ module Xqsr3
55
+ module Conversion
56
+
57
+ module BoolParser
58
+
59
+ private
60
+ def self.matches_to_ s, expr
61
+
62
+ case expr
63
+ when ::Regexp
64
+ return expr =~ s
65
+ else
66
+ return s == expr
67
+ end
68
+ end
69
+
70
+ public
71
+
72
+ DEFAULT_TRUE_VALUES = [ /true/i, '1' ]
73
+ DEFAULT_FALSE_VALUES = [ /false/i, '0' ]
74
+
75
+ # Attempts to parse the given String to a Boolean value, based on the
76
+ # given +options+
77
+ #
78
+ # === Signature
79
+ #
80
+ # * *Parameters*:
81
+ # - +options+:: An options hash, containing any of the following options
82
+ #
83
+ # * *Options*:
84
+ # - +:false_values+:: [::Array] An array of strings or regular
85
+ # expressions against which to match for false value. Defaults to
86
+ # +DEFAULT_FALSE_VALUES+
87
+ # - +:true_values+:: [::Array] An array of strings or regular
88
+ # expressions against which to match for true value. Defaults to
89
+ # +DEFAULT_TRUE_VALUES+
90
+ # - +:default_value+:: An object to be returned if matching fails.
91
+ # Defaults to +nil+
92
+ # - +:false_value+:: An object to be returned if matching succeeds to
93
+ # match against +:false_values+.
94
+ # - +:true_value+:: An object to be returned if matching succeeds to
95
+ # match against +:true_values+.
96
+ def self.to_bool s, **options
97
+
98
+ true_values = options[:true_values] || DEFAULT_TRUE_VALUES
99
+ true_values = [ true_values ] unless true_values.is_a? ::Array
100
+ false_values = options[:false_values] || DEFAULT_FALSE_VALUES
101
+ false_values = [ false_values ] unless false_values.is_a? ::Array
102
+ default_value = options[:default] || nil
103
+ true_value = options[:true] || true
104
+ false_value = options[:false] || false
105
+
106
+ return true_value if true_values.any? { |v| self.matches_to_ s, v }
107
+ return false_value if false_values.any? { |v| self.matches_to_ s, v }
108
+
109
+ return default_value
110
+ end
111
+
112
+ end # module BoolParser
113
+
114
+ end # module Conversion
115
+ end # module Xqsr3
116
+
117
+ # ############################## end of file ############################# #
118
+
119
+
120
+
@@ -0,0 +1,12 @@
1
+
2
+ require 'xqsr3/conversion/bool_parser'
3
+
4
+ class String
5
+
6
+ def to_bool **options
7
+
8
+ return ::Xqsr3::Conversion::BoolParser.to_bool self, **options
9
+ end
10
+ end # class String
11
+
12
+
@@ -3,5 +3,6 @@ require 'xqsr3/extensions/string/ends_with'
3
3
  require 'xqsr3/extensions/string/map_option_string'
4
4
  require 'xqsr3/extensions/string/quote_if'
5
5
  require 'xqsr3/extensions/string/starts_with'
6
+ require 'xqsr3/extensions/string/to_bool'
6
7
  require 'xqsr3/extensions/string/to_symbol'
7
8
 
@@ -0,0 +1,19 @@
1
+
2
+ module Test
3
+ module Unit
4
+
5
+ module Assertions
6
+
7
+ unless respond_to? :assert_false
8
+
9
+ def assert_false(expression, failure_message = '')
10
+
11
+ assert ::FalseClass === (expression), failure_message
12
+ end
13
+ end
14
+
15
+ end # class Assertions
16
+ end # module Unit
17
+ end # module Test
18
+
19
+
@@ -0,0 +1,20 @@
1
+
2
+ module Test
3
+ module Unit
4
+
5
+ module Assertions
6
+
7
+ unless respond_to? :assert_true
8
+
9
+ def assert_true(expression, failure_message = '')
10
+
11
+ assert ::TrueClass === (expression), failure_message
12
+ end
13
+ end
14
+
15
+ end # class Assertions
16
+ end # module Unit
17
+ end # module Test
18
+
19
+
20
+
@@ -1,5 +1,7 @@
1
1
 
2
2
  require 'xqsr3/extensions/test/unit/assert_eql'
3
+ require 'xqsr3/extensions/test/unit/assert_false'
3
4
  require 'xqsr3/extensions/test/unit/assert_not'
4
5
  require 'xqsr3/extensions/test/unit/assert_not_eql'
6
+ require 'xqsr3/extensions/test/unit/assert_true'
5
7
 
data/lib/xqsr3/version.rb CHANGED
@@ -50,7 +50,7 @@
50
50
  module Xqsr3
51
51
 
52
52
  # Current version of the Xqsr3 library
53
- VERSION = '0.11.1'
53
+ VERSION = '0.12.1'
54
54
 
55
55
  private
56
56
  VERSION_PARTS_ = VERSION.split(/[.]/).collect { |n| n.to_i } # :nodoc:
@@ -0,0 +1,46 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $:.unshift File.join(File.dirname(__FILE__), '../../../lib')
4
+
5
+ require 'xqsr3/conversion/bool_parser'
6
+
7
+ require 'xqsr3/extensions/test/unit'
8
+ require 'test/unit'
9
+
10
+ include ::Xqsr3::Conversion
11
+
12
+ class Test_Xqsr3_ConversionMultiMap < Test::Unit::TestCase
13
+
14
+ def test_parse_normal
15
+
16
+ assert_true BoolParser.to_bool 'true'
17
+ assert_false BoolParser.to_bool 'false'
18
+ end
19
+
20
+ def test_parse_custom_true_false
21
+
22
+ true_values = [ 'affirmative', 'yup' ]
23
+ false_values = [ 'negative', 'nope' ]
24
+
25
+ assert_true BoolParser.to_bool 'affirmative', true_values: true_values, false_values: false_values
26
+ assert_true BoolParser.to_bool 'yup', true_values: true_values, false_values: false_values
27
+ assert_nil BoolParser.to_bool 'true', true_values: true_values, false_values: false_values
28
+ assert_false BoolParser.to_bool 'negative', true_values: true_values, false_values: false_values
29
+ assert_false BoolParser.to_bool 'nope', true_values: true_values, false_values: false_values
30
+ assert_nil BoolParser.to_bool 'false', true_values: true_values, false_values: false_values
31
+ end
32
+
33
+ def test_parse_custom_true_false_regexes
34
+
35
+ true_values = [ /affirm/, /yup/ ]
36
+ false_values = [ 'negative', /no/ ]
37
+
38
+ assert_true BoolParser.to_bool 'affirmative', true_values: true_values, false_values: false_values
39
+ assert_true BoolParser.to_bool 'yup', true_values: true_values, false_values: false_values
40
+ assert_nil BoolParser.to_bool 'true', true_values: true_values, false_values: false_values
41
+ assert_false BoolParser.to_bool 'negative', true_values: true_values, false_values: false_values
42
+ assert_false BoolParser.to_bool 'nope', true_values: true_values, false_values: false_values
43
+ assert_nil BoolParser.to_bool 'false', true_values: true_values, false_values: false_values
44
+ end
45
+ end
46
+
@@ -0,0 +1,13 @@
1
+ #! /usr/bin/env ruby
2
+ #
3
+ # executes all other tests
4
+
5
+ this_file = File.expand_path(__FILE__)
6
+ this_dir = File.expand_path(File.dirname(__FILE__))
7
+
8
+ # all tc_*rb in current directory
9
+ Dir[File.join(this_dir, 'tc_*rb')].each { |file| require file }
10
+
11
+ # all ts_*rb in immediate sub-directories
12
+ Dir[File.join(this_dir, '*', 'ts_*rb')].each { |file| require file }
13
+
File without changes
@@ -0,0 +1,44 @@
1
+ #! /usr/bin/env ruby
2
+
3
+ $:.unshift File.join(File.dirname(__FILE__), '../../../../lib')
4
+
5
+ require 'xqsr3/extensions/string/to_bool'
6
+
7
+ require 'xqsr3/extensions/test/unit'
8
+ require 'test/unit'
9
+
10
+ class Test_String_to_bool < Test::Unit::TestCase
11
+
12
+ def test_parse_normal
13
+
14
+ assert_true 'true'.to_bool
15
+ assert_false 'false'.to_bool
16
+ end
17
+
18
+ def test_parse_custom_true_false
19
+
20
+ true_values = [ 'affirmative', 'yup' ]
21
+ false_values = [ 'negative', 'nope' ]
22
+
23
+ assert_true 'affirmative'.to_bool true_values: true_values, false_values: false_values
24
+ assert_true 'yup'.to_bool true_values: true_values, false_values: false_values
25
+ assert_nil 'true'.to_bool true_values: true_values, false_values: false_values
26
+ assert_false 'negative'.to_bool true_values: true_values, false_values: false_values
27
+ assert_false 'nope'.to_bool true_values: true_values, false_values: false_values
28
+ assert_nil 'false'.to_bool true_values: true_values, false_values: false_values
29
+ end
30
+
31
+ def test_parse_custom_true_false_regexes
32
+
33
+ true_values = [ /affirm/, /yup/ ]
34
+ false_values = [ 'negative', /no/ ]
35
+
36
+ assert_true 'affirmative'.to_bool true_values: true_values, false_values: false_values
37
+ assert_true 'yup'.to_bool true_values: true_values, false_values: false_values
38
+ assert_nil 'true'.to_bool true_values: true_values, false_values: false_values
39
+ assert_false 'negative'.to_bool true_values: true_values, false_values: false_values
40
+ assert_false 'nope'.to_bool true_values: true_values, false_values: false_values
41
+ assert_nil 'false'.to_bool true_values: true_values, false_values: false_values
42
+ end
43
+ end
44
+
File without changes
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: xqsr3
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.11.1
4
+ version: 0.12.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matt Wilson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-06-07 00:00:00.000000000 Z
11
+ date: 2017-06-08 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: |
14
14
  eXtensions by fine Quantum for Standard Ruby and 3rd-party libraries is a
@@ -24,6 +24,7 @@ files:
24
24
  - lib/xqsr3/command_line_utilities/map_option_string.rb
25
25
  - lib/xqsr3/containers/frequency_map.rb
26
26
  - lib/xqsr3/containers/multi_map.rb
27
+ - lib/xqsr3/conversion/bool_parser.rb
27
28
  - lib/xqsr3/diagnostics/exception_utilities.rb
28
29
  - lib/xqsr3/doc_.rb
29
30
  - lib/xqsr3/extensions/enumerable.rb
@@ -39,11 +40,14 @@ files:
39
40
  - lib/xqsr3/extensions/string/map_option_string.rb
40
41
  - lib/xqsr3/extensions/string/quote_if.rb
41
42
  - lib/xqsr3/extensions/string/starts_with.rb
43
+ - lib/xqsr3/extensions/string/to_bool.rb
42
44
  - lib/xqsr3/extensions/string/to_symbol.rb
43
45
  - lib/xqsr3/extensions/test/unit.rb
44
46
  - lib/xqsr3/extensions/test/unit/assert_eql.rb
47
+ - lib/xqsr3/extensions/test/unit/assert_false.rb
45
48
  - lib/xqsr3/extensions/test/unit/assert_not.rb
46
49
  - lib/xqsr3/extensions/test/unit/assert_not_eql.rb
50
+ - lib/xqsr3/extensions/test/unit/assert_true.rb
47
51
  - lib/xqsr3/hash_utilities/deep_transform.rb
48
52
  - lib/xqsr3/io/writelines.rb
49
53
  - lib/xqsr3/quality.rb
@@ -58,6 +62,8 @@ files:
58
62
  - test/unit/containers/tc_frequency_map.rb
59
63
  - test/unit/containers/tc_multi_map.rb
60
64
  - test/unit/containers/ts_all.rb
65
+ - test/unit/conversion/tc_to_bool.rb
66
+ - test/unit/conversion/ts_all.rb
61
67
  - test/unit/diagnostics/tc_exception_utilities.rb
62
68
  - test/unit/diagnostics/ts_all.rb
63
69
  - test/unit/extensions/enumerable/tc_collect_with_index.rb
@@ -69,6 +75,7 @@ files:
69
75
  - test/unit/extensions/io/ts_all.rb
70
76
  - test/unit/extensions/kernel/tc_raise_with_options.rb
71
77
  - test/unit/extensions/kernel/ts_all.rb
78
+ - test/unit/extensions/string/tc_bool.tb
72
79
  - test/unit/extensions/string/tc_ends_with.rb
73
80
  - test/unit/extensions/string/tc_map_option_string.rb
74
81
  - test/unit/extensions/string/tc_quote_if.rb