util 0.2.0 → 0.4.0

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.
@@ -0,0 +1,7 @@
1
+ <html>
2
+ <head></head>
3
+ <body>
4
+ <h1>Ac&ograve;&rsquo;s un b&eacute;u drap&eacute;u&nbsp;!</h1>
5
+ <p><img src="https://gitlab.com/uploads/-/system/user/avatar/5582173/avatar.png" /></p>
6
+ </body>
7
+ </html>
@@ -0,0 +1,180 @@
1
+ results = [:ok, 42, nil, :ok, nil, nil, :err, :nope, nil]
2
+ results += [:err, nil, nil, :err, :nope, 'Temp#my_func']
3
+ $test.register 'result-new', results do ||
4
+ require 'util/result'
5
+ res = []
6
+ success = Util::Result.ok 42
7
+ res << success.instance_variable_get('@type')
8
+ res << success.instance_variable_get('@content')
9
+ res << success.instance_variable_get('@where')
10
+
11
+ success = Util::Result.ok
12
+ res << success.instance_variable_get('@type')
13
+ res << success.instance_variable_get('@content')
14
+ res << success.instance_variable_get('@where')
15
+
16
+ failure = Util::Result.err :nope
17
+ res << failure.instance_variable_get('@type')
18
+ res << failure.instance_variable_get('@content')
19
+ res << failure.instance_variable_get('@where')
20
+
21
+ failure = Util::Result.err
22
+ res << failure.instance_variable_get('@type')
23
+ res << failure.instance_variable_get('@content')
24
+ res << failure.instance_variable_get('@where')
25
+
26
+ class Temp
27
+ def my_func
28
+ Util::Result.err :nope, self
29
+ end
30
+ end
31
+
32
+ failure = Temp.new.my_func
33
+ res << failure.instance_variable_get('@type')
34
+ res << failure.instance_variable_get('@content')
35
+ res << failure.instance_variable_get('@where')
36
+
37
+ res
38
+ end
39
+
40
+ results = [true, false, false, false, true, false, false, true]
41
+ $test.register 'result-compare', results do ||
42
+ require 'util/result'
43
+ [ Util::Result.ok == Util::Result.ok,
44
+ Util::Result.ok == Util::Result.err,
45
+ Util::Result.ok == Util::Result.ok(42),
46
+ Util::Result.ok(42.0) == Util::Result.ok(42),
47
+ Util::Result.ok(42) == Util::Result.ok(42),
48
+ Util::Result.ok(42) == Util::Result.err(79),
49
+ Util::Result.ok(Object.new) == Util::Result.err(Object.new),
50
+ Util::Result.ok != Util::Result.err
51
+ ]
52
+ end
53
+
54
+ results = [true, false, false, true, false, true]
55
+ $test.register 'result-type-determiner', results do ||
56
+ require 'util/result'
57
+ class Temp
58
+ def self.divide num, denom
59
+ return Util::Result.err :div0, self if denom == 0
60
+ Util::Result.ok num / denom
61
+ end
62
+ end
63
+ [ Util::Result.ok(42).ok?,
64
+ Util::Result.ok(42).err?,
65
+ Util::Result.err(:nope, self).ok?,
66
+ Util::Result.err(:nope, self).err?,
67
+ Temp.divide(42, 7).err?,
68
+ Temp.divide(42, 0).err?,
69
+ ]
70
+ end
71
+
72
+ results = [6, 6, :nope, :div0, :div0, :nope]
73
+ $test.register 'result-retrieve-value', results do ||
74
+ require 'util/result'
75
+ class Temp
76
+ def self.divide num, denom
77
+ return Util::Result.err :div0, self if denom == 0
78
+ Util::Result.ok num / denom
79
+ end
80
+ end
81
+ [ Temp.divide(42, 7).value,
82
+ Temp.divide(42, 7).value_or(:nope),
83
+ Temp.divide(42, 0).value_or(:nope),
84
+ Temp.divide(42, 0).error,
85
+ Temp.divide(42, 0).error_or(:nope),
86
+ Temp.divide(42, 7).error_or(:nope),
87
+ ]
88
+ end
89
+
90
+ $test.register 'result-value-on-failure', ArgumentError do ||
91
+ require 'util/result'
92
+ class Temp
93
+ def self.divide num, denom
94
+ return Util::Result.err :div0, self if denom == 0
95
+ Util::Result.ok num / denom
96
+ end
97
+ end
98
+ Temp.divide(42, 0).value
99
+ end
100
+
101
+ $test.register 'result-error-on-success', ArgumentError do ||
102
+ require 'util/result'
103
+ class Temp
104
+ def self.divide num, denom
105
+ return Util::Result.err :div0, self if denom == 0
106
+ Util::Result.ok num / denom
107
+ end
108
+ end
109
+ Temp.divide(42, 7).error
110
+ end
111
+
112
+ results = ['[+] ', '[-] ', '[+] 6', '[- Temp.divide] div0']
113
+ $test.register 'result-to_s', results do ||
114
+ require 'util/result'
115
+ class Temp
116
+ def self.divide num, denom
117
+ return Util::Result.err :div0, self if denom == 0
118
+ Util::Result.ok num / denom
119
+ end
120
+ end
121
+ [ Util::Result.ok.to_s,
122
+ Util::Result.err.to_s,
123
+ Temp.divide(42, 7).to_s,
124
+ Temp.divide(42, 0).to_s,
125
+ ]
126
+ end
127
+
128
+ def next_num num
129
+ num.content + 1
130
+ end
131
+
132
+ results = [121, 122, 133, 133, :div0, 'Num#divide', 91, :div0, 'Num#divide']
133
+ results += [NameError, 'undefined method `substract\' for class `Num\'']
134
+ results += ['Util::Result#bind_common']
135
+ $test.register 'result-binding', results do ||
136
+ require 'util/result'
137
+
138
+ class Num
139
+ attr_reader :content
140
+ def initialize num
141
+ @content = num.to_f
142
+ end
143
+
144
+ def self.add first, second
145
+ Util::Result.ok Num.new(first.content + second.content)
146
+ end
147
+
148
+ def == num
149
+ return false unless num.respond_to?(:to_f)
150
+ @content.to_f == num.to_f
151
+ end
152
+
153
+ def add num
154
+ Util::Result.ok Num.new(@content + num.to_f)
155
+ end
156
+
157
+ def divide denom
158
+ return Util::Result.err :div0, self if denom == 0
159
+ Util::Result.ok Num.new(@content / denom.to_f)
160
+ end
161
+ end
162
+
163
+ base = Num.add(Num.new(42), Num.new(79))
164
+ [ base.value_or(:nope),
165
+ base.bind(:next_num).value_or(:nope), # Top-level method
166
+ base.bindm(:add, 12).value_or(:nope), # Instance method
167
+ base.bindcm(:add, Num.new(12)).value_or(:nope), # Class method
168
+ base.bindm(:divide, 0).error, # Failure
169
+ base.bindm(:divide, 0).where,
170
+ base.bindm(:add, 12).bindcm(:add, Num.new(-42)).value_or(:nope),
171
+ # Chain multiple binds
172
+ base.bindm(:add, 12).bindm(:divide, 0).bindcm(:add, Num.new(-42)).error,
173
+ base.bindm(:add, 12).bindm(:divide, 0).bindcm(:add, Num.new(-42)).where,
174
+ # Chain multiple binds with an error in between
175
+ base.bindm(:substract, 12).error.class,
176
+ base.bindm(:substract, 12).error.message,
177
+ base.bindm(:substract, 12).where,
178
+ # The method does not exist, hence raises an exception
179
+ ]
180
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: util
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Guillaume Lestringant
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-04-08 00:00:00.000000000 Z
11
+ date: 2020-04-12 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: |2
14
14
  A collection of diverse simple utilities without much anything to do
@@ -26,16 +26,21 @@ files:
26
26
  - LICENSES.md
27
27
  - README.md
28
28
  - lib/util.rb
29
- - lib/util/arg.rb
29
+ - lib/util/args.rb
30
30
  - lib/util/communia.rb
31
31
  - lib/util/console_logger.rb
32
+ - lib/util/downloader.rb
32
33
  - lib/util/i18n.rb
33
34
  - lib/util/lists.rb
34
35
  - lib/util/lists/iso639.rb
36
+ - lib/util/result.rb
35
37
  - lib/util/test.rb
36
38
  - lib/util/yaml.rb
37
39
  - share/lists/iso639-3.yml
38
40
  - test/unit.rb
41
+ - test/unit/args.rb
42
+ - test/unit/downloader.rb
43
+ - test/unit/downloader/orig/simple.html
39
44
  - test/unit/i18n.rb
40
45
  - test/unit/i18n/CamelCase/eng.yml
41
46
  - test/unit/i18n/CamelCase/fra.yml
@@ -47,6 +52,7 @@ files:
47
52
  - test/unit/i18n/void/fra.yml/Yes-git-I-need-this-folder-even-if-it-is-empty
48
53
  - test/unit/i18n/αλιας/fra.yml
49
54
  - test/unit/lists/iso639.rb
55
+ - test/unit/result.rb
50
56
  - tools/create-iso639-3.rb
51
57
  homepage: https://gitlab.com/guillel/util-gem
52
58
  licenses:
@@ -1,90 +0,0 @@
1
- # Functions to typecheck the arguments of a function.
2
- # @note WARNING!! The API of the module will most probably change
3
- # in future versions of the gem. For now consider it for internal
4
- # use only.
5
- class Arg
6
- private_class_method :new
7
-
8
- # Verify whether a given argument belongs to a class or can be converted
9
- # into that class.
10
- # @param [Object] value argument to check
11
- # @param [Class, String] klass class to which it must belong; Boolean is a synonym
12
- # for TrueClass; all standard classes who have #to_X will try to convert
13
- # the value if it responds to the given conversion method
14
- # @param [Object] alt value to use if the argument does not belong to the
15
- # wanted class
16
- # @param [Boolean] disallow_nil if true and +value+ is +nil+, then +alt+
17
- # will be returned instead of trying to convert +nil+ to the right type
18
- # @return [Object]
19
- # @example
20
- # def create_integer_hash key, value
21
- # key = Arg.check key, Symbol, :def # Nil does not respond to #to_sym
22
- # value = Arg.check value, Integer, nil, true
23
- # # If nil is provided as value, it shall remain as such and not
24
- # # be converted to 0 by #to_i
25
- # { key => value }
26
- # end
27
- #
28
- # hash1 = create_integer_hash 'hello', 13.4 # { :hello => 13 }
29
- # hash2 = create_integer_hash nil, nil # { :def => nil }
30
- def self.check value, klass, alt, disallow_nil = false
31
- return alt if disallow_nil and value.nil?
32
- case klass.to_s
33
- when 'Array' then return value.respond_to?(:to_a) ? value.to_a : alt
34
- when 'Boolean' then return value === true ? true : alt
35
- when 'Complex' then return value.respond_to?(:to_c) ? value.to_c : alt
36
- when 'FalseClass' then return value === false ? false : alt
37
- when 'Float' then return value.respond_to?(:to_f) ? value.to_f : alt
38
- when 'Hash' then return value.respond_to?(:to_h) ? value.to_h : alt
39
- when 'Integer' then return value.respond_to?(:to_i) ? value.to_i : alt
40
- when 'Rational' then return value.respond_to?(:to_r) ? value.to_r : alt
41
- when 'String' then return value.respond_to?(:to_s) ? value.to_s : alt
42
- when 'Symbol' then return value.respond_to?(:to_sym) ? value.to_sym : alt
43
- when 'TrueClass' then return value === true ? true : alt
44
- else return value.is_a?(klass) ? value : alt
45
- end
46
- end
47
-
48
- # Easier way to typecheck a whole list of argument than individually
49
- # calling Arg.check on each of them. Takes an array of arrays containing
50
- # the three or four argument of Arg.check, performs said method on each
51
- # of them, and returns an array of the results.
52
- # @param [Array<Array<Object, Class, Object, Boolean>>] array arguments to check
53
- # @return [Array<Object>]
54
- # @example
55
- # def create_integer_hash key, value
56
- # key, value = Arg.check_a [ [key, Symbol, :def],
57
- # [value, Integer, nil, true] ]
58
- # { key => value }
59
- # end
60
- def self.check_a array
61
- result = []
62
- array.each do |elem|
63
- value, klass, alt, dis = elem
64
- result << check(value, klass, alt, (dis.nil? ? false : dis))
65
- end
66
- result
67
- end
68
-
69
- # Typecheck the content of an options hash, while ignoring undefined
70
- # options. Calls Arg.check on the values associated with a given key,
71
- # according to the rest of the informations given.
72
- # @param [Hash] hash hash whose content to check
73
- # @param [Array<Array<Object, Class, Object, Boolean>>] array options to check
74
- # @return [Array<Object>]
75
- # @example
76
- # def initialize opts={}
77
- # opts = Arg.check opts, Hash, {}
78
- # @encoding, @font_size, @line_height = Arg.check_h opts,
79
- # [ [:enc, String, 'UTF-8', true], [:size, Integer, 12, true ],
80
- # [:height, Float, 1.0, true] ]
81
- # end
82
- def self.check_h hash, array
83
- result = []
84
- array.each do |elem|
85
- idx, klass, alt, dis = elem
86
- result << check(hash[idx], klass, alt, (dis.nil? ? false : dis))
87
- end
88
- result
89
- end
90
- end