humanize_enum 0.1.6 → 0.1.7
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 +5 -5
- data/README.md +12 -3
- data/lib/humanize_enum/helpers.rb +13 -3
- data/lib/humanize_enum/version.rb +1 -1
- metadata +3 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: aea84b7e18259d8a4baa05da18dc8aa65f3efe55763d2b973cce833dda7c73f2
|
4
|
+
data.tar.gz: e131ee1e1af380919831f7795fe1daf321e5c4b488ff4acf8bee5ccfe5bc0f5e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9efc1abfc5a447f51183907f315be5f08b372588fb72ded5a302e47b58cb9f495cc6ed5a7e0f8b837130baff36a5294e2f90d2250160e483ae9ad4ab03fa202b
|
7
|
+
data.tar.gz: 2bc8f16455ab065fc12f436033ff5ceda2dcfa8218ed6c36ae9ad43783656eeeb4bc97016ef01b7588f8df2a358e346b9fbd53ca46ee877e7f80f8ca53afe4a7
|
data/README.md
CHANGED
@@ -53,13 +53,22 @@ Payment.humanize_enums(:status)
|
|
53
53
|
payment.humanize_enums(:status)
|
54
54
|
# both will return
|
55
55
|
# {
|
56
|
-
# initial
|
57
|
-
#
|
58
|
-
#
|
56
|
+
# "initial" => "Initial status",
|
57
|
+
# "paid" => "Payment processed",
|
58
|
+
# "error" => "Payment error"
|
59
59
|
# }
|
60
60
|
|
61
61
|
|
62
62
|
|
63
|
+
```
|
64
|
+
|
65
|
+
Or if the incoming data is a text label, get its enum_value:
|
66
|
+
```ruby
|
67
|
+
str = 'Payment error'
|
68
|
+
Payment.dehumanize_enum(:status, str) # => "error"
|
69
|
+
|
70
|
+
# or nil if the label does not correspond to a translation:
|
71
|
+
Payment.dehumanize_enum(:status, 'blah') # => nil
|
63
72
|
```
|
64
73
|
|
65
74
|
## Inspired by
|
@@ -23,9 +23,9 @@ module HumanizeEnum
|
|
23
23
|
# @example
|
24
24
|
# Payment.humanize_enums(:status)
|
25
25
|
# {
|
26
|
-
# initial
|
27
|
-
#
|
28
|
-
#
|
26
|
+
# "initial" => "Initial status",
|
27
|
+
# "paid" => "Payment processed",
|
28
|
+
# "error" => "Payment error"
|
29
29
|
# }
|
30
30
|
def humanize_enums(enum_name)
|
31
31
|
enum_options(enum_name).map do |enum|
|
@@ -33,6 +33,16 @@ module HumanizeEnum
|
|
33
33
|
end.to_h
|
34
34
|
end
|
35
35
|
|
36
|
+
# Get enum value from a translated enum text.
|
37
|
+
# Useful for parsing incoming i18n-ed text labels that were generated by +humanize_enum+
|
38
|
+
#
|
39
|
+
# @param enum_name [String, Symbol] enum name
|
40
|
+
# @param enum_text [String] text value of the enum
|
41
|
+
# @return [Integer, String, NilClass]
|
42
|
+
def dehumanize_enum(enum_name, enum_text)
|
43
|
+
humanize_enums(enum_name).invert[enum_text]
|
44
|
+
end
|
45
|
+
|
36
46
|
# @param enum_name [String, Symbol] enum key to be translated
|
37
47
|
# @return [Array<SelectOption>] array of structs with :id, :key, :text, :checked
|
38
48
|
# @raise [UnknownEnumKey] if enum_name is not a defined enum
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: humanize_enum
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Max Buslaev
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-01-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -62,8 +62,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
62
62
|
- !ruby/object:Gem::Version
|
63
63
|
version: '0'
|
64
64
|
requirements: []
|
65
|
-
|
66
|
-
rubygems_version: 2.5.2
|
65
|
+
rubygems_version: 3.3.7
|
67
66
|
signing_key:
|
68
67
|
specification_version: 4
|
69
68
|
summary: Enhanced I18n support for ActiveRecord Enums.
|