num2words 0.4.0 → 0.5.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 +4 -4
- data/CHANGELOG.md +11 -0
- data/README.md +30 -0
- data/config/locales/en.yml +4 -0
- data/config/locales/ru.yml +4 -0
- data/docs/api.md +60 -4
- data/docs/api.ru.md +60 -4
- data/docs/rails.md +11 -0
- data/docs/rails.ru.md +11 -0
- data/lib/num2words/config.rb +54 -0
- data/lib/num2words/converter.rb +8 -5
- data/lib/num2words/errors.rb +49 -0
- data/lib/num2words/rails/validators/currency_validator.rb +1 -1
- data/lib/num2words/rails/validators/locale_validator.rb +1 -1
- data/lib/num2words/version.rb +1 -1
- data/lib/num2words.rb +1 -0
- metadata +3 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 131bb24135ba3f8c1345bbd7ccf5c3b27d79b9c9776d7e0ec45b259960be1dbd
|
|
4
|
+
data.tar.gz: 27a943ef2bbaae410ae51d1aca3fe009375f68632e1928b4639a570919619c25
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 3601c493e2cf40b2a908daca5980f79c95933316fa2a87393306c479919ec795c15af731e2eb075fd92b9aa61a97fd72badaa575723d8201e5093cecc89e6d88
|
|
7
|
+
data.tar.gz: 5000cdf5a8fcaa4cf2af57cb2ad7640e111f9f2a1eb7fdf94783ac19a76a2cd8aedd2ef44cbecd7eee9647d3bb1be064f2d70f711634aa3de679f83ede17a47d
|
data/CHANGELOG.md
CHANGED
|
@@ -11,6 +11,17 @@
|
|
|
11
11
|
|
|
12
12
|
---
|
|
13
13
|
|
|
14
|
+
## [0.5.0] - 2026-07-06
|
|
15
|
+
|
|
16
|
+
### Added
|
|
17
|
+
- Опция `strict: true` для `to_currency`, которая вызывает `Num2words::UnsupportedCurrencyError` при недоступной валюте вместо fallback на валюту по умолчанию.
|
|
18
|
+
- Классы ошибок `Num2words::UnsupportedInputError`, `Num2words::UnsupportedOptionError` и `Num2words::UnsupportedCurrencyAmountError`, совместимые с `ArgumentError`.
|
|
19
|
+
- Rails/ActiveModel сообщения ошибок для валидаторов `num2words_currency` и `num2words_locale`.
|
|
20
|
+
- Публичный metadata API `Num2words.currency_options` для select-полей и настроек.
|
|
21
|
+
- Публичный metadata API `Num2words.locale_options` для select-полей и настроек.
|
|
22
|
+
|
|
23
|
+
---
|
|
24
|
+
|
|
14
25
|
## [0.4.0] - 2026-07-05
|
|
15
26
|
|
|
16
27
|
### Added
|
data/README.md
CHANGED
|
@@ -84,6 +84,9 @@ Num2words.to_currency(BigDecimal("21.05"), :ru)
|
|
|
84
84
|
12.50.to_currency(:ru, code: :USD)
|
|
85
85
|
# => "двенадцать долларов пятьдесят центов"
|
|
86
86
|
|
|
87
|
+
Num2words.to_currency(12.50, :ru, code: :XYZ, strict: true)
|
|
88
|
+
# raises Num2words::UnsupportedCurrencyError
|
|
89
|
+
|
|
87
90
|
1.to_currency(:ru, minor: :nonzero)
|
|
88
91
|
# => "один рубль"
|
|
89
92
|
|
|
@@ -145,6 +148,7 @@ Num2words.to_currency(BigDecimal("21.05"), :ru)
|
|
|
145
148
|
- `word_case: :default | :upper | :downcase | :capitalize | :title` — регистр результата.
|
|
146
149
|
- `minor: :always | :nonzero | :never` — вывод младшей денежной единицы.
|
|
147
150
|
- `code: :USD` — выбор валюты для `to_currency`.
|
|
151
|
+
- `strict: true` — ошибка вместо fallback при недоступной валюте в `to_currency`.
|
|
148
152
|
|
|
149
153
|
Полная справка по API:
|
|
150
154
|
|
|
@@ -208,10 +212,36 @@ Num2words.default_currency_info(:ru)
|
|
|
208
212
|
# minor_unit: ["копейка", "копейки", "копеек"],
|
|
209
213
|
# symbol: "₽"
|
|
210
214
|
# }
|
|
215
|
+
|
|
216
|
+
Num2words.currency_options(:ru)
|
|
217
|
+
# => [["RUB — ₽", :RUB], ["USD — $", :USD], ...]
|
|
218
|
+
|
|
219
|
+
Num2words.locale_options
|
|
220
|
+
# => [["ar", :ar], ["be", :be], ["bg", :bg], ...]
|
|
211
221
|
```
|
|
212
222
|
|
|
213
223
|
---
|
|
214
224
|
|
|
225
|
+
## Ошибки
|
|
226
|
+
|
|
227
|
+
```ruby
|
|
228
|
+
Num2words.to_currency(10, :ru, code: :XYZ, strict: true)
|
|
229
|
+
# raises Num2words::UnsupportedCurrencyError
|
|
230
|
+
|
|
231
|
+
Num2words.to_words(Object.new, :ru)
|
|
232
|
+
# raises Num2words::UnsupportedInputError
|
|
233
|
+
|
|
234
|
+
Num2words.to_words(0.5, :ru, joiner: :plus)
|
|
235
|
+
# raises Num2words::UnsupportedOptionError
|
|
236
|
+
|
|
237
|
+
Num2words.to_currency("abc", :ru)
|
|
238
|
+
# raises Num2words::UnsupportedCurrencyAmountError
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
Ошибки пользовательских аргументов наследуются от `ArgumentError`, поэтому существующий код с `rescue ArgumentError` продолжает работать.
|
|
242
|
+
|
|
243
|
+
---
|
|
244
|
+
|
|
215
245
|
## Консоль
|
|
216
246
|
|
|
217
247
|
Запуск из репозитория:
|
data/config/locales/en.yml
CHANGED
data/config/locales/ru.yml
CHANGED
data/docs/api.md
CHANGED
|
@@ -318,6 +318,15 @@ Num2words.to_currency(12.50, :ru, code: :USD)
|
|
|
318
318
|
|
|
319
319
|
If the currency is not available for the locale, `num2words` falls back to the locale default currency and may emit a warning depending on configuration.
|
|
320
320
|
|
|
321
|
+
### `strict`
|
|
322
|
+
|
|
323
|
+
For `to_currency`, enables strict currency validation. When `code:` is unavailable for the locale, the method raises `Num2words::UnsupportedCurrencyError` instead of falling back to the default currency.
|
|
324
|
+
|
|
325
|
+
```ruby
|
|
326
|
+
Num2words.to_currency(12.50, :ru, code: :XYZ, strict: true)
|
|
327
|
+
# raises Num2words::UnsupportedCurrencyError
|
|
328
|
+
```
|
|
329
|
+
|
|
321
330
|
### `minor`
|
|
322
331
|
|
|
323
332
|
Controls minor currency unit output.
|
|
@@ -410,6 +419,27 @@ Num2words.default_currency_info(:ru)
|
|
|
410
419
|
# }
|
|
411
420
|
```
|
|
412
421
|
|
|
422
|
+
### `Num2words.currency_options`
|
|
423
|
+
|
|
424
|
+
Returns an array for select inputs and settings screens.
|
|
425
|
+
|
|
426
|
+
```ruby
|
|
427
|
+
Num2words.currency_options(:ru)
|
|
428
|
+
# => [["RUB — ₽", :RUB], ["USD — $", :USD], ...]
|
|
429
|
+
|
|
430
|
+
Num2words.currency_options(:ru, format: :code)
|
|
431
|
+
# => [["RUB", :RUB], ["USD", :USD], ...]
|
|
432
|
+
|
|
433
|
+
Num2words.currency_options(:ru, format: :name)
|
|
434
|
+
# => [["рубль", :RUB], ["доллар", :USD], ...]
|
|
435
|
+
```
|
|
436
|
+
|
|
437
|
+
Supported `format:` values:
|
|
438
|
+
|
|
439
|
+
- `:symbol` - currency code and symbol, default.
|
|
440
|
+
- `:code` - currency code only.
|
|
441
|
+
- `:name` - localized major currency unit name.
|
|
442
|
+
|
|
413
443
|
### `Num2words.available_locales`
|
|
414
444
|
|
|
415
445
|
Returns supported locale codes.
|
|
@@ -419,6 +449,23 @@ Num2words.available_locales
|
|
|
419
449
|
# => [:ar, :be, :bg, :bn, :cs, ...]
|
|
420
450
|
```
|
|
421
451
|
|
|
452
|
+
### `Num2words.locale_options`
|
|
453
|
+
|
|
454
|
+
Returns an array of locales for select inputs and settings screens.
|
|
455
|
+
|
|
456
|
+
```ruby
|
|
457
|
+
Num2words.locale_options
|
|
458
|
+
# => [["ar", :ar], ["be", :be], ["bg", :bg], ...]
|
|
459
|
+
|
|
460
|
+
Num2words.locale_options(format: :upcase)
|
|
461
|
+
# => [["AR", :ar], ["BE", :be], ["BG", :bg], ...]
|
|
462
|
+
```
|
|
463
|
+
|
|
464
|
+
Supported `format:` values:
|
|
465
|
+
|
|
466
|
+
- `:code` - locale code, default.
|
|
467
|
+
- `:upcase` - upper-case locale code.
|
|
468
|
+
|
|
422
469
|
### `Num2words.currency_warnings`
|
|
423
470
|
|
|
424
471
|
Controls warnings when requested currency is unavailable.
|
|
@@ -446,28 +493,37 @@ KZT MYR NOK PKR PLN RON RSD RUB SAR SEK THB TRY UAH USD VND
|
|
|
446
493
|
|
|
447
494
|
## Errors
|
|
448
495
|
|
|
496
|
+
User argument errors inherit from `ArgumentError`, so existing code using `rescue ArgumentError` continues to work.
|
|
497
|
+
|
|
449
498
|
Unsupported input type:
|
|
450
499
|
|
|
451
500
|
```ruby
|
|
452
501
|
Num2words.to_words(Object.new, :en)
|
|
453
|
-
# raises
|
|
502
|
+
# raises Num2words::UnsupportedInputError
|
|
454
503
|
```
|
|
455
504
|
|
|
456
505
|
Unsupported options:
|
|
457
506
|
|
|
458
507
|
```ruby
|
|
459
508
|
Num2words.to_words(0.5, :ru, joiner: :plus)
|
|
460
|
-
# raises
|
|
509
|
+
# raises Num2words::UnsupportedOptionError
|
|
461
510
|
|
|
462
511
|
Num2words.to_currency(12, :ru, minor: :sometimes)
|
|
463
|
-
# raises
|
|
512
|
+
# raises Num2words::UnsupportedOptionError
|
|
464
513
|
```
|
|
465
514
|
|
|
466
515
|
Unsupported currency amount:
|
|
467
516
|
|
|
468
517
|
```ruby
|
|
469
518
|
Num2words.to_currency("abc", :en)
|
|
470
|
-
# raises
|
|
519
|
+
# raises Num2words::UnsupportedCurrencyAmountError
|
|
520
|
+
```
|
|
521
|
+
|
|
522
|
+
Unsupported currency in strict mode:
|
|
523
|
+
|
|
524
|
+
```ruby
|
|
525
|
+
Num2words.to_currency(10, :ru, code: :XYZ, strict: true)
|
|
526
|
+
# raises Num2words::UnsupportedCurrencyError
|
|
471
527
|
```
|
|
472
528
|
|
|
473
529
|
## Development References
|
data/docs/api.ru.md
CHANGED
|
@@ -318,6 +318,15 @@ Num2words.to_currency(12.50, :ru, code: :USD)
|
|
|
318
318
|
|
|
319
319
|
Если валюта недоступна для локали, `num2words` использует валюту локали по умолчанию и может вывести warning в зависимости от настроек.
|
|
320
320
|
|
|
321
|
+
### `strict`
|
|
322
|
+
|
|
323
|
+
Для `to_currency` включает строгую проверку валюты. Если `code:` недоступен для локали, метод вызывает `Num2words::UnsupportedCurrencyError` вместо fallback на валюту по умолчанию.
|
|
324
|
+
|
|
325
|
+
```ruby
|
|
326
|
+
Num2words.to_currency(12.50, :ru, code: :XYZ, strict: true)
|
|
327
|
+
# raises Num2words::UnsupportedCurrencyError
|
|
328
|
+
```
|
|
329
|
+
|
|
321
330
|
### `minor`
|
|
322
331
|
|
|
323
332
|
Управляет выводом младшей денежной единицы.
|
|
@@ -410,6 +419,27 @@ Num2words.default_currency_info(:ru)
|
|
|
410
419
|
# }
|
|
411
420
|
```
|
|
412
421
|
|
|
422
|
+
### `Num2words.currency_options`
|
|
423
|
+
|
|
424
|
+
Возвращает массив для select-полей и настроек.
|
|
425
|
+
|
|
426
|
+
```ruby
|
|
427
|
+
Num2words.currency_options(:ru)
|
|
428
|
+
# => [["RUB — ₽", :RUB], ["USD — $", :USD], ...]
|
|
429
|
+
|
|
430
|
+
Num2words.currency_options(:ru, format: :code)
|
|
431
|
+
# => [["RUB", :RUB], ["USD", :USD], ...]
|
|
432
|
+
|
|
433
|
+
Num2words.currency_options(:ru, format: :name)
|
|
434
|
+
# => [["рубль", :RUB], ["доллар", :USD], ...]
|
|
435
|
+
```
|
|
436
|
+
|
|
437
|
+
Поддерживаемые значения `format:`:
|
|
438
|
+
|
|
439
|
+
- `:symbol` - код и символ валюты, значение по умолчанию.
|
|
440
|
+
- `:code` - только код валюты.
|
|
441
|
+
- `:name` - локализованное название основной денежной единицы.
|
|
442
|
+
|
|
413
443
|
### `Num2words.available_locales`
|
|
414
444
|
|
|
415
445
|
Возвращает список поддерживаемых локалей.
|
|
@@ -419,6 +449,23 @@ Num2words.available_locales
|
|
|
419
449
|
# => [:ar, :be, :bg, :bn, :cs, ...]
|
|
420
450
|
```
|
|
421
451
|
|
|
452
|
+
### `Num2words.locale_options`
|
|
453
|
+
|
|
454
|
+
Возвращает массив локалей для select-полей и настроек.
|
|
455
|
+
|
|
456
|
+
```ruby
|
|
457
|
+
Num2words.locale_options
|
|
458
|
+
# => [["ar", :ar], ["be", :be], ["bg", :bg], ...]
|
|
459
|
+
|
|
460
|
+
Num2words.locale_options(format: :upcase)
|
|
461
|
+
# => [["AR", :ar], ["BE", :be], ["BG", :bg], ...]
|
|
462
|
+
```
|
|
463
|
+
|
|
464
|
+
Поддерживаемые значения `format:`:
|
|
465
|
+
|
|
466
|
+
- `:code` - код локали, значение по умолчанию.
|
|
467
|
+
- `:upcase` - код локали в верхнем регистре.
|
|
468
|
+
|
|
422
469
|
### `Num2words.currency_warnings`
|
|
423
470
|
|
|
424
471
|
Управляет warning при запросе валюты, недоступной для локали.
|
|
@@ -446,28 +493,37 @@ KZT MYR NOK PKR PLN RON RSD RUB SAR SEK THB TRY UAH USD VND
|
|
|
446
493
|
|
|
447
494
|
## Ошибки
|
|
448
495
|
|
|
496
|
+
Ошибки пользовательских аргументов наследуются от `ArgumentError`, поэтому существующий код с `rescue ArgumentError` продолжает работать.
|
|
497
|
+
|
|
449
498
|
Неподдерживаемый тип входных данных:
|
|
450
499
|
|
|
451
500
|
```ruby
|
|
452
501
|
Num2words.to_words(Object.new, :en)
|
|
453
|
-
# raises
|
|
502
|
+
# raises Num2words::UnsupportedInputError
|
|
454
503
|
```
|
|
455
504
|
|
|
456
505
|
Неподдерживаемые опции:
|
|
457
506
|
|
|
458
507
|
```ruby
|
|
459
508
|
Num2words.to_words(0.5, :ru, joiner: :plus)
|
|
460
|
-
# raises
|
|
509
|
+
# raises Num2words::UnsupportedOptionError
|
|
461
510
|
|
|
462
511
|
Num2words.to_currency(12, :ru, minor: :sometimes)
|
|
463
|
-
# raises
|
|
512
|
+
# raises Num2words::UnsupportedOptionError
|
|
464
513
|
```
|
|
465
514
|
|
|
466
515
|
Некорректная денежная сумма:
|
|
467
516
|
|
|
468
517
|
```ruby
|
|
469
518
|
Num2words.to_currency("abc", :en)
|
|
470
|
-
# raises
|
|
519
|
+
# raises Num2words::UnsupportedCurrencyAmountError
|
|
520
|
+
```
|
|
521
|
+
|
|
522
|
+
Недоступная валюта в строгом режиме:
|
|
523
|
+
|
|
524
|
+
```ruby
|
|
525
|
+
Num2words.to_currency(10, :ru, code: :XYZ, strict: true)
|
|
526
|
+
# raises Num2words::UnsupportedCurrencyError
|
|
471
527
|
```
|
|
472
528
|
|
|
473
529
|
## Документация для разработки
|
data/docs/rails.md
CHANGED
|
@@ -89,6 +89,8 @@ Payment.new(locale: "xx").valid?
|
|
|
89
89
|
|
|
90
90
|
Valid values are checked against `Num2words.available_locales`.
|
|
91
91
|
|
|
92
|
+
Error messages for `unsupported_currency` and `unsupported_locale` are included in the gem locale files. Rails apps can override them through the standard `errors.messages` keys.
|
|
93
|
+
|
|
92
94
|
## Metadata API
|
|
93
95
|
|
|
94
96
|
For forms, select inputs and settings screens, use the public metadata API:
|
|
@@ -99,6 +101,15 @@ Num2words.available_currencies(:ru)
|
|
|
99
101
|
Num2words.currency_available?(:ru, :RUB)
|
|
100
102
|
Num2words.currency_info(:ru, :RUB)
|
|
101
103
|
Num2words.default_currency_info(:ru)
|
|
104
|
+
Num2words.currency_options(:ru)
|
|
105
|
+
Num2words.locale_options
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
Rails form example:
|
|
109
|
+
|
|
110
|
+
```erb
|
|
111
|
+
<%= form.select :currency, Num2words.currency_options(I18n.locale) %>
|
|
112
|
+
<%= form.select :locale, Num2words.locale_options %>
|
|
102
113
|
```
|
|
103
114
|
|
|
104
115
|
## Manual Usage
|
data/docs/rails.ru.md
CHANGED
|
@@ -89,6 +89,8 @@ Payment.new(locale: "xx").valid?
|
|
|
89
89
|
|
|
90
90
|
Валидные значения проверяются через `Num2words.available_locales`.
|
|
91
91
|
|
|
92
|
+
Сообщения ошибок для `unsupported_currency` и `unsupported_locale` уже добавлены в locale files гема. Их можно переопределить в Rails-приложении стандартным способом через `errors.messages`.
|
|
93
|
+
|
|
92
94
|
## Metadata API
|
|
93
95
|
|
|
94
96
|
Для форм, select-полей и настроек можно использовать публичные методы metadata API:
|
|
@@ -99,6 +101,15 @@ Num2words.available_currencies(:ru)
|
|
|
99
101
|
Num2words.currency_available?(:ru, :RUB)
|
|
100
102
|
Num2words.currency_info(:ru, :RUB)
|
|
101
103
|
Num2words.default_currency_info(:ru)
|
|
104
|
+
Num2words.currency_options(:ru)
|
|
105
|
+
Num2words.locale_options
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
Пример для Rails form:
|
|
109
|
+
|
|
110
|
+
```erb
|
|
111
|
+
<%= form.select :currency, Num2words.currency_options(I18n.locale) %>
|
|
112
|
+
<%= form.select :locale, Num2words.locale_options %>
|
|
102
113
|
```
|
|
103
114
|
|
|
104
115
|
## Ручное использование
|
data/lib/num2words/config.rb
CHANGED
|
@@ -49,12 +49,30 @@ module Num2words
|
|
|
49
49
|
currency_info(locale, default_currency(locale))
|
|
50
50
|
end
|
|
51
51
|
|
|
52
|
+
def currency_options(locale = I18n.locale, format: :symbol)
|
|
53
|
+
validate_currency_options_format!(format)
|
|
54
|
+
|
|
55
|
+
available_currencies(locale).map do |code|
|
|
56
|
+
info = currency_info(locale, code)
|
|
57
|
+
|
|
58
|
+
[currency_option_label(info, format), code]
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
|
|
52
62
|
def available_locales
|
|
53
63
|
@available_locales ||= Dir[File.expand_path("../../config/locales/*.yml", __dir__)]
|
|
54
64
|
.map { |file| File.basename(file, ".yml").to_sym }
|
|
55
65
|
.sort
|
|
56
66
|
end
|
|
57
67
|
|
|
68
|
+
def locale_options(format: :code)
|
|
69
|
+
validate_locale_options_format!(format)
|
|
70
|
+
|
|
71
|
+
available_locales.map do |locale|
|
|
72
|
+
[locale_option_label(locale, format), locale]
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
|
|
58
76
|
def reset!(locale = nil)
|
|
59
77
|
return @local_currency.delete(locale) if locale
|
|
60
78
|
|
|
@@ -62,6 +80,34 @@ module Num2words
|
|
|
62
80
|
@local_currency = {}
|
|
63
81
|
@currency_warnings = true
|
|
64
82
|
end
|
|
83
|
+
|
|
84
|
+
private
|
|
85
|
+
|
|
86
|
+
def validate_currency_options_format!(format)
|
|
87
|
+
return if %i[symbol code name].include?(format)
|
|
88
|
+
|
|
89
|
+
raise UnsupportedOptionError.new(name: :format, value: format)
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def validate_locale_options_format!(format)
|
|
93
|
+
return if %i[code upcase].include?(format)
|
|
94
|
+
|
|
95
|
+
raise UnsupportedOptionError.new(name: :format, value: format)
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def currency_option_label(info, format)
|
|
99
|
+
case format
|
|
100
|
+
when :code then info[:code].to_s
|
|
101
|
+
when :name then info[:major_unit].first
|
|
102
|
+
else [info[:code], info[:symbol]].compact.join(" — ")
|
|
103
|
+
end
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
def locale_option_label(locale, format)
|
|
107
|
+
label = locale.to_s
|
|
108
|
+
|
|
109
|
+
format == :upcase ? label.upcase : label
|
|
110
|
+
end
|
|
65
111
|
end
|
|
66
112
|
|
|
67
113
|
def self.config
|
|
@@ -92,10 +138,18 @@ module Num2words
|
|
|
92
138
|
config.default_currency_info(locale)
|
|
93
139
|
end
|
|
94
140
|
|
|
141
|
+
def self.currency_options(locale = I18n.locale, format: :symbol)
|
|
142
|
+
config.currency_options(locale, format: format)
|
|
143
|
+
end
|
|
144
|
+
|
|
95
145
|
def self.available_locales
|
|
96
146
|
config.available_locales
|
|
97
147
|
end
|
|
98
148
|
|
|
149
|
+
def self.locale_options(format: :code)
|
|
150
|
+
config.locale_options(format: format)
|
|
151
|
+
end
|
|
152
|
+
|
|
99
153
|
def self.currency_warnings
|
|
100
154
|
config.currency_warnings
|
|
101
155
|
end
|
data/lib/num2words/converter.rb
CHANGED
|
@@ -33,7 +33,7 @@ module Num2words
|
|
|
33
33
|
else nil
|
|
34
34
|
end
|
|
35
35
|
|
|
36
|
-
raise
|
|
36
|
+
raise UnsupportedInputError, number if result.nil?
|
|
37
37
|
|
|
38
38
|
apply_case(result, word_case)
|
|
39
39
|
end
|
|
@@ -43,12 +43,15 @@ module Num2words
|
|
|
43
43
|
word_case = opts[:word_case] || :downcase
|
|
44
44
|
currency = (opts[:code] || Num2words.default_currency(locale)).to_s.upcase.to_sym
|
|
45
45
|
minor = opts[:minor] || :always
|
|
46
|
+
strict = opts[:strict] || false
|
|
46
47
|
|
|
47
48
|
validate_option!(:minor, minor, %i[always nonzero never])
|
|
48
49
|
|
|
49
50
|
unless Num2words.available_currencies(locale).include?(currency)
|
|
51
|
+
raise UnsupportedCurrencyError.new(locale: locale, currency: currency) if strict
|
|
52
|
+
|
|
50
53
|
warn I18n.t("num2words.warnings.currency_not_available",
|
|
51
|
-
currency: currency, locale: locale) if Num2words.currency_warnings
|
|
54
|
+
currency: currency, lang: locale, locale: locale) if Num2words.currency_warnings
|
|
52
55
|
currency = Num2words.default_currency(locale)
|
|
53
56
|
end
|
|
54
57
|
|
|
@@ -83,7 +86,7 @@ module Num2words
|
|
|
83
86
|
def validate_option!(name, value, allowed_values)
|
|
84
87
|
return if allowed_values.include?(value)
|
|
85
88
|
|
|
86
|
-
raise
|
|
89
|
+
raise UnsupportedOptionError.new(name: name, value: value)
|
|
87
90
|
end
|
|
88
91
|
|
|
89
92
|
def decimal_currency_amount(amount)
|
|
@@ -91,8 +94,8 @@ module Num2words
|
|
|
91
94
|
|
|
92
95
|
normalized_amount = amount.is_a?(String) ? amount.tr(",", ".") : amount.to_s
|
|
93
96
|
BigDecimal(normalized_amount)
|
|
94
|
-
rescue ArgumentError
|
|
95
|
-
raise
|
|
97
|
+
rescue ::ArgumentError
|
|
98
|
+
raise UnsupportedCurrencyAmountError, amount
|
|
96
99
|
end
|
|
97
100
|
|
|
98
101
|
def pluralize(number, singular, few, plural)
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Num2words
|
|
4
|
+
class Error < StandardError; end
|
|
5
|
+
|
|
6
|
+
class ArgumentError < ::ArgumentError; end
|
|
7
|
+
|
|
8
|
+
class UnsupportedInputError < ArgumentError
|
|
9
|
+
attr_reader :input
|
|
10
|
+
|
|
11
|
+
def initialize(input)
|
|
12
|
+
@input = input
|
|
13
|
+
|
|
14
|
+
super("Unsupported input type: #{input.inspect}")
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
class UnsupportedOptionError < ArgumentError
|
|
19
|
+
attr_reader :name, :value
|
|
20
|
+
|
|
21
|
+
def initialize(name:, value:)
|
|
22
|
+
@name = name
|
|
23
|
+
@value = value
|
|
24
|
+
|
|
25
|
+
super("Unsupported #{name} option: #{value.inspect}")
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
class UnsupportedCurrencyError < Error
|
|
30
|
+
attr_reader :locale, :currency
|
|
31
|
+
|
|
32
|
+
def initialize(locale:, currency:)
|
|
33
|
+
@locale = locale
|
|
34
|
+
@currency = currency
|
|
35
|
+
|
|
36
|
+
super("Currency #{currency} is not supported for locale #{locale}")
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
class UnsupportedCurrencyAmountError < ArgumentError
|
|
41
|
+
attr_reader :amount
|
|
42
|
+
|
|
43
|
+
def initialize(amount)
|
|
44
|
+
@amount = amount
|
|
45
|
+
|
|
46
|
+
super("Unsupported currency amount: #{amount.inspect}")
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
@@ -12,7 +12,7 @@ module Num2words
|
|
|
12
12
|
|
|
13
13
|
return if Num2words.currency_available?(locale, currency)
|
|
14
14
|
|
|
15
|
-
record.errors.add(attribute, :unsupported_currency, currency: currency,
|
|
15
|
+
record.errors.add(attribute, :unsupported_currency, currency: currency, num2words_locale: locale)
|
|
16
16
|
end
|
|
17
17
|
|
|
18
18
|
private
|
data/lib/num2words/version.rb
CHANGED
data/lib/num2words.rb
CHANGED
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: num2words
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.5.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Ruslan Fedotov
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: exe
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2026-07-
|
|
11
|
+
date: 2026-07-06 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: i18n
|
|
@@ -121,6 +121,7 @@ files:
|
|
|
121
121
|
- lib/num2words/console.rb
|
|
122
122
|
- lib/num2words/converter.rb
|
|
123
123
|
- lib/num2words/core_ext.rb
|
|
124
|
+
- lib/num2words/errors.rb
|
|
124
125
|
- lib/num2words/i18n.rb
|
|
125
126
|
- lib/num2words/locales.rb
|
|
126
127
|
- lib/num2words/locales/ar.rb
|