util 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.
- checksums.yaml +7 -0
- data/.yardopts +1 -0
- data/CECILL-C.EN +517 -0
- data/CECILL-C.FR +521 -0
- data/LICENSES.md +21 -0
- data/README.md +478 -0
- data/lib/util.rb +12 -0
- data/lib/util/args.rb +138 -0
- data/lib/util/communia.rb +8 -0
- data/lib/util/console_logger.rb +178 -0
- data/lib/util/downloader.rb +157 -0
- data/lib/util/i18n.rb +327 -0
- data/lib/util/lists.rb +6 -0
- data/lib/util/lists/iso639.rb +149 -0
- data/lib/util/result.rb +175 -0
- data/lib/util/test.rb +225 -0
- data/lib/util/yaml.rb +18 -0
- data/share/lists/iso639-3.yml +35528 -0
- data/test/unit.rb +20 -0
- data/test/unit/args.rb +22 -0
- data/test/unit/downloader.rb +145 -0
- data/test/unit/downloader/orig/simple.html +7 -0
- data/test/unit/i18n.rb +216 -0
- data/test/unit/i18n/CamelCase/eng.yml +2 -0
- data/test/unit/i18n/CamelCase/fra.yml +2 -0
- data/test/unit/i18n/aaj.yml +2 -0
- data/test/unit/i18n/en.yml +2 -0
- data/test/unit/i18n/fra.yml +2 -0
- data/test/unit/i18n/i18n +0 -0
- data/test/unit/i18n/prv.yml +2 -0
- data/test/unit/i18n/void/fra.yml/Yes-git-I-need-this-folder-even-if-it-is-empty +0 -0
- data/test/unit/i18n//316/261/316/273/316/271/316/261/317/202/fra.yml +2 -0
- data/test/unit/lists/iso639.rb +78 -0
- data/test/unit/result.rb +180 -0
- data/tools/create-iso639-3.rb +235 -0
- metadata +80 -0
data/LICENSES.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
Code principal / Main code / Code principau
|
|
2
|
+
===========================================
|
|
3
|
+
|
|
4
|
+
Distribué sous license [CeCIll-C](https://cecill.info/licences.fr.html) (plus ou moins équivalent à une LGPL, mais conforme au droit français). Texte complet dans `CECILL-C.FR`.
|
|
5
|
+
|
|
6
|
+
Distributed under [CeCIll-C](https://cecill.info/licences.fr.html) license (more or less a compliant with French law version of LGPL). See full text in `CECILL-C.EN`.
|
|
7
|
+
|
|
8
|
+
Espargi amé la licènci [CeCIll-C](https://cecill.info/licences.fr.html) (mai o mens coume uno LGPL, mai counforme au dre francés). Tèste entié dins `CECILL-C.FR`.
|
|
9
|
+
|
|
10
|
+
ISO 639-3 Code Tables
|
|
11
|
+
=====================
|
|
12
|
+
|
|
13
|
+
The ISO 639-3 code set may be downloaded and incorporated into software products, web-based systems, digital devices, etc., either commercial or non-commercial, provided that:
|
|
14
|
+
|
|
15
|
+
- attribution is given [www.iso639-3.sil.org](https://iso639-3.sil.org) as the source of the codes;
|
|
16
|
+
- the identifiers of the code set are not modified or extended except as may be privately agreed using the Private Use Area (range qaa to qtz), and then such extensions shall not be distributed publicly;
|
|
17
|
+
- the product, system, or device does not provide a means to redistribute the code set.
|
|
18
|
+
|
|
19
|
+
Expansions to the information provided by the standard (e.g., population data, names in other languages, geographic coordinates, etc.) may be made and distributed as long as such added information is clearly identified as not being part of the standard itself. The ISO 639-3 website is the only authorized distribution site for the ISO 639-3 code tables.
|
|
20
|
+
|
|
21
|
+
For any questions about whether a particular use is covered by these guidelines, contact the Registration Authority at [ISO639-3@sil.org](mailto:iso639-3@sil.org).
|
data/README.md
ADDED
|
@@ -0,0 +1,478 @@
|
|
|
1
|
+
Gemme `Util`
|
|
2
|
+
============
|
|
3
|
+
|
|
4
|
+
**English explanations [below](#util-gem). ⇓ Esplicacioun en prouvènçau [dessouto](#gèmo-util).**
|
|
5
|
+
|
|
6
|
+
Cette gemme regroupe un certain nombre de petits utilitaires visant à simplifier l’écriture de code en Ruby. Il n'y a pas de logique particulière dans ce qui y est réuni, si ce n’est que chaque classe correspond à une fonctionnalité répétitive à écrire, mais utile dans de nombreux codes plus spécialisés. Pour l’heure, les classes sont les suivantes (par ordre alphabétique).
|
|
7
|
+
|
|
8
|
+
Sauf mention contraire, toutes les classes sont contenues dans le module principal `Util`. Se reporter à la documentation (en anglais) pour une explication plus détaillée du fonctionnement de chaque classe.
|
|
9
|
+
|
|
10
|
+
### → `Args` et `Opts`
|
|
11
|
+
|
|
12
|
+
Cette classe sert à vérifier le type des arguments passés à une fonction et, au besoin, à remplacer un argument invalide par une valeur par défaut. La variante `Opts` est utilisée pour les dictionnaires d’options.
|
|
13
|
+
|
|
14
|
+
```ruby
|
|
15
|
+
def creer_dict_entier cle, valeur
|
|
16
|
+
cle, valeur = Util::Args.check cle, Symbol, :def, valeur, Integer, nil
|
|
17
|
+
{ cle => valeur }
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def initialize opts={}
|
|
21
|
+
@encodage, @corps, @hauteur_ligne = Util::Opts.check opts, \
|
|
22
|
+
:enc, String, 'UTF-8', :corps, Integer, 12, :hauteur, Float, 1.0
|
|
23
|
+
end
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
### → `ConsoleLogger`
|
|
27
|
+
|
|
28
|
+
Cette classe offre une interface simple pour écrire différents types de messages sur le terminal, mis en forme et avec des couleurs. Fortement configurable.
|
|
29
|
+
|
|
30
|
+
```ruby
|
|
31
|
+
cl = ConsoleLogger.new e: { :stderr => true }
|
|
32
|
+
cl.warning 'Les erreurs seront affichées sur STDERR.'
|
|
33
|
+
# Message écrit en jaune
|
|
34
|
+
begin
|
|
35
|
+
texte = File.read 'secret.msg'
|
|
36
|
+
rescue Exception => e
|
|
37
|
+
msg = 'Impossible d’aller plus loin à cause de %E%, abandon.'
|
|
38
|
+
cl.error msg, 'E': e.message
|
|
39
|
+
# Message écrit en rouge
|
|
40
|
+
end
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
### → `Downloader`
|
|
44
|
+
|
|
45
|
+
Une classe permettant de télécharger un fichier de manière sécurisée.
|
|
46
|
+
|
|
47
|
+
```ruby
|
|
48
|
+
require 'oga' # Doit être requis par l’utilisateur, `Util` ne le fera pas
|
|
49
|
+
url = 'https://www.perdu.com/'
|
|
50
|
+
html = Util::Downloader.new(url).set_dest(Oga)
|
|
51
|
+
html.download # => Util::Result.ok
|
|
52
|
+
html.data.at_css('h1').text # 'Perdu sur l\'Internet ?'
|
|
53
|
+
|
|
54
|
+
url = 'https://gitlab.com/uploads/-/system/user/avatar/5582173/avatar.png'
|
|
55
|
+
Util::Downloader.new(url).set_name('guillel.png').set_force.download
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
### → `I18n`
|
|
59
|
+
|
|
60
|
+
Une classe permettant de faire de l’internationalisation basique. Ce n’est *en rien* un système complet prenant en charge le sens de lecture des langues ou les différentes manières de gérer le pluriel. Un code d’identification et une langue donnent une chaîne de caractères, rien de plus. Si vous avez besoin de quelque chose de plus complexe, il vaudra mieux vous tourner vers la gemme [`i18n`](https://rubygems.org/gems/i18n) de Rails, voire vers `gettext`.
|
|
61
|
+
|
|
62
|
+
```ruby
|
|
63
|
+
Util::I18n << __FILE__
|
|
64
|
+
Util::I18n.set_default_language :prv
|
|
65
|
+
Util::I18n.message 'bonjour' # 'Adiéu'
|
|
66
|
+
Util::I18n.message 'bonjour', lang: :fra # 'Bonjour'
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
### → `Lists`
|
|
70
|
+
|
|
71
|
+
Un module qui regroupe des sous-modules représentant des listes utiles.
|
|
72
|
+
|
|
73
|
+
#### ⇒ `Lists::ISO639`
|
|
74
|
+
|
|
75
|
+
Un module contenant les codes [ISO 639](https://fr.wikipedia.org/wiki/ISO_639). Pour l’instant, seule la classe `P3` contenant les codes ISO 639-3 est disponible.
|
|
76
|
+
|
|
77
|
+
```ruby
|
|
78
|
+
codes = Util::Lists::ISO639
|
|
79
|
+
puts codes::P3.from1 'fr' # :fre
|
|
80
|
+
puts codes::P3.exist? :prv # true
|
|
81
|
+
puts codes::P3.valid? :prv # false
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
### → `Result`
|
|
85
|
+
|
|
86
|
+
Une classe pour encapsuler un résultat et éviter les exceptions, similaire au `Result` de Rust ou OCaml.
|
|
87
|
+
|
|
88
|
+
```ruby
|
|
89
|
+
class Num
|
|
90
|
+
attr_reader :contenu
|
|
91
|
+
def initialize num
|
|
92
|
+
@contenu = num.to_f
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def self.ajouter premier, second
|
|
96
|
+
Util::Result.ok Num.new(premier.contenu + second.contenu)
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
def ajouter num
|
|
100
|
+
Util::Result.ok Num.new(@contenu + num.to_f)
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
def diviser denom
|
|
104
|
+
return Util::Result.err :div0, self if denom == 0
|
|
105
|
+
Util::Result.ok Num.new(@contenu / denom.to_f)
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
res = Num.new(42).ajouter(79) \
|
|
110
|
+
.bindm(:ajouter, 11) \
|
|
111
|
+
.bindm(:diviser, 12) \
|
|
112
|
+
.bindcm(:ajouter, Num.new(13)).value # 24
|
|
113
|
+
|
|
114
|
+
res = Num.new(42).ajouter(79) \
|
|
115
|
+
.bindm(:ajouter, 11) \
|
|
116
|
+
.bindm(:diviser, 0) \
|
|
117
|
+
.bindcm(:ajouter, Num.new(13)).error # :div0
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
### → `Testing`
|
|
121
|
+
|
|
122
|
+
Une classe pour simplifier l’écriture de tests unitaires. Là encore, on reste sur du basique. Pour quelque chose de plus complexe, on se tournera vers des environnements de test dédiés.
|
|
123
|
+
|
|
124
|
+
```ruby
|
|
125
|
+
class MaClasse
|
|
126
|
+
def self.fonction arg
|
|
127
|
+
raise ArgumentError, 'Ce n’est pas un entier non signé.' unless arg.to_i >= 0
|
|
128
|
+
-arg.to_i
|
|
129
|
+
end
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
test = Util::Testing.new
|
|
133
|
+
|
|
134
|
+
test.register 'succes', [-12, -42] do ||
|
|
135
|
+
[MaClasse.fonction('12'), MaClasse.fonction(42.0)]
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
test.register 'succes-avec-resultat-implicite' do ||
|
|
139
|
+
MaClasse.fonction '12'
|
|
140
|
+
true
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
test.register 'echec-attendu', ArgumentError, 'signé' do ||
|
|
144
|
+
MaClasse.fonction -1
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
test.register 'mauvaise-raison', ArgumentError, 'signé' do ||
|
|
148
|
+
MaClasse.fonction
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
test.register 'mauvaise-exception', ArgumentError, 'signé' do ||
|
|
152
|
+
MonAutreClasse.fonction
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
test.run
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
### → `YAML` (module indépendant)
|
|
159
|
+
|
|
160
|
+
Une extension du module `YAML` de la bibliothèque standard, qui se contente d’ajouter une fonction `from_file` permettant de lire un fichier YAML tout en vérifiant qu’aucune erreur ne s’est produite.
|
|
161
|
+
|
|
162
|
+
`Util` gem
|
|
163
|
+
==========
|
|
164
|
+
|
|
165
|
+
This gem groups together a number of small utilities that aim to make writing code in Ruby easier. There is no specific logic behind the collection, except that each class corresponds to a functionality that is repetitive to write, but useful in many more specialized codes. For now, the following classes are extant (in alphabetical order).
|
|
166
|
+
|
|
167
|
+
Unless specified otherwise, all classes are contained inside the `Util` main module. See the documentation for a more detailed explanation of how each class works.
|
|
168
|
+
|
|
169
|
+
### → `Args` and `Opts`
|
|
170
|
+
|
|
171
|
+
This class is used to typecheck the arguments provided to a function and, if needed, replace an invalid argument by a default value. The `Opts` variant is used for option hashes.
|
|
172
|
+
|
|
173
|
+
```ruby
|
|
174
|
+
def create_integer_hash key, value
|
|
175
|
+
key, value = Util::Args.check key, Symbol, :def, value, Integer, nil
|
|
176
|
+
{ key => value }
|
|
177
|
+
end
|
|
178
|
+
|
|
179
|
+
def initialize opts={}
|
|
180
|
+
@encoding, @font_size, @line_height = Util::Opts.check opts, \
|
|
181
|
+
:enc, String, 'UTF-8', :size, Integer, 12, :height, Float, 1.0
|
|
182
|
+
end
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
### → `ConsoleLogger`
|
|
186
|
+
|
|
187
|
+
This class offers a simple interface to write different kinds of messages on the console, formatted and colored. Highly configurable.
|
|
188
|
+
|
|
189
|
+
```ruby
|
|
190
|
+
cl = ConsoleLogger.new e: { :stderr => true }
|
|
191
|
+
cl.warning 'Errors will be logged on STDERR.'
|
|
192
|
+
# Message written in yellow
|
|
193
|
+
begin
|
|
194
|
+
text = File.read 'secret.msg'
|
|
195
|
+
rescue Exception => e
|
|
196
|
+
msg = 'Cannot go any further because of %E%, aborting.'
|
|
197
|
+
cl.error msg, 'E': e.message
|
|
198
|
+
# Message written in red
|
|
199
|
+
end
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
### → `Downloader`
|
|
203
|
+
|
|
204
|
+
A class to safely download a file.
|
|
205
|
+
|
|
206
|
+
```ruby
|
|
207
|
+
require 'oga' # Must be required by the user, `Util` will not do it
|
|
208
|
+
url = 'https://www.perdu.com/'
|
|
209
|
+
html = Util::Downloader.new(url).set_dest(Oga)
|
|
210
|
+
html.download # => Util::Result.ok
|
|
211
|
+
html.data.at_css('h1').text # 'Perdu sur l\'Internet ?'
|
|
212
|
+
|
|
213
|
+
url = 'https://gitlab.com/uploads/-/system/user/avatar/5582173/avatar.png'
|
|
214
|
+
Util::Downloader.new(url).set_name('guillel.png').set_force.download
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
### → `I18n`
|
|
218
|
+
|
|
219
|
+
A class to do some basic internationalization. It is *in no way* a full-fledged system that would take into account reading directions or different ways of pluralizing. An token and a language give a string, nothing more. If you need something more complex, you should rather use Rails [`i18n`](https://rubygems.org/gems/i18n) gem, or even `gettext`.
|
|
220
|
+
|
|
221
|
+
```ruby
|
|
222
|
+
Util::I18n << __FILE__
|
|
223
|
+
Util::I18n.set_default_language :prv
|
|
224
|
+
Util::I18n.message 'bonjour' # 'Adiéu'
|
|
225
|
+
Util::I18n.message 'bonjour', lang: :fra # 'Bonjour'
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
### → `Lists`
|
|
229
|
+
|
|
230
|
+
A collection of submodules respresenting useful lists.
|
|
231
|
+
|
|
232
|
+
#### ⇒ `Lists::ISO639`
|
|
233
|
+
|
|
234
|
+
A module containing [ISO 639](https://en.wikipedia.org/wiki/ISO_639) codes. For now, the class `P3` containing ISO 639-3 codes is the only one available.
|
|
235
|
+
|
|
236
|
+
```ruby
|
|
237
|
+
codes = Util::Lists::ISO639
|
|
238
|
+
puts codes::P3.from1 'fr' # :fre
|
|
239
|
+
puts codes::P3.exist? :prv # true
|
|
240
|
+
puts codes::P3.valid? :prv # false
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
### → `Result`
|
|
244
|
+
|
|
245
|
+
A class to wrap a result and avoid exceptions, similar to Rust’s or OCaml’s `Result`.
|
|
246
|
+
|
|
247
|
+
```ruby
|
|
248
|
+
class Num
|
|
249
|
+
attr_reader :content
|
|
250
|
+
def initialize num
|
|
251
|
+
@content = num.to_f
|
|
252
|
+
end
|
|
253
|
+
|
|
254
|
+
def self.add first, second
|
|
255
|
+
Util::Result.ok Num.new(premier.first + second.content)
|
|
256
|
+
end
|
|
257
|
+
|
|
258
|
+
def add num
|
|
259
|
+
Util::Result.ok Num.new(@content + num.to_f)
|
|
260
|
+
end
|
|
261
|
+
|
|
262
|
+
def divide denom
|
|
263
|
+
return Util::Result.err :div0, self if denom == 0
|
|
264
|
+
Util::Result.ok Num.new(@content / denom.to_f)
|
|
265
|
+
end
|
|
266
|
+
end
|
|
267
|
+
|
|
268
|
+
res = Num.new(42).add(79) \
|
|
269
|
+
.bindm(:add, 11) \
|
|
270
|
+
.bindm(:divide, 12) \
|
|
271
|
+
.bindcm(:add, Num.new(13)).value # 24
|
|
272
|
+
|
|
273
|
+
res = Num.new(42).add(79) \
|
|
274
|
+
.bindm(:add, 11) \
|
|
275
|
+
.bindm(:divide, 0) \
|
|
276
|
+
.bindcm(:add, Num.new(13)).error # :div0
|
|
277
|
+
```
|
|
278
|
+
|
|
279
|
+
### → `Testing`
|
|
280
|
+
|
|
281
|
+
A class to make writing unit tests easier. There again, it remains basic. For something more complex, better go with a dedicated testing framework.
|
|
282
|
+
|
|
283
|
+
```ruby
|
|
284
|
+
class MyClass
|
|
285
|
+
def self.function arg
|
|
286
|
+
raise ArgumentError, 'Not an unsigned int.' unless arg.to_i >= 0
|
|
287
|
+
-arg.to_i
|
|
288
|
+
end
|
|
289
|
+
end
|
|
290
|
+
|
|
291
|
+
test = Util::Testing.new
|
|
292
|
+
|
|
293
|
+
test.register 'success', [-12, -42] do ||
|
|
294
|
+
[MyClass.function('12'), MyClass.function(42.0)]
|
|
295
|
+
end
|
|
296
|
+
|
|
297
|
+
test.register 'success-with-implicit-result' do ||
|
|
298
|
+
MyClass.function '12'
|
|
299
|
+
true
|
|
300
|
+
end
|
|
301
|
+
|
|
302
|
+
test.register 'expected-fail', ArgumentError, 'unsigned' do ||
|
|
303
|
+
MyClass.function -1
|
|
304
|
+
end
|
|
305
|
+
|
|
306
|
+
test.register 'wrong-reason', ArgumentError, 'unsigned' do ||
|
|
307
|
+
MyClass.function
|
|
308
|
+
end
|
|
309
|
+
|
|
310
|
+
test.register 'wrong-exception', ArgumentError, 'unsigned' do ||
|
|
311
|
+
MyOtherClass.function
|
|
312
|
+
end
|
|
313
|
+
|
|
314
|
+
test.run
|
|
315
|
+
```
|
|
316
|
+
|
|
317
|
+
### → `YAML` (module indépendant)
|
|
318
|
+
|
|
319
|
+
An extension to the standard library module `YAML`, that just adds a `from_file` function allowing to read a YAML file while checking that no error happened.
|
|
320
|
+
|
|
321
|
+
Gèmo `Util`
|
|
322
|
+
===========
|
|
323
|
+
|
|
324
|
+
Aquesto gèmo recampo quauqui pichòtis utilita servènt à escriéure dóu code Ruby mai simplamen. I a pas gaire de lougico dins ço qu’i es reüni, à despart de que cado classo courrespond à quicon de repetitéu à escriéure, mai utile dins un mouloun de code mai especialisa. Aro, li classo soun li seguènto (alfabeticamen).
|
|
325
|
+
|
|
326
|
+
Sènso mencioun countràri, tòuti li classo soun countengudo dins lou moudule principau `Util`. Vèire la doucumentacioun pèr d’esclargimen sus coume marcho cado classo.
|
|
327
|
+
|
|
328
|
+
### → `Args` e `Opts`
|
|
329
|
+
|
|
330
|
+
Aquesto classo sèr à s’assegura dóu tipe dis argumen passa à-n-uno founcioun e, au besoun, à ramplaça un argumen invalide amé ’no valour pèr defaut. La varianto `Opts` s’uso pèr li diciounàri d’oupcioun.
|
|
331
|
+
|
|
332
|
+
```ruby
|
|
333
|
+
def crea_dic_entie clau, valour
|
|
334
|
+
clau, valour = Util::Args.check clau, Symbol, :def, valour, Integer, nil
|
|
335
|
+
{ clau => valour }
|
|
336
|
+
end
|
|
337
|
+
|
|
338
|
+
def initialize oupc={}
|
|
339
|
+
@encoudage, @cors, @aussado_ligno = Util::Opts.check oupc, \
|
|
340
|
+
:enc, String, 'UTF-8', :cors, Integer, 12, :aussado, Float, 1.0
|
|
341
|
+
end
|
|
342
|
+
```
|
|
343
|
+
|
|
344
|
+
### → `ConsoleLogger`
|
|
345
|
+
|
|
346
|
+
Aquesto classo óufris uno interfàci simplo pèr escriéure divèrsi message sus lou terminau, mes en fourmo e acoulouri. Se pòu mai counfigura.
|
|
347
|
+
|
|
348
|
+
```ruby
|
|
349
|
+
cl = ConsoleLogger.new e: { :stderr => true }
|
|
350
|
+
cl.warning 'Lis errour saran escricho sus STDERR.'
|
|
351
|
+
# Message escri en jaune
|
|
352
|
+
begin
|
|
353
|
+
text = File.read 'secret.msg'
|
|
354
|
+
rescue Exception => e
|
|
355
|
+
msg = 'Impoussible d’ana mai liuen à cause de %E%, abandoun.'
|
|
356
|
+
cl.error msg, 'E': e.message
|
|
357
|
+
# Message escri en rouge
|
|
358
|
+
end
|
|
359
|
+
```
|
|
360
|
+
|
|
361
|
+
### → `Downloader`
|
|
362
|
+
|
|
363
|
+
Uno classo pèr telecarga un fiquié seguramen.
|
|
364
|
+
|
|
365
|
+
```ruby
|
|
366
|
+
require 'oga' # Dèu èstre requist pèr l’usagié, `Util` lou fara pas
|
|
367
|
+
url = 'https://www.perdu.com/'
|
|
368
|
+
html = Util::Downloader.new(url).set_dest(Oga)
|
|
369
|
+
html.download # => Util::Result.ok
|
|
370
|
+
html.data.at_css('h1').text # 'Perdu sur l\'Internet ?'
|
|
371
|
+
|
|
372
|
+
url = 'https://gitlab.com/uploads/-/system/user/avatar/5582173/avatar.png'
|
|
373
|
+
Util::Downloader.new(url).set_name('guillel.png').set_force.download
|
|
374
|
+
```
|
|
375
|
+
|
|
376
|
+
### → `I18n`
|
|
377
|
+
|
|
378
|
+
Uno classo pèr faire de l’internaciounalisacioun elementàri. Es *pas ges* de sistèmo entié que tèn comte dóu sèns de leituro di lengo vo di biais diferènt de mena lou plurau. Un code d’identificacioun e uno lengo baion uno cadeno d’emprèsso, ges de mai. Se vous fai mestié d’agué quicon de mai coumplèisse, vaudra miés chausi la gèmo [`i18n`](https://rubygems.org/gems/i18n) de Rails, emai bessai `gettext`.
|
|
379
|
+
|
|
380
|
+
```ruby
|
|
381
|
+
Util::I18n << __FILE__
|
|
382
|
+
Util::I18n.set_default_language :prv
|
|
383
|
+
Util::I18n.message 'bonjour' # 'Adiéu'
|
|
384
|
+
Util::I18n.message 'bonjour', lang: :fra # 'Bonjour'
|
|
385
|
+
```
|
|
386
|
+
|
|
387
|
+
### → `Lists`
|
|
388
|
+
|
|
389
|
+
Uno couleicioun de souto-moudule que represènton de tiero utile.
|
|
390
|
+
|
|
391
|
+
#### ⇒ `Lists::ISO639`
|
|
392
|
+
|
|
393
|
+
Un moudule countenènt li code [ISO 639](https://fr.wikipedia.org/wiki/ISO_639). Aro, i a que la classo `P3`, que countèn li code ISO 639-3.
|
|
394
|
+
|
|
395
|
+
```ruby
|
|
396
|
+
codes = Util::Lists::ISO639
|
|
397
|
+
puts codes::P3.from1 'fr' # :fre
|
|
398
|
+
puts codes::P3.exist? :prv # true
|
|
399
|
+
puts codes::P3.valid? :prv # false
|
|
400
|
+
```
|
|
401
|
+
|
|
402
|
+
### → `Result`
|
|
403
|
+
|
|
404
|
+
Uno classo pèr embala un resultat e s’engarda dis eicecioun, coume lou `Result` de Rust o OCaml.
|
|
405
|
+
|
|
406
|
+
```ruby
|
|
407
|
+
class Num
|
|
408
|
+
attr_reader :countengu
|
|
409
|
+
def initialize num
|
|
410
|
+
@countengu = num.to_f
|
|
411
|
+
end
|
|
412
|
+
|
|
413
|
+
def self.apoundre premie, segound
|
|
414
|
+
Util::Result.ok Num.new(premie.countengu + segound.countengu)
|
|
415
|
+
end
|
|
416
|
+
|
|
417
|
+
def apoundre num
|
|
418
|
+
Util::Result.ok Num.new(@countengu + num.to_f)
|
|
419
|
+
end
|
|
420
|
+
|
|
421
|
+
def parti denoum
|
|
422
|
+
return Util::Result.err :div0, self if denoum == 0
|
|
423
|
+
Util::Result.ok Num.new(@countengu / denoum.to_f)
|
|
424
|
+
end
|
|
425
|
+
end
|
|
426
|
+
|
|
427
|
+
res = Num.new(42).apoundre(79) \
|
|
428
|
+
.bindm(:apoundre, 11) \
|
|
429
|
+
.bindm(:parti, 12) \
|
|
430
|
+
.bindcm(:apoundre, Num.new(13)).value # 24
|
|
431
|
+
|
|
432
|
+
res = Num.new(42).apoundre(79) \
|
|
433
|
+
.bindm(:apoundre, 11) \
|
|
434
|
+
.bindm(:parti, 0) \
|
|
435
|
+
.bindcm(:apoundre, Num.new(13)).error # :div0
|
|
436
|
+
```
|
|
437
|
+
|
|
438
|
+
### → `Testing`
|
|
439
|
+
|
|
440
|
+
Uno classo pèr assimpli l’escrituro d’assai unitàri. Aqui tambèn, demouro elementàri. Pèr quicon de mai coumplèisse, fau chausi un vertadier envirounamen d’assai.
|
|
441
|
+
|
|
442
|
+
```ruby
|
|
443
|
+
class MaClasso
|
|
444
|
+
def self.founcioun arg
|
|
445
|
+
raise ArgumentError, 'Pas ges d’entié noun signa.' unless arg.to_i >= 0
|
|
446
|
+
-arg.to_i
|
|
447
|
+
end
|
|
448
|
+
end
|
|
449
|
+
|
|
450
|
+
assai = Util::Testing.new
|
|
451
|
+
|
|
452
|
+
assai.register 'reussido', [-12, -42] do ||
|
|
453
|
+
[MaClasso.founcioun('12'), MaClasso.founcioun(42.0)]
|
|
454
|
+
end
|
|
455
|
+
|
|
456
|
+
assai.register 'reussido-senso-resultat-espres' do ||
|
|
457
|
+
MaClasso.founcioun '12'
|
|
458
|
+
true
|
|
459
|
+
end
|
|
460
|
+
|
|
461
|
+
assai.register 'revirado-esperado', ArgumentError, 'signa' do ||
|
|
462
|
+
MaClasso.founcioun -1
|
|
463
|
+
end
|
|
464
|
+
|
|
465
|
+
assai.register 'marrido-encauso', ArgumentError, 'signa' do ||
|
|
466
|
+
MaClasso.founcioun
|
|
467
|
+
end
|
|
468
|
+
|
|
469
|
+
assai.register 'marrido-eicecioun', ArgumentError, 'signa' do ||
|
|
470
|
+
MounAutroClasso.founcioun
|
|
471
|
+
end
|
|
472
|
+
|
|
473
|
+
assai.run
|
|
474
|
+
```
|
|
475
|
+
|
|
476
|
+
### → `YAML` (moudule independènt)
|
|
477
|
+
|
|
478
|
+
Vèn espandi lou moudule `YAML` de la biblioutèco ourdinàrio, e fai ren de mai qu’apoundre uno founcioun `from_file`, que permet de legi un fiquié YAML en s’assegurant que i a pas agu d’errour.
|