simple_enumeration 0.2.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: db482227bb77ad9212fc6077487d30784764163e517ecc6afba0edb2b2c64d77
4
+ data.tar.gz: 302b4da499d755e061cabfe66499bd4ad3de820c853b1f3f485a88ed09821462
5
+ SHA512:
6
+ metadata.gz: 3516edf3c82716566abc62d4d7c887e6498f7e8900a81b1c9e1ef641886cd6f8068e0d160667fce7914f3ae9c85c02f377682c92ed3d8034145354b5604d7af7
7
+ data.tar.gz: a870839c4f45381fa4b6320d1ecad91e78cfc0ffc78d722b4eef53a28a4bbdb83e01f5b970c7a380ff242d2eee1149af695a25d094d472f7f0e3632c508cc8e1
@@ -0,0 +1,16 @@
1
+ name: Lint
2
+
3
+ on: [push,pull_request]
4
+
5
+ jobs:
6
+ build:
7
+ runs-on: ubuntu-latest
8
+ steps:
9
+ - uses: actions/checkout@v2
10
+ - name: Set up Ruby
11
+ uses: ruby/setup-ruby@v1
12
+ with:
13
+ ruby-version: 3.0.2
14
+ bundler-cache: true
15
+ - name: Run rubocop
16
+ run: bundle exec rake rubocop
@@ -0,0 +1,16 @@
1
+ name: Specs
2
+
3
+ on: [push,pull_request]
4
+
5
+ jobs:
6
+ build:
7
+ runs-on: ubuntu-latest
8
+ steps:
9
+ - uses: actions/checkout@v2
10
+ - name: Set up Ruby
11
+ uses: ruby/setup-ruby@v1
12
+ with:
13
+ ruby-version: 3.0.2
14
+ bundler-cache: true
15
+ - name: Run specs
16
+ run: bundle exec rake spec
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,22 @@
1
+ require:
2
+ - rubocop-performance
3
+ - rubocop-rspec
4
+
5
+ AllCops:
6
+ NewCops: enable
7
+
8
+ Layout/LineLength:
9
+ Max: 150
10
+
11
+ Style/Documentation:
12
+ Enabled: false
13
+
14
+ Metrics/BlockLength:
15
+ Exclude:
16
+ - "spec/**/*.rb"
17
+
18
+ RSpec/MultipleExpectations:
19
+ Max: 20
20
+
21
+ RSpec/ExampleLength:
22
+ Max: 50
data/CHANGELOG.md ADDED
@@ -0,0 +1,9 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
+
7
+ ## [0.2.0] - 2021-10-27
8
+
9
+ First public release
@@ -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 norbert.malecki@icloud.com. 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,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in enumeration.gemspec
6
+ gemspec
7
+
8
+ gem 'rake'
9
+ gem 'rspec'
10
+ gem 'rubocop'
11
+ gem 'rubocop-performance'
12
+ gem 'rubocop-rake'
13
+ gem 'rubocop-rspec'
data/Gemfile.lock ADDED
@@ -0,0 +1,92 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ simple_enumeration (0.2.0)
5
+ activesupport
6
+ callee
7
+ dry-initializer
8
+ i18n
9
+
10
+ GEM
11
+ remote: https://rubygems.org/
12
+ specs:
13
+ activesupport (6.1.4.1)
14
+ concurrent-ruby (~> 1.0, >= 1.0.2)
15
+ i18n (>= 1.6, < 2)
16
+ minitest (>= 5.1)
17
+ tzinfo (~> 2.0)
18
+ zeitwerk (~> 2.3)
19
+ ast (2.4.2)
20
+ callee (0.3.5)
21
+ dry-initializer (>= 2.5, < 4.0)
22
+ coderay (1.1.3)
23
+ concurrent-ruby (1.1.9)
24
+ diff-lcs (1.4.4)
25
+ dry-initializer (3.0.4)
26
+ i18n (1.8.10)
27
+ concurrent-ruby (~> 1.0)
28
+ method_source (1.0.0)
29
+ minitest (5.14.4)
30
+ parallel (1.21.0)
31
+ parser (3.0.2.0)
32
+ ast (~> 2.4.1)
33
+ pry (0.14.1)
34
+ coderay (~> 1.1)
35
+ method_source (~> 1.0)
36
+ rainbow (3.0.0)
37
+ rake (13.0.6)
38
+ regexp_parser (2.1.1)
39
+ rexml (3.2.5)
40
+ rspec (3.10.0)
41
+ rspec-core (~> 3.10.0)
42
+ rspec-expectations (~> 3.10.0)
43
+ rspec-mocks (~> 3.10.0)
44
+ rspec-core (3.10.1)
45
+ rspec-support (~> 3.10.0)
46
+ rspec-expectations (3.10.1)
47
+ diff-lcs (>= 1.2.0, < 2.0)
48
+ rspec-support (~> 3.10.0)
49
+ rspec-mocks (3.10.2)
50
+ diff-lcs (>= 1.2.0, < 2.0)
51
+ rspec-support (~> 3.10.0)
52
+ rspec-support (3.10.2)
53
+ rubocop (1.22.2)
54
+ parallel (~> 1.10)
55
+ parser (>= 3.0.0.0)
56
+ rainbow (>= 2.2.2, < 4.0)
57
+ regexp_parser (>= 1.8, < 3.0)
58
+ rexml
59
+ rubocop-ast (>= 1.12.0, < 2.0)
60
+ ruby-progressbar (~> 1.7)
61
+ unicode-display_width (>= 1.4.0, < 3.0)
62
+ rubocop-ast (1.12.0)
63
+ parser (>= 3.0.1.1)
64
+ rubocop-performance (1.11.5)
65
+ rubocop (>= 1.7.0, < 2.0)
66
+ rubocop-ast (>= 0.4.0)
67
+ rubocop-rake (0.6.0)
68
+ rubocop (~> 1.0)
69
+ rubocop-rspec (2.5.0)
70
+ rubocop (~> 1.19)
71
+ ruby-progressbar (1.11.0)
72
+ tzinfo (2.0.4)
73
+ concurrent-ruby (~> 1.0)
74
+ unicode-display_width (2.1.0)
75
+ zeitwerk (2.5.1)
76
+
77
+ PLATFORMS
78
+ x86_64-darwin-20
79
+ x86_64-linux
80
+
81
+ DEPENDENCIES
82
+ pry
83
+ rake
84
+ rspec
85
+ rubocop
86
+ rubocop-performance
87
+ rubocop-rake
88
+ rubocop-rspec
89
+ simple_enumeration!
90
+
91
+ BUNDLED WITH
92
+ 2.2.22
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Norbert Małecki
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,41 @@
1
+ # Enumeration
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/simple_enumeration`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'simple_enumeration'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install simple_enumeration
20
+
21
+ ## Usage
22
+
23
+ See code in the file `examples/payment_status` and `spec/simple_enumeration/entity_spec`.
24
+
25
+ ## Development
26
+
27
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
28
+
29
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
30
+
31
+ ## Contributing
32
+
33
+ Bug reports and pull requests are welcome on GitHub at https://github.com/norbertmaleckii/simple-enumeration-rb. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/norbertmaleckii/simple-enumeration-rb/blob/main/CODE_OF_CONDUCT.md).
34
+
35
+ ## License
36
+
37
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
38
+
39
+ ## Code of Conduct
40
+
41
+ Everyone interacting in the Enumeration project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/norbertmaleckii/simple-enumeration-rb/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
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
+ require 'rubocop/rake_task'
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'simple_enumeration'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'simple_enumeration'
4
+
5
+ class PaymentStatus < SimpleEnumeration::Entity
6
+ define_basic_collection(
7
+ :unpaid,
8
+ :failed,
9
+ :expired,
10
+ completed: :paid
11
+ )
12
+
13
+ define_custom_collection(
14
+ :finished,
15
+ failed,
16
+ expired,
17
+ completed
18
+ )
19
+
20
+ def self.convert_from_provider(string)
21
+ case string
22
+ when 'ERROR_1', 'ERROR_2'
23
+ failed
24
+ when 'TIMEOUT'
25
+ expired
26
+ when 'PAID'
27
+ completed
28
+ else
29
+ unapid
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,72 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SimpleEnumeration
4
+ class Collection
5
+ extend Dry::Initializer
6
+
7
+ MissingTypeError = Class.new(StandardError)
8
+
9
+ option :name
10
+ option :types, default: proc { {} }
11
+
12
+ METHOD_NAME_SUFFIX = '_collection'
13
+ VALUE_METHOD_NAME_SUFFIX = '_collection_values'
14
+ VALUE_PREDICATE_METHOD_NAME_SUFFIX = '_collection_value?'
15
+ FOR_SELECT_METHOD_NAME_SUFFIX = '_collection_for_select'
16
+ HUMANIZED_METHOD_NAME_SUFFIX = '_collection_humanized'
17
+
18
+ def self.collection_name_from_method_name(method_name)
19
+ method_name.to_s.remove(METHOD_NAME_SUFFIX)
20
+ end
21
+
22
+ def self.collection_name_from_value_method_name(method_name)
23
+ method_name.to_s.remove(VALUE_METHOD_NAME_SUFFIX)
24
+ end
25
+
26
+ def self.collection_name_from_value_predicate_method_name(method_name)
27
+ method_name.to_s.remove(VALUE_PREDICATE_METHOD_NAME_SUFFIX)
28
+ end
29
+
30
+ def self.collection_name_from_for_select_method_name(method_name)
31
+ method_name.to_s.remove(FOR_SELECT_METHOD_NAME_SUFFIX)
32
+ end
33
+
34
+ def self.collection_name_from_humanized_method_name(method_name)
35
+ method_name.to_s.remove(HUMANIZED_METHOD_NAME_SUFFIX)
36
+ end
37
+
38
+ def method_name
39
+ [name, METHOD_NAME_SUFFIX].join
40
+ end
41
+
42
+ def value_method_name
43
+ [name, VALUE_METHOD_NAME_SUFFIX].join
44
+ end
45
+
46
+ def value_predicate_method_name
47
+ [name, VALUE_PREDICATE_METHOD_NAME_SUFFIX].join
48
+ end
49
+
50
+ def for_select_method_name
51
+ [name, FOR_SELECT_METHOD_NAME_SUFFIX].join
52
+ end
53
+
54
+ def humanized_method_name
55
+ [name, HUMANIZED_METHOD_NAME_SUFFIX].join
56
+ end
57
+
58
+ def get_type(name)
59
+ types[name].tap do |value|
60
+ raise MissingTypeError, "Missing type for #{name}" unless value
61
+ end
62
+ end
63
+
64
+ def add_type(name, value)
65
+ types[name] = value
66
+ end
67
+
68
+ def remove_type(name)
69
+ types[name] = nil
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,76 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SimpleEnumeration
4
+ class CollectionMethodsDefiner
5
+ include Callee
6
+
7
+ option :enum_class
8
+ option :collection
9
+
10
+ def call
11
+ set_collection
12
+ define_singleton_collection
13
+ define_singleton_collection_values_method
14
+ define_singleton_collection_value_predicate_method
15
+ define_singleton_collection_for_select_method
16
+ define_singleton_collection_humanized_method
17
+ define_instance_collection_value_predicate_method
18
+ end
19
+
20
+ private
21
+
22
+ def set_collection
23
+ enum_class.set_collection(collection.name, collection)
24
+ end
25
+
26
+ def define_singleton_collection
27
+ enum_class.define_singleton_method collection.method_name do
28
+ collection_name = Collection.collection_name_from_method_name(__method__)
29
+
30
+ get_collection(collection_name)
31
+ end
32
+ end
33
+
34
+ def define_singleton_collection_values_method
35
+ enum_class.define_singleton_method collection.value_method_name do
36
+ collection_name = Collection.collection_name_from_value_method_name(__method__)
37
+
38
+ get_collection(collection_name).types.values.map(&:converted_value)
39
+ end
40
+ end
41
+
42
+ def define_singleton_collection_value_predicate_method
43
+ enum_class.define_singleton_method collection.value_predicate_method_name do |converted_value|
44
+ collection_name = Collection.collection_name_from_value_predicate_method_name(__method__)
45
+
46
+ get_collection(collection_name).types.values.map(&:converted_value).include?(converted_value)
47
+ end
48
+ end
49
+
50
+ def define_singleton_collection_for_select_method
51
+ enum_class.define_singleton_method collection.for_select_method_name do
52
+ collection_name = Collection.collection_name_from_for_select_method_name(__method__)
53
+
54
+ get_collection(collection_name).types.values.map(&:for_select)
55
+ end
56
+ end
57
+
58
+ def define_singleton_collection_humanized_method
59
+ enum_class.define_singleton_method collection.humanized_method_name do
60
+ collection_name = Collection.collection_name_from_humanized_method_name(__method__)
61
+
62
+ get_collection(collection_name).types.values.map(&:humanized)
63
+ end
64
+ end
65
+
66
+ def define_instance_collection_value_predicate_method
67
+ enum_class.define_method collection.value_predicate_method_name do
68
+ return false unless type
69
+
70
+ collection_name = Collection.collection_name_from_value_predicate_method_name(__method__)
71
+
72
+ self.class.get_collection(collection_name).types.values.map(&:converted_value).include?(type.converted_value)
73
+ end
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SimpleEnumeration
4
+ module Collections
5
+ class BasicFactory
6
+ include Callee
7
+
8
+ option :enum_class
9
+ option :collection_name, default: proc { :basic }
10
+ option :definitions
11
+
12
+ def call
13
+ collection = Collection.new(name: collection_name)
14
+
15
+ CollectionMethodsDefiner.call(enum_class: enum_class, collection: collection)
16
+
17
+ definitions.each do |definition|
18
+ type = TypeBuilder.call(definition: definition, enum_class: enum_class)
19
+
20
+ TypeMethodsDefiner.call(enum_class: enum_class, collection: collection, type: type)
21
+
22
+ collection.add_type(type.value, type)
23
+ end
24
+
25
+ collection
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SimpleEnumeration
4
+ module Collections
5
+ class CustomFactory
6
+ include Callee
7
+
8
+ option :enum_class
9
+ option :collection_name
10
+ option :types
11
+
12
+ def call
13
+ collection = Collection.new(name: collection_name)
14
+
15
+ CollectionMethodsDefiner.call(enum_class: enum_class, collection: collection)
16
+
17
+ types.each do |type|
18
+ collection.add_type(type.value, type)
19
+ end
20
+
21
+ collection
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SimpleEnumeration
4
+ class Entity
5
+ extend Dry::Initializer
6
+
7
+ option :converted_value
8
+
9
+ def self.define_basic_collection(*definitions)
10
+ Collections::BasicFactory.call(
11
+ enum_class: self,
12
+ definitions: definitions
13
+ )
14
+ end
15
+
16
+ def self.define_custom_collection(collection_name, *types)
17
+ Collections::CustomFactory.call(
18
+ enum_class: self,
19
+ collection_name: collection_name,
20
+ types: types
21
+ )
22
+ end
23
+
24
+ def self.get_collection(name)
25
+ instance_variable_get("@#{name}")
26
+ end
27
+
28
+ def self.set_collection(name, value)
29
+ instance_variable_set("@#{name}", value)
30
+ end
31
+
32
+ def type
33
+ @type ||= self.class.basic_collection.types.values.find do |type|
34
+ type.converted_value.to_s == converted_value.to_s
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SimpleEnumeration
4
+ class Type
5
+ extend Dry::Initializer
6
+
7
+ option :definition
8
+ option :value
9
+ option :converted_value
10
+ option :enum_class
11
+
12
+ VALUE_METHOD_NAME_SUFFIX = '_value'
13
+ VALUE_PREDICATE_METHOD_NAME_SUFFIX = '_value?'
14
+
15
+ def self.type_value_for_method_name(method_name)
16
+ method_name.to_s
17
+ end
18
+
19
+ def self.type_value_for_value_method_name(method_name)
20
+ method_name.to_s.remove(VALUE_METHOD_NAME_SUFFIX)
21
+ end
22
+
23
+ def self.type_value_for_value_predicate_method_name(method_name)
24
+ method_name.to_s.remove(VALUE_PREDICATE_METHOD_NAME_SUFFIX)
25
+ end
26
+
27
+ def method_name
28
+ value
29
+ end
30
+
31
+ def value_method_name
32
+ [value, VALUE_METHOD_NAME_SUFFIX].join
33
+ end
34
+
35
+ def value_predicate_method_name
36
+ [value, VALUE_PREDICATE_METHOD_NAME_SUFFIX].join
37
+ end
38
+
39
+ def for_select
40
+ [humanized, converted_value]
41
+ end
42
+
43
+ def humanized
44
+ translations[:text]
45
+ end
46
+
47
+ def meta
48
+ translations.except(:text)
49
+ end
50
+
51
+ def translations
52
+ I18n.t(
53
+ value,
54
+ raise: true,
55
+ scope: [
56
+ 'simple_enumeration',
57
+ enum_class.name.remove('Enumeration').underscore
58
+ ].join('.')
59
+ )
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SimpleEnumeration
4
+ class TypeBuilder
5
+ include Callee
6
+
7
+ option :enum_class
8
+ option :definition
9
+
10
+ def call
11
+ factory_klass.call(
12
+ enum_class: enum_class,
13
+ definition: definition
14
+ )
15
+ end
16
+
17
+ def factory_klass
18
+ if definition.is_a?(Hash)
19
+ Types::HashFactory
20
+ else
21
+ Types::StringFactory
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SimpleEnumeration
4
+ class TypeMethodsDefiner
5
+ include Callee
6
+
7
+ option :enum_class
8
+ option :collection
9
+ option :type
10
+
11
+ def call
12
+ define_singleton_type_method
13
+ define_singleton_type_value_method
14
+ define_singleton_type_value_predicate_method
15
+ define_instance_type_value_predicate_method
16
+ end
17
+
18
+ private
19
+
20
+ def define_singleton_type_method
21
+ enum_class.define_singleton_method type.method_name do
22
+ type_value = Type.type_value_for_method_name(__method__)
23
+
24
+ basic_collection.get_type(type_value)
25
+ end
26
+ end
27
+
28
+ def define_singleton_type_value_method
29
+ enum_class.define_singleton_method type.value_method_name do
30
+ type_value = Type.type_value_for_value_method_name(__method__)
31
+
32
+ basic_collection.get_type(type_value).converted_value
33
+ end
34
+ end
35
+
36
+ def define_singleton_type_value_predicate_method
37
+ enum_class.define_singleton_method type.value_predicate_method_name do |converted_value|
38
+ type_value = Type.type_value_for_value_predicate_method_name(__method__)
39
+
40
+ basic_collection.get_type(type_value).converted_value == converted_value
41
+ end
42
+ end
43
+
44
+ def define_instance_type_value_predicate_method
45
+ enum_class.define_method type.value_predicate_method_name do
46
+ return false unless type
47
+
48
+ type_value = Type.type_value_for_value_predicate_method_name(__method__)
49
+
50
+ self.class.basic_collection.get_type(type_value).converted_value == type.converted_value
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SimpleEnumeration
4
+ module Types
5
+ class BaseFactory
6
+ include Callee
7
+
8
+ option :definition
9
+ option :enum_class
10
+
11
+ def call
12
+ Type.new(
13
+ definition: definition,
14
+ value: value,
15
+ converted_value: converted_value,
16
+ enum_class: enum_class
17
+ )
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SimpleEnumeration
4
+ module Types
5
+ class HashFactory < BaseFactory
6
+ def value
7
+ definition.first.first.to_s
8
+ end
9
+
10
+ def converted_value
11
+ definition.first.last.to_s
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SimpleEnumeration
4
+ module Types
5
+ class StringFactory < BaseFactory
6
+ def value
7
+ definition.to_s
8
+ end
9
+
10
+ def converted_value
11
+ definition.to_s
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SimpleEnumeration
4
+ VERSION = '0.2.0'
5
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/core_ext/string/filters'
4
+ require 'active_support/core_ext/string/inflections'
5
+ require 'callee'
6
+ require 'dry/initializer'
7
+ require 'i18n'
8
+
9
+ require_relative 'simple_enumeration/version'
10
+ require_relative 'simple_enumeration/type'
11
+ require_relative 'simple_enumeration/types/base_factory'
12
+ require_relative 'simple_enumeration/types/hash_factory'
13
+ require_relative 'simple_enumeration/types/string_factory'
14
+ require_relative 'simple_enumeration/type_builder'
15
+ require_relative 'simple_enumeration/type_methods_definer'
16
+ require_relative 'simple_enumeration/collection'
17
+ require_relative 'simple_enumeration/collections/basic_factory'
18
+ require_relative 'simple_enumeration/collections/custom_factory'
19
+ require_relative 'simple_enumeration/collection_methods_definer'
20
+ require_relative 'simple_enumeration/entity'
21
+
22
+ module SimpleEnumeration
23
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/simple_enumeration/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'simple_enumeration'
7
+ spec.version = SimpleEnumeration::VERSION
8
+ spec.authors = ['Norbert Małecki']
9
+ spec.email = ['norbert.malecki@icloud.com']
10
+
11
+ spec.summary = 'Enumeration'
12
+ spec.description = 'Enumeration'
13
+ spec.homepage = 'https://github.com/norbertmaleckii/simple-enumeration-rb'
14
+ spec.license = 'MIT'
15
+ spec.required_ruby_version = '>= 2.5.0'
16
+
17
+ spec.metadata['homepage_uri'] = spec.homepage
18
+ spec.metadata['source_code_uri'] = spec.homepage
19
+ spec.metadata['changelog_uri'] = "#{spec.homepage}/CHANGELOG.md"
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
24
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
25
+ end
26
+ spec.bindir = 'exe'
27
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ['lib']
29
+
30
+ spec.add_dependency 'activesupport'
31
+ spec.add_dependency 'callee'
32
+ spec.add_dependency 'dry-initializer'
33
+ spec.add_dependency 'i18n'
34
+
35
+ spec.add_development_dependency 'pry'
36
+ end
metadata ADDED
@@ -0,0 +1,145 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple_enumeration
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Norbert Małecki
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-10-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: callee
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: dry-initializer
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: i18n
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Enumeration
84
+ email:
85
+ - norbert.malecki@icloud.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".github/workflows/lint.yml"
91
+ - ".github/workflows/specs.yml"
92
+ - ".gitignore"
93
+ - ".rspec"
94
+ - ".rubocop.yml"
95
+ - CHANGELOG.md
96
+ - CODE_OF_CONDUCT.md
97
+ - Gemfile
98
+ - Gemfile.lock
99
+ - LICENSE.txt
100
+ - README.md
101
+ - Rakefile
102
+ - bin/console
103
+ - bin/setup
104
+ - examples/payment_status.rb
105
+ - lib/simple_enumeration.rb
106
+ - lib/simple_enumeration/collection.rb
107
+ - lib/simple_enumeration/collection_methods_definer.rb
108
+ - lib/simple_enumeration/collections/basic_factory.rb
109
+ - lib/simple_enumeration/collections/custom_factory.rb
110
+ - lib/simple_enumeration/entity.rb
111
+ - lib/simple_enumeration/type.rb
112
+ - lib/simple_enumeration/type_builder.rb
113
+ - lib/simple_enumeration/type_methods_definer.rb
114
+ - lib/simple_enumeration/types/base_factory.rb
115
+ - lib/simple_enumeration/types/hash_factory.rb
116
+ - lib/simple_enumeration/types/string_factory.rb
117
+ - lib/simple_enumeration/version.rb
118
+ - simple_enumeration.gemspec
119
+ homepage: https://github.com/norbertmaleckii/simple-enumeration-rb
120
+ licenses:
121
+ - MIT
122
+ metadata:
123
+ homepage_uri: https://github.com/norbertmaleckii/simple-enumeration-rb
124
+ source_code_uri: https://github.com/norbertmaleckii/simple-enumeration-rb
125
+ changelog_uri: https://github.com/norbertmaleckii/simple-enumeration-rb/CHANGELOG.md
126
+ post_install_message:
127
+ rdoc_options: []
128
+ require_paths:
129
+ - lib
130
+ required_ruby_version: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ version: 2.5.0
135
+ required_rubygems_version: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - ">="
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ requirements: []
141
+ rubygems_version: 3.2.22
142
+ signing_key:
143
+ specification_version: 4
144
+ summary: Enumeration
145
+ test_files: []