humanize_enum 0.1.0 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6b421ba3bb248560b9d5f540bbd1dd4020146b75
4
- data.tar.gz: 6d56d3b92a6b0b33d4666a54fe000aaf66badeb2
3
+ metadata.gz: 804e00044895d3546ff2225c3158eb08f2583c2f
4
+ data.tar.gz: 6a4899271c7fee46227835094f2f782c55569752
5
5
  SHA512:
6
- metadata.gz: 768c590017ce2fd290acf07215b4aeb27f990a90a5fdb685a18eb09a1cf191a95a68a75f38f88e9f6c521f955cfbba94b0bd36a1d75e5262d1612774a05d98d5
7
- data.tar.gz: 407a4f428be0ca78a60897820adaa84c07ef0ebf1fe1edbe2d233d3609a69c885b4343ed5c274d07554ec1097c19229885831a75e4ed32b8da8d1585a6b8a245
6
+ metadata.gz: b48689b16e9b8d8a726e533247d711b20c520430b24cf126fb08e0be0c072430694d83194af0a68cb7d2408476b9ad82a20210bad63fd18d08752fad0c0afa34
7
+ data.tar.gz: 73cb3f897a809242573b97ce3abddea5d0c67ac163da1597ac86861f000a8c9bc2f20e6dd697369227b8a3f3fa31d9438fd4490b34630d1695f2f6b6be4247f9
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at max@buslaev.net. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in humanize_enum.gemspec
6
+ gemspec
7
+
8
+ gem 'rspec', '~> 3.0'
9
+ gem 'activesupport'
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Max Buslaev
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,104 @@
1
+ # Humanize Enum
2
+
3
+ Enhanced I18n support for ActiveRecord Enums.
4
+
5
+ ## Installation
6
+
7
+ Install the gem and add to the application's Gemfile by executing:
8
+
9
+ $ bundle add humanize_enum
10
+
11
+ If bundler is not being used to manage dependencies, install the gem by executing:
12
+
13
+ $ gem install humanize_enum
14
+
15
+ ## Usage
16
+
17
+ Declare your enums:
18
+
19
+ ```ruby
20
+ class Payment < ActiveRecord::Base
21
+ enum status: {initial: 0, paid: 1, error: 2}
22
+ end
23
+ ```
24
+
25
+ Add enum values to locale files:
26
+ ```yaml
27
+ en:
28
+ activerecord:
29
+ attributes:
30
+ payment:
31
+ status: Status
32
+ status/initial: Initial status
33
+ status/paid: Payment processed
34
+ status/error: Payment error
35
+ ```
36
+
37
+ And now the translation is available in every place you need it:
38
+
39
+ ```ruby
40
+ payment = Payment.last
41
+ payment.status # => :paid
42
+ payment.humanize_enum(:status) # => 'Payment processed'
43
+ ```
44
+
45
+ And to query a translation for a specific value
46
+ ```ruby
47
+ Payment.humanize_enum(:status, :error) # => 'Payment error'
48
+ ```
49
+
50
+ To get a hash of available enums and their translations
51
+ ```ruby
52
+ Payment.humanize_enums(:status)
53
+ payment.humanize_enums(:status)
54
+ # both will return
55
+ # {
56
+ # initial: 'Initial status',
57
+ # paid: 'Payment processed',
58
+ # error: 'Payment error'
59
+ # }
60
+
61
+
62
+
63
+ ```
64
+
65
+ ## Inspired by
66
+
67
+ - a post by [Juraj Kostolanský](https://www.kostolansky.sk/posts/localize-rails-enums/) and
68
+ his [gem](https://github.com/jkostolansky/human_enum_name)
69
+ - i18n keys lookup approach of [aasm i18n support](https://github.com/aasm/aasm/wiki/I18n-support)
70
+
71
+ ## Alternatives
72
+
73
+ - [human_enum_name](https://github.com/jkostolansky/human_enum_name)
74
+ - [ar_enum_i18n](https://github.com/dalpo/ar_enum_i18n)
75
+ - [translate_enum](https://github.com/shlima/translate_enum)
76
+
77
+ ## 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
81
+
82
+ ## Development
83
+
84
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can
85
+ also run `bin/console` for an interactive prompt that will allow you to experiment.
86
+
87
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the
88
+ version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version,
89
+ push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
90
+
91
+ ## Contributing
92
+
93
+ Bug reports and pull requests are welcome on GitHub at https://github.com/austerlitz/humanize_enum. This project is
94
+ intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to
95
+ the [code of conduct](https://github.com/austerlitz/humanize_enum/blob/master/CODE_OF_CONDUCT.md).
96
+
97
+ ## License
98
+
99
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
100
+
101
+ ## Code of Conduct
102
+
103
+ Everyone interacting in the HumanizeEnum project's codebases, issue trackers, chat rooms and mailing lists is expected
104
+ to follow the [code of conduct](https://github.com/austerlitz/humanize_enum/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
@@ -0,0 +1,75 @@
1
+ module HumanizeEnum
2
+ module Helpers
3
+ # Struct to be used in html selects and similar places
4
+ SelectOption = Struct.new(:id, :value, :text, :checked)
5
+
6
+ def self.included(base)
7
+ base.extend ClassMethods
8
+ end
9
+
10
+ module ClassMethods
11
+ # @param enum_name [String, Symbol]
12
+ # @param enum_value [String, Symbol]
13
+ # @return [String] translated value of an enum
14
+ # @raise [UnknownEnumKey] if enum_name is not a defined enum
15
+ # @example
16
+ # Payment.humanize_enum(:status, :pending) # => 'Pending'
17
+ def humanize_enum(enum_name, enum_value)
18
+ check_enum!(enum_name)
19
+ I18n.t("activerecord.attributes.#{model_name.i18n_key}.#{enum_name}/#{enum_value}")
20
+ end
21
+
22
+ # @return [Hash<String, String>] hash where key is enum name and value is its translation
23
+ # @example
24
+ # Payment.humanize_enums(:status)
25
+ # {
26
+ # initial: 'Initial status',
27
+ # paid: 'Payment processed',
28
+ # error: 'Payment error'
29
+ # }
30
+ def humanize_enums(enum_name)
31
+ enum_options(enum_name).map do |enum|
32
+ [enum.value, enum.text]
33
+ end.to_h
34
+ end
35
+
36
+ # @param enum_name [String, Symbol] enum key to be translated
37
+ # @return [Array<SelectOption>] array of structs with :id, :key, :text, :checked
38
+ # @raise [UnknownEnumKey] if enum_name is not a defined enum
39
+ def enum_options(enum_name)
40
+ check_enum!(enum_name)
41
+ enum_i18n_key = enum_name.to_s.pluralize
42
+ send(enum_i18n_key).map do |key, val|
43
+ SelectOption.new(val, key, humanize_enum(enum_name, key), nil)
44
+ end
45
+ end
46
+
47
+ # @param enum_name [String, Symbol]
48
+ # @raise [UnknownEnumKey] if enum_name is not a defined enum
49
+ def check_enum!(enum_name)
50
+ raise UnknownEnumKey unless defined_enums.keys.include?(enum_name.to_s)
51
+ end
52
+ end
53
+
54
+ # @return [String] translated enum value of an instance
55
+ # @example
56
+ # payment = Payment.new
57
+ # payment.status = 'initial'
58
+ # payment.humanize_enum(:status) # => 'Initial'
59
+ def humanize_enum(enum_name)
60
+ self.class.check_enum!(enum_name)
61
+ I18n.t("activerecord.attributes.#{self.class.model_name.i18n_key}.#{enum_name}/#{send(enum_name)}")
62
+ end
63
+
64
+ # @see .humanize_enums
65
+ def humanize_enums(enum_name)
66
+ self.class.humanize_enums(enum_name)
67
+ end
68
+
69
+ # @return [Array<SelectOption>]
70
+ # @see .enum_options
71
+ def enum_options(enum_name)
72
+ self.class.enum_options(enum_name)
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HumanizeEnum
4
+ VERSION = '0.1.6'
5
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'humanize_enum/version'
4
+ require_relative 'humanize_enum/helpers'
5
+ require 'active_support/lazy_load_hooks'
6
+
7
+ module HumanizeEnum
8
+
9
+ # Error for an unknown enum key
10
+ UnknownEnumKey = Class.new(StandardError)
11
+
12
+ if defined?(ActiveSupport.on_load)
13
+ ActiveSupport.on_load(:active_record) do
14
+ include HumanizeEnum::Helpers
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,4 @@
1
+ module HumanizeEnum
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ 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.0
4
+ version: 0.1.6
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-23 00:00:00.000000000 Z
11
+ date: 2022-11-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 4.2.0
19
+ version: '0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: 4.2.0
26
+ version: '0'
27
27
  description: Enhanced I18n support for ActiveRecord Enums. Simplifies enums translations
28
28
  and usage in views/serializers.
29
29
  email:
@@ -31,7 +31,17 @@ email:
31
31
  executables: []
32
32
  extensions: []
33
33
  extra_rdoc_files: []
34
- files: []
34
+ files:
35
+ - ".rspec"
36
+ - CODE_OF_CONDUCT.md
37
+ - Gemfile
38
+ - LICENSE.txt
39
+ - README.md
40
+ - Rakefile
41
+ - lib/humanize_enum.rb
42
+ - lib/humanize_enum/helpers.rb
43
+ - lib/humanize_enum/version.rb
44
+ - sig/humanize_enum.rbs
35
45
  homepage: https://github.com/austerlitz/humanize_enum
36
46
  licenses:
37
47
  - MIT