humanize_enum 0.1.6 → 0.1.8

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: f58656198e18e6351df8adad2213dba5ea46c9cc6fde6ae3a0fdf2ea43584961
4
+ data.tar.gz: d3d98c03cb1827c7e83ba3ea037eebe8db398ad88eee0c7184a15cd0e196c541
5
5
  SHA512:
6
- metadata.gz: b48689b16e9b8d8a726e533247d711b20c520430b24cf126fb08e0be0c072430694d83194af0a68cb7d2408476b9ad82a20210bad63fd18d08752fad0c0afa34
7
- data.tar.gz: 73cb3f897a809242573b97ce3abddea5d0c67ac163da1597ac86861f000a8c9bc2f20e6dd697369227b8a3f3fa31d9438fd4490b34630d1695f2f6b6be4247f9
6
+ metadata.gz: 43316746804a9f0c50a5615e7edb75b0b741e2d46853dc777c6da5cf07df0705cbf3d36a0063ad4d34126d946021f7160a58747ea2fd314527db158a187f1efe
7
+ data.tar.gz: b6e9774193ace25f8421cafd9ce3faecd4a89e2ad180cb22fc35788b047ae166028be52ab16bff4477ccf80920ad5a4d4d81e2de16ef6ca033b7930657e0a60d
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
@@ -75,9 +84,9 @@ payment.humanize_enums(:status)
75
84
  - [translate_enum](https://github.com/shlima/translate_enum)
76
85
 
77
86
  ## TODO
78
- - check input parameters for presence in enums
79
- - add some specs?
80
- - maybe some justification on why on earth make yet another gem on enum i18n
87
+ - [x] check input parameters for presence in enums
88
+ - [x] add some specs?
89
+ - [ ] maybe some justification on why on earth make yet another gem on enum i18n
81
90
 
82
91
  ## Development
83
92
 
@@ -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.8'
5
5
  end
data/lib/humanize_enum.rb CHANGED
@@ -3,6 +3,7 @@
3
3
  require_relative 'humanize_enum/version'
4
4
  require_relative 'humanize_enum/helpers'
5
5
  require 'active_support/lazy_load_hooks'
6
+ require 'active_support/inflector'
6
7
 
7
8
  module HumanizeEnum
8
9
 
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.8
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-07-31 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.4.13
67
66
  signing_key:
68
67
  specification_version: 4
69
68
  summary: Enhanced I18n support for ActiveRecord Enums.