num2words 0.3.1 → 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 +4 -4
- data/CHANGELOG.md +21 -0
- data/Gemfile +1 -0
- data/README.md +57 -0
- data/docs/api.md +56 -0
- data/docs/api.ru.md +56 -0
- data/docs/rails.md +130 -0
- data/docs/rails.ru.md +130 -0
- data/lib/num2words/config.rb +44 -0
- data/lib/num2words/converter.rb +2 -1
- data/lib/num2words/rails/helpers.rb +27 -0
- data/lib/num2words/rails/validators/currency_validator.rb +30 -0
- data/lib/num2words/rails/validators/locale_validator.rb +22 -0
- data/lib/num2words/railtie.rb +27 -0
- data/lib/num2words/version.rb +1 -1
- data/lib/num2words.rb +2 -0
- metadata +8 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 288098773139982ac980a108f2bde3e3220c7b090cadd82cf8bada24053cce84
|
|
4
|
+
data.tar.gz: 0103cff58a94f52fd255aec76b83d4d487cd3767a7861ea25b2aff44c334797b
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: e9570c86cb884cd7abc6c8f16df6946d85ab8292c38a05a8567b48179a1aa04fdc3d4ecfb8252f358c35bcac4d093c5f236c04a63b16009ee04487b47d5fad3d
|
|
7
|
+
data.tar.gz: 613f2887655f9b5dcd1d9dcf927bf6d368337fa95abe2f2e7b909a87df7caf8d5ea88fda30e167ab22f9b624ef20bc68371ed916a52d239ea3156c9f6ef09bdb
|
data/CHANGELOG.md
CHANGED
|
@@ -11,6 +11,27 @@
|
|
|
11
11
|
|
|
12
12
|
---
|
|
13
13
|
|
|
14
|
+
## [0.4.0] - 2026-07-05
|
|
15
|
+
|
|
16
|
+
### Added
|
|
17
|
+
- Опциональные Rails helpers и Railtie:
|
|
18
|
+
- `number_to_words`
|
|
19
|
+
- `currency_to_words`
|
|
20
|
+
- `date_to_words`
|
|
21
|
+
- `time_to_words`
|
|
22
|
+
- `datetime_to_words`
|
|
23
|
+
- Интеграционные specs для проверки Railtie в минимальном Rails-приложении.
|
|
24
|
+
- Подключение Rails helpers в controller helper context.
|
|
25
|
+
- Валидатор ActiveModel `num2words_currency` для проверки валюты по локали.
|
|
26
|
+
- Валидатор ActiveModel `num2words_locale` для проверки поддерживаемой локали.
|
|
27
|
+
- Публичный metadata API `Num2words.available_locales`.
|
|
28
|
+
- Публичный metadata API `Num2words.currency_available?`.
|
|
29
|
+
- Публичный metadata API `Num2words.currency_info`.
|
|
30
|
+
- Публичный metadata API `Num2words.default_currency_info`.
|
|
31
|
+
- Документация по Rails-интеграции: `docs/rails.md` и `docs/rails.ru.md`.
|
|
32
|
+
|
|
33
|
+
---
|
|
34
|
+
|
|
14
35
|
## [0.3.1] - 2026-07-04
|
|
15
36
|
|
|
16
37
|
### Added
|
data/Gemfile
CHANGED
data/README.md
CHANGED
|
@@ -14,6 +14,9 @@ Ruby-гем для преобразования чисел, дробей, ден
|
|
|
14
14
|
- Даты, время и дата-время.
|
|
15
15
|
- Регистры вывода: `word_case: :upper | :downcase | :capitalize | :title`.
|
|
16
16
|
- Удобные методы для `Integer`, `Float`, `String`, `Date`, `Time`, `DateTime`.
|
|
17
|
+
- Опциональные Rails helpers и Railtie.
|
|
18
|
+
- ActiveModel validators для локалей и валют.
|
|
19
|
+
- Metadata API для локалей и валют.
|
|
17
20
|
- Интерактивная консоль `num2words-console`.
|
|
18
21
|
|
|
19
22
|
---
|
|
@@ -153,6 +156,60 @@ Num2words.to_currency(BigDecimal("21.05"), :ru)
|
|
|
153
156
|
- [English](docs/limits.md)
|
|
154
157
|
- [Русский](docs/limits.ru.md)
|
|
155
158
|
|
|
159
|
+
Rails-интеграция:
|
|
160
|
+
|
|
161
|
+
- [English](docs/rails.md)
|
|
162
|
+
- [Русский](docs/rails.ru.md)
|
|
163
|
+
|
|
164
|
+
---
|
|
165
|
+
|
|
166
|
+
## Rails
|
|
167
|
+
|
|
168
|
+
Если Rails доступен, helpers подключаются автоматически через Railtie.
|
|
169
|
+
|
|
170
|
+
```erb
|
|
171
|
+
<%= number_to_words(123, locale: :ru) %>
|
|
172
|
+
<%= currency_to_words(@payment.amount, locale: :ru, code: @payment.currency) %>
|
|
173
|
+
<%= date_to_words(Date.current, locale: I18n.locale) %>
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
Также доступны ActiveModel validators:
|
|
177
|
+
|
|
178
|
+
```ruby
|
|
179
|
+
class Payment < ApplicationRecord
|
|
180
|
+
validates :currency, num2words_currency: { locale: ->(payment) { payment.locale } }
|
|
181
|
+
validates :locale, num2words_locale: true
|
|
182
|
+
end
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
---
|
|
186
|
+
|
|
187
|
+
## Metadata API
|
|
188
|
+
|
|
189
|
+
```ruby
|
|
190
|
+
Num2words.available_locales
|
|
191
|
+
# => [:ar, :be, :bg, :bn, :cs, ...]
|
|
192
|
+
|
|
193
|
+
Num2words.currency_available?(:ru, :RUB)
|
|
194
|
+
# => true
|
|
195
|
+
|
|
196
|
+
Num2words.currency_info(:ru, :RUB)
|
|
197
|
+
# => {
|
|
198
|
+
# code: :RUB,
|
|
199
|
+
# major_unit: ["рубль", "рубля", "рублей"],
|
|
200
|
+
# minor_unit: ["копейка", "копейки", "копеек"],
|
|
201
|
+
# symbol: "₽"
|
|
202
|
+
# }
|
|
203
|
+
|
|
204
|
+
Num2words.default_currency_info(:ru)
|
|
205
|
+
# => {
|
|
206
|
+
# code: :RUB,
|
|
207
|
+
# major_unit: ["рубль", "рубля", "рублей"],
|
|
208
|
+
# minor_unit: ["копейка", "копейки", "копеек"],
|
|
209
|
+
# symbol: "₽"
|
|
210
|
+
# }
|
|
211
|
+
```
|
|
212
|
+
|
|
156
213
|
---
|
|
157
214
|
|
|
158
215
|
## Консоль
|
data/docs/api.md
CHANGED
|
@@ -364,6 +364,61 @@ Num2words.available_currencies(:ru)
|
|
|
364
364
|
# => [:RUB, :USD, :EUR, ...]
|
|
365
365
|
```
|
|
366
366
|
|
|
367
|
+
### `Num2words.currency_available?`
|
|
368
|
+
|
|
369
|
+
Checks whether a currency is available for a locale.
|
|
370
|
+
|
|
371
|
+
```ruby
|
|
372
|
+
Num2words.currency_available?(:ru, :RUB)
|
|
373
|
+
# => true
|
|
374
|
+
|
|
375
|
+
Num2words.currency_available?(:ru, "usd")
|
|
376
|
+
# => true
|
|
377
|
+
|
|
378
|
+
Num2words.currency_available?(:ru, :XYZ)
|
|
379
|
+
# => false
|
|
380
|
+
```
|
|
381
|
+
|
|
382
|
+
### `Num2words.currency_info`
|
|
383
|
+
|
|
384
|
+
Returns currency metadata for a locale or `nil` when the currency is unavailable.
|
|
385
|
+
|
|
386
|
+
```ruby
|
|
387
|
+
Num2words.currency_info(:ru, :RUB)
|
|
388
|
+
# => {
|
|
389
|
+
# code: :RUB,
|
|
390
|
+
# major_unit: ["рубль", "рубля", "рублей"],
|
|
391
|
+
# minor_unit: ["копейка", "копейки", "копеек"],
|
|
392
|
+
# symbol: "₽"
|
|
393
|
+
# }
|
|
394
|
+
|
|
395
|
+
Num2words.currency_info(:ru, :XYZ)
|
|
396
|
+
# => nil
|
|
397
|
+
```
|
|
398
|
+
|
|
399
|
+
### `Num2words.default_currency_info`
|
|
400
|
+
|
|
401
|
+
Returns metadata for the locale default currency.
|
|
402
|
+
|
|
403
|
+
```ruby
|
|
404
|
+
Num2words.default_currency_info(:ru)
|
|
405
|
+
# => {
|
|
406
|
+
# code: :RUB,
|
|
407
|
+
# major_unit: ["рубль", "рубля", "рублей"],
|
|
408
|
+
# minor_unit: ["копейка", "копейки", "копеек"],
|
|
409
|
+
# symbol: "₽"
|
|
410
|
+
# }
|
|
411
|
+
```
|
|
412
|
+
|
|
413
|
+
### `Num2words.available_locales`
|
|
414
|
+
|
|
415
|
+
Returns supported locale codes.
|
|
416
|
+
|
|
417
|
+
```ruby
|
|
418
|
+
Num2words.available_locales
|
|
419
|
+
# => [:ar, :be, :bg, :bn, :cs, ...]
|
|
420
|
+
```
|
|
421
|
+
|
|
367
422
|
### `Num2words.currency_warnings`
|
|
368
423
|
|
|
369
424
|
Controls warnings when requested currency is unavailable.
|
|
@@ -418,5 +473,6 @@ Num2words.to_currency("abc", :en)
|
|
|
418
473
|
## Development References
|
|
419
474
|
|
|
420
475
|
- [Numeric limits](limits.md)
|
|
476
|
+
- [Rails integration](rails.md)
|
|
421
477
|
- [Locale development guide](locale_development.md)
|
|
422
478
|
- [Russian locale development guide](locale_development.ru.md)
|
data/docs/api.ru.md
CHANGED
|
@@ -364,6 +364,61 @@ Num2words.available_currencies(:ru)
|
|
|
364
364
|
# => [:RUB, :USD, :EUR, ...]
|
|
365
365
|
```
|
|
366
366
|
|
|
367
|
+
### `Num2words.currency_available?`
|
|
368
|
+
|
|
369
|
+
Проверяет, доступна ли валюта для локали.
|
|
370
|
+
|
|
371
|
+
```ruby
|
|
372
|
+
Num2words.currency_available?(:ru, :RUB)
|
|
373
|
+
# => true
|
|
374
|
+
|
|
375
|
+
Num2words.currency_available?(:ru, "usd")
|
|
376
|
+
# => true
|
|
377
|
+
|
|
378
|
+
Num2words.currency_available?(:ru, :XYZ)
|
|
379
|
+
# => false
|
|
380
|
+
```
|
|
381
|
+
|
|
382
|
+
### `Num2words.currency_info`
|
|
383
|
+
|
|
384
|
+
Возвращает данные валюты для локали или `nil`, если валюта недоступна.
|
|
385
|
+
|
|
386
|
+
```ruby
|
|
387
|
+
Num2words.currency_info(:ru, :RUB)
|
|
388
|
+
# => {
|
|
389
|
+
# code: :RUB,
|
|
390
|
+
# major_unit: ["рубль", "рубля", "рублей"],
|
|
391
|
+
# minor_unit: ["копейка", "копейки", "копеек"],
|
|
392
|
+
# symbol: "₽"
|
|
393
|
+
# }
|
|
394
|
+
|
|
395
|
+
Num2words.currency_info(:ru, :XYZ)
|
|
396
|
+
# => nil
|
|
397
|
+
```
|
|
398
|
+
|
|
399
|
+
### `Num2words.default_currency_info`
|
|
400
|
+
|
|
401
|
+
Возвращает данные валюты по умолчанию для локали.
|
|
402
|
+
|
|
403
|
+
```ruby
|
|
404
|
+
Num2words.default_currency_info(:ru)
|
|
405
|
+
# => {
|
|
406
|
+
# code: :RUB,
|
|
407
|
+
# major_unit: ["рубль", "рубля", "рублей"],
|
|
408
|
+
# minor_unit: ["копейка", "копейки", "копеек"],
|
|
409
|
+
# symbol: "₽"
|
|
410
|
+
# }
|
|
411
|
+
```
|
|
412
|
+
|
|
413
|
+
### `Num2words.available_locales`
|
|
414
|
+
|
|
415
|
+
Возвращает список поддерживаемых локалей.
|
|
416
|
+
|
|
417
|
+
```ruby
|
|
418
|
+
Num2words.available_locales
|
|
419
|
+
# => [:ar, :be, :bg, :bn, :cs, ...]
|
|
420
|
+
```
|
|
421
|
+
|
|
367
422
|
### `Num2words.currency_warnings`
|
|
368
423
|
|
|
369
424
|
Управляет warning при запросе валюты, недоступной для локали.
|
|
@@ -418,5 +473,6 @@ Num2words.to_currency("abc", :en)
|
|
|
418
473
|
## Документация для разработки
|
|
419
474
|
|
|
420
475
|
- [Числовые ограничения](limits.ru.md)
|
|
476
|
+
- [Rails-интеграция](rails.ru.md)
|
|
421
477
|
- [Locale development guide](locale_development.md)
|
|
422
478
|
- [Русская инструкция по разработке локалей](locale_development.ru.md)
|
data/docs/rails.md
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
# Rails Integration
|
|
2
|
+
|
|
3
|
+
`num2words` provides optional Rails helpers without adding Rails as a runtime dependency.
|
|
4
|
+
|
|
5
|
+
If Rails is available, `Num2words::Railtie` automatically includes helpers into Action View. Outside Rails, the helper module can be included manually.
|
|
6
|
+
|
|
7
|
+
The integration is covered by specs with a minimal Rails application, but Rails remains a development/test dependency only.
|
|
8
|
+
|
|
9
|
+
## Helpers
|
|
10
|
+
|
|
11
|
+
Available helpers:
|
|
12
|
+
|
|
13
|
+
- `number_to_words(number, locale: I18n.locale, **options)`
|
|
14
|
+
- `currency_to_words(amount, locale: I18n.locale, **options)`
|
|
15
|
+
- `date_to_words(date, locale: I18n.locale, **options)`
|
|
16
|
+
- `time_to_words(time, locale: I18n.locale, **options)`
|
|
17
|
+
- `datetime_to_words(datetime, locale: I18n.locale, **options)`
|
|
18
|
+
|
|
19
|
+
## Rails Views
|
|
20
|
+
|
|
21
|
+
```erb
|
|
22
|
+
<%= number_to_words(123, locale: :ru) %>
|
|
23
|
+
<%= currency_to_words(@invoice.total, locale: :ru, code: :RUB) %>
|
|
24
|
+
<%= date_to_words(Date.current, locale: I18n.locale) %>
|
|
25
|
+
<%= time_to_words(Time.current, locale: I18n.locale) %>
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
When `locale:` is omitted, helpers use `I18n.locale`.
|
|
29
|
+
|
|
30
|
+
```erb
|
|
31
|
+
<%= number_to_words(123) %>
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Controllers
|
|
35
|
+
|
|
36
|
+
Helpers are also exposed through the controller helper context:
|
|
37
|
+
|
|
38
|
+
```ruby
|
|
39
|
+
class PaymentsController < ApplicationController
|
|
40
|
+
def show
|
|
41
|
+
@amount_in_words = helpers.currency_to_words(@payment.amount, locale: :ru, code: @payment.currency)
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## ActiveModel Validations
|
|
47
|
+
|
|
48
|
+
`num2words` provides a currency validator for Rails and ActiveModel models:
|
|
49
|
+
|
|
50
|
+
```ruby
|
|
51
|
+
class Payment < ApplicationRecord
|
|
52
|
+
validates :currency, num2words_currency: { locale: :ru }
|
|
53
|
+
end
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
Valid values are checked against `Num2words.currency_available?(locale, currency)`.
|
|
57
|
+
|
|
58
|
+
```ruby
|
|
59
|
+
Payment.new(currency: "RUB").valid?
|
|
60
|
+
# => true
|
|
61
|
+
|
|
62
|
+
Payment.new(currency: "XYZ").valid?
|
|
63
|
+
# => false
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
The locale can also be dynamic:
|
|
67
|
+
|
|
68
|
+
```ruby
|
|
69
|
+
class Payment < ApplicationRecord
|
|
70
|
+
validates :currency, num2words_currency: { locale: ->(payment) { payment.locale } }
|
|
71
|
+
end
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
Locale validation is also available:
|
|
75
|
+
|
|
76
|
+
```ruby
|
|
77
|
+
class Payment < ApplicationRecord
|
|
78
|
+
validates :locale, num2words_locale: true
|
|
79
|
+
end
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
```ruby
|
|
83
|
+
Payment.new(locale: "ru").valid?
|
|
84
|
+
# => true
|
|
85
|
+
|
|
86
|
+
Payment.new(locale: "xx").valid?
|
|
87
|
+
# => false
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
Valid values are checked against `Num2words.available_locales`.
|
|
91
|
+
|
|
92
|
+
## Metadata API
|
|
93
|
+
|
|
94
|
+
For forms, select inputs and settings screens, use the public metadata API:
|
|
95
|
+
|
|
96
|
+
```ruby
|
|
97
|
+
Num2words.available_locales
|
|
98
|
+
Num2words.available_currencies(:ru)
|
|
99
|
+
Num2words.currency_available?(:ru, :RUB)
|
|
100
|
+
Num2words.currency_info(:ru, :RUB)
|
|
101
|
+
Num2words.default_currency_info(:ru)
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
## Manual Usage
|
|
105
|
+
|
|
106
|
+
The helpers can be used without Rails:
|
|
107
|
+
|
|
108
|
+
```ruby
|
|
109
|
+
helper = Object.new.extend(Num2words::Rails::Helpers)
|
|
110
|
+
|
|
111
|
+
helper.number_to_words(123, locale: :en)
|
|
112
|
+
# => "one hundred twenty three"
|
|
113
|
+
|
|
114
|
+
helper.currency_to_words("12.50", locale: :ru, code: :USD)
|
|
115
|
+
# => "двенадцать долларов пятьдесят центов"
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
## Options
|
|
119
|
+
|
|
120
|
+
Helpers forward all options to `Num2words.to_words` or `Num2words.to_currency`.
|
|
121
|
+
|
|
122
|
+
```ruby
|
|
123
|
+
number_to_words(0.5, locale: :ru, joiner: :and)
|
|
124
|
+
# => "ноль и пять десятых"
|
|
125
|
+
|
|
126
|
+
currency_to_words(12, locale: :ru, minor: :nonzero)
|
|
127
|
+
# => "двенадцать рублей"
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
See [API reference](api.md) for the full option list.
|
data/docs/rails.ru.md
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
# Rails-интеграция
|
|
2
|
+
|
|
3
|
+
`num2words` предоставляет optional Rails helpers без добавления Rails как runtime-зависимости.
|
|
4
|
+
|
|
5
|
+
Если Rails доступен, `Num2words::Railtie` автоматически подключает helpers в Action View. Вне Rails модуль helpers можно подключить вручную.
|
|
6
|
+
|
|
7
|
+
Интеграция покрыта specs с минимальным Rails-приложением, но Rails остается зависимостью только для development/test.
|
|
8
|
+
|
|
9
|
+
## Helpers
|
|
10
|
+
|
|
11
|
+
Доступные helpers:
|
|
12
|
+
|
|
13
|
+
- `number_to_words(number, locale: I18n.locale, **options)`
|
|
14
|
+
- `currency_to_words(amount, locale: I18n.locale, **options)`
|
|
15
|
+
- `date_to_words(date, locale: I18n.locale, **options)`
|
|
16
|
+
- `time_to_words(time, locale: I18n.locale, **options)`
|
|
17
|
+
- `datetime_to_words(datetime, locale: I18n.locale, **options)`
|
|
18
|
+
|
|
19
|
+
## Rails views
|
|
20
|
+
|
|
21
|
+
```erb
|
|
22
|
+
<%= number_to_words(123, locale: :ru) %>
|
|
23
|
+
<%= currency_to_words(@invoice.total, locale: :ru, code: :RUB) %>
|
|
24
|
+
<%= date_to_words(Date.current, locale: I18n.locale) %>
|
|
25
|
+
<%= time_to_words(Time.current, locale: I18n.locale) %>
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
Если `locale:` не передан, helpers используют `I18n.locale`.
|
|
29
|
+
|
|
30
|
+
```erb
|
|
31
|
+
<%= number_to_words(123) %>
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Controllers
|
|
35
|
+
|
|
36
|
+
Helpers также доступны через controller helper context:
|
|
37
|
+
|
|
38
|
+
```ruby
|
|
39
|
+
class PaymentsController < ApplicationController
|
|
40
|
+
def show
|
|
41
|
+
@amount_in_words = helpers.currency_to_words(@payment.amount, locale: :ru, code: @payment.currency)
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## ActiveModel validations
|
|
47
|
+
|
|
48
|
+
`num2words` предоставляет валидатор валюты для Rails и ActiveModel моделей:
|
|
49
|
+
|
|
50
|
+
```ruby
|
|
51
|
+
class Payment < ApplicationRecord
|
|
52
|
+
validates :currency, num2words_currency: { locale: :ru }
|
|
53
|
+
end
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
Валидные значения проверяются через `Num2words.currency_available?(locale, currency)`.
|
|
57
|
+
|
|
58
|
+
```ruby
|
|
59
|
+
Payment.new(currency: "RUB").valid?
|
|
60
|
+
# => true
|
|
61
|
+
|
|
62
|
+
Payment.new(currency: "XYZ").valid?
|
|
63
|
+
# => false
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
Локаль может быть динамической:
|
|
67
|
+
|
|
68
|
+
```ruby
|
|
69
|
+
class Payment < ApplicationRecord
|
|
70
|
+
validates :currency, num2words_currency: { locale: ->(payment) { payment.locale } }
|
|
71
|
+
end
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
Также доступна проверка локали:
|
|
75
|
+
|
|
76
|
+
```ruby
|
|
77
|
+
class Payment < ApplicationRecord
|
|
78
|
+
validates :locale, num2words_locale: true
|
|
79
|
+
end
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
```ruby
|
|
83
|
+
Payment.new(locale: "ru").valid?
|
|
84
|
+
# => true
|
|
85
|
+
|
|
86
|
+
Payment.new(locale: "xx").valid?
|
|
87
|
+
# => false
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
Валидные значения проверяются через `Num2words.available_locales`.
|
|
91
|
+
|
|
92
|
+
## Metadata API
|
|
93
|
+
|
|
94
|
+
Для форм, select-полей и настроек можно использовать публичные методы metadata API:
|
|
95
|
+
|
|
96
|
+
```ruby
|
|
97
|
+
Num2words.available_locales
|
|
98
|
+
Num2words.available_currencies(:ru)
|
|
99
|
+
Num2words.currency_available?(:ru, :RUB)
|
|
100
|
+
Num2words.currency_info(:ru, :RUB)
|
|
101
|
+
Num2words.default_currency_info(:ru)
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
## Ручное использование
|
|
105
|
+
|
|
106
|
+
Helpers можно использовать без Rails:
|
|
107
|
+
|
|
108
|
+
```ruby
|
|
109
|
+
helper = Object.new.extend(Num2words::Rails::Helpers)
|
|
110
|
+
|
|
111
|
+
helper.number_to_words(123, locale: :en)
|
|
112
|
+
# => "one hundred twenty three"
|
|
113
|
+
|
|
114
|
+
helper.currency_to_words("12.50", locale: :ru, code: :USD)
|
|
115
|
+
# => "двенадцать долларов пятьдесят центов"
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
## Опции
|
|
119
|
+
|
|
120
|
+
Helpers передают все опции в `Num2words.to_words` или `Num2words.to_currency`.
|
|
121
|
+
|
|
122
|
+
```ruby
|
|
123
|
+
number_to_words(0.5, locale: :ru, joiner: :and)
|
|
124
|
+
# => "ноль и пять десятых"
|
|
125
|
+
|
|
126
|
+
currency_to_words(12, locale: :ru, minor: :nonzero)
|
|
127
|
+
# => "двенадцать рублей"
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
Полный список опций смотри в [справочнике API](api.ru.md).
|
data/lib/num2words/config.rb
CHANGED
|
@@ -27,6 +27,34 @@ module Num2words
|
|
|
27
27
|
I18n.t("num2words.currencies", locale: locale).keys.map(&:to_sym)
|
|
28
28
|
end
|
|
29
29
|
|
|
30
|
+
def currency_available?(locale, currency)
|
|
31
|
+
available_currencies(locale).include?(currency.to_s.upcase.to_sym)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def currency_info(locale, currency)
|
|
35
|
+
code = currency.to_s.upcase.to_sym
|
|
36
|
+
data = I18n.t("num2words.currencies", locale: locale)[code]
|
|
37
|
+
|
|
38
|
+
return unless data
|
|
39
|
+
|
|
40
|
+
{
|
|
41
|
+
code: code,
|
|
42
|
+
major_unit: data[:major_unit],
|
|
43
|
+
minor_unit: data[:minor_unit],
|
|
44
|
+
symbol: data[:symbol]
|
|
45
|
+
}
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def default_currency_info(locale = I18n.locale)
|
|
49
|
+
currency_info(locale, default_currency(locale))
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def available_locales
|
|
53
|
+
@available_locales ||= Dir[File.expand_path("../../config/locales/*.yml", __dir__)]
|
|
54
|
+
.map { |file| File.basename(file, ".yml").to_sym }
|
|
55
|
+
.sort
|
|
56
|
+
end
|
|
57
|
+
|
|
30
58
|
def reset!(locale = nil)
|
|
31
59
|
return @local_currency.delete(locale) if locale
|
|
32
60
|
|
|
@@ -52,6 +80,22 @@ module Num2words
|
|
|
52
80
|
config.available_currencies(locale)
|
|
53
81
|
end
|
|
54
82
|
|
|
83
|
+
def self.currency_available?(locale, currency)
|
|
84
|
+
config.currency_available?(locale, currency)
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def self.currency_info(locale, currency)
|
|
88
|
+
config.currency_info(locale, currency)
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def self.default_currency_info(locale = I18n.locale)
|
|
92
|
+
config.default_currency_info(locale)
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def self.available_locales
|
|
96
|
+
config.available_locales
|
|
97
|
+
end
|
|
98
|
+
|
|
55
99
|
def self.currency_warnings
|
|
56
100
|
config.currency_warnings
|
|
57
101
|
end
|
data/lib/num2words/converter.rb
CHANGED
|
@@ -383,7 +383,8 @@ module Num2words
|
|
|
383
383
|
time_format = short && only == :time ? :short : :default
|
|
384
384
|
|
|
385
385
|
date_part = to_words_date(datetime.to_date, locale, locale_data, format: date_format, date_case: date_case)
|
|
386
|
-
time_part = to_words_time(datetime.
|
|
386
|
+
time_part = to_words_time(Time.new(datetime.year, datetime.month, datetime.day, datetime.hour, datetime.min, datetime.sec),
|
|
387
|
+
locale, locale_data, format: time_format, short: short)
|
|
387
388
|
|
|
388
389
|
return date_part if only == :date
|
|
389
390
|
return time_part if only == :time
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Num2words
|
|
4
|
+
module Rails
|
|
5
|
+
module Helpers
|
|
6
|
+
def number_to_words(number, locale: I18n.locale, **options)
|
|
7
|
+
Num2words.to_words(number, locale: locale, **options)
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def currency_to_words(amount, locale: I18n.locale, **options)
|
|
11
|
+
Num2words.to_currency(amount, locale: locale, **options)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def date_to_words(date, locale: I18n.locale, **options)
|
|
15
|
+
Num2words.to_words(date, locale: locale, **options)
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def time_to_words(time, locale: I18n.locale, **options)
|
|
19
|
+
Num2words.to_words(time, locale: locale, **options)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def datetime_to_words(datetime, locale: I18n.locale, **options)
|
|
23
|
+
Num2words.to_words(datetime, locale: locale, **options)
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "active_model"
|
|
4
|
+
|
|
5
|
+
module Num2words
|
|
6
|
+
module Rails
|
|
7
|
+
module Validators
|
|
8
|
+
class CurrencyValidator < ActiveModel::EachValidator
|
|
9
|
+
def validate_each(record, attribute, value)
|
|
10
|
+
locale = resolve_locale(record)
|
|
11
|
+
currency = value.to_s.upcase.to_sym
|
|
12
|
+
|
|
13
|
+
return if Num2words.currency_available?(locale, currency)
|
|
14
|
+
|
|
15
|
+
record.errors.add(attribute, :unsupported_currency, currency: currency, locale: locale)
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
private
|
|
19
|
+
|
|
20
|
+
def resolve_locale(record)
|
|
21
|
+
locale = options[:locale]
|
|
22
|
+
locale.respond_to?(:call) ? locale.call(record) : locale || I18n.locale
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
class Num2wordsCurrencyValidator < Num2words::Rails::Validators::CurrencyValidator
|
|
30
|
+
end
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "active_model"
|
|
4
|
+
|
|
5
|
+
module Num2words
|
|
6
|
+
module Rails
|
|
7
|
+
module Validators
|
|
8
|
+
class LocaleValidator < ActiveModel::EachValidator
|
|
9
|
+
def validate_each(record, attribute, value)
|
|
10
|
+
locale = value.to_s.to_sym
|
|
11
|
+
|
|
12
|
+
return if Num2words.available_locales.include?(locale)
|
|
13
|
+
|
|
14
|
+
record.errors.add(attribute, :unsupported_locale, locale: locale)
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
class Num2wordsLocaleValidator < Num2words::Rails::Validators::LocaleValidator
|
|
22
|
+
end
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "rails/helpers"
|
|
4
|
+
|
|
5
|
+
begin
|
|
6
|
+
require "rails/railtie"
|
|
7
|
+
require_relative "rails/validators/currency_validator"
|
|
8
|
+
require_relative "rails/validators/locale_validator"
|
|
9
|
+
rescue LoadError
|
|
10
|
+
nil
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
if defined?(::Rails::Railtie)
|
|
14
|
+
module Num2words
|
|
15
|
+
class Railtie < ::Rails::Railtie
|
|
16
|
+
initializer "num2words.helpers" do
|
|
17
|
+
ActiveSupport.on_load(:action_view) do
|
|
18
|
+
include Num2words::Rails::Helpers
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
ActiveSupport.on_load(:action_controller) do
|
|
22
|
+
helper Num2words::Rails::Helpers if respond_to?(:helper)
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
data/lib/num2words/version.rb
CHANGED
data/lib/num2words.rb
CHANGED
|
@@ -6,6 +6,8 @@ require_relative "num2words/converter"
|
|
|
6
6
|
require_relative "num2words/core_ext"
|
|
7
7
|
require_relative "num2words/locales"
|
|
8
8
|
require_relative "num2words/config"
|
|
9
|
+
require_relative "num2words/rails/helpers"
|
|
10
|
+
require_relative "num2words/railtie"
|
|
9
11
|
|
|
10
12
|
module Num2words
|
|
11
13
|
def self.to_words(number, *args, **opts)
|
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.4.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-05 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: i18n
|
|
@@ -113,6 +113,8 @@ files:
|
|
|
113
113
|
- docs/limits.ru.md
|
|
114
114
|
- docs/locale_development.md
|
|
115
115
|
- docs/locale_development.ru.md
|
|
116
|
+
- docs/rails.md
|
|
117
|
+
- docs/rails.ru.md
|
|
116
118
|
- exe/num2words-console
|
|
117
119
|
- lib/num2words.rb
|
|
118
120
|
- lib/num2words/config.rb
|
|
@@ -171,6 +173,10 @@ files:
|
|
|
171
173
|
- lib/num2words/locales/ur.rb
|
|
172
174
|
- lib/num2words/locales/vi.rb
|
|
173
175
|
- lib/num2words/locales/zh.rb
|
|
176
|
+
- lib/num2words/rails/helpers.rb
|
|
177
|
+
- lib/num2words/rails/validators/currency_validator.rb
|
|
178
|
+
- lib/num2words/rails/validators/locale_validator.rb
|
|
179
|
+
- lib/num2words/railtie.rb
|
|
174
180
|
- lib/num2words/version.rb
|
|
175
181
|
- num2words.gemspec
|
|
176
182
|
homepage: https://github.com/skyrusx/num2words
|