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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 804e00044895d3546ff2225c3158eb08f2583c2f
4
- data.tar.gz: 6a4899271c7fee46227835094f2f782c55569752
2
+ SHA256:
3
+ metadata.gz: aea84b7e18259d8a4baa05da18dc8aa65f3efe55763d2b973cce833dda7c73f2
4
+ data.tar.gz: e131ee1e1af380919831f7795fe1daf321e5c4b488ff4acf8bee5ccfe5bc0f5e
5
5
  SHA512:
6
- metadata.gz: b48689b16e9b8d8a726e533247d711b20c520430b24cf126fb08e0be0c072430694d83194af0a68cb7d2408476b9ad82a20210bad63fd18d08752fad0c0afa34
7
- data.tar.gz: 73cb3f897a809242573b97ce3abddea5d0c67ac163da1597ac86861f000a8c9bc2f20e6dd697369227b8a3f3fa31d9438fd4490b34630d1695f2f6b6be4247f9
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: 'Initial status',
57
- # paid: 'Payment processed',
58
- # error: 'Payment error'
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: 'Initial status',
27
- # paid: 'Payment processed',
28
- # error: 'Payment error'
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module HumanizeEnum
4
- VERSION = '0.1.6'
4
+ VERSION = '0.1.7'
5
5
  end
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.6
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: 2022-11-24 00:00:00.000000000 Z
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
- rubyforge_project:
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.