enum_for 0.1.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 7eeb87db303be73cfbd4952dfd4aa0a2260ee855358d4b2dc8e94120d456ab02
4
+ data.tar.gz: cd66c71fcad2101627bda0b09c40c72d01629bb39c33156d0d4022b0c84180ac
5
+ SHA512:
6
+ metadata.gz: 12a1907d85fd076db39c6bb42c663fc2fd9b6cb445190b8fa8b3eeb1cd49129f29b5514ec09d6f3012be0e19b2469915976f187ff5cd9ea9f22989a03d584c27
7
+ data.tar.gz: 41535f19fe0710b8db81096a1a9095df66ce14b2e7550ab22854fb015fa4540d0b243030c384abefa3abfc58a205634a1ccf4abd13e48b204a522e456edc9f46
@@ -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
@@ -0,0 +1,57 @@
1
+ Layout/IndentationConsistency:
2
+ EnforcedStyle: indented_internal_methods
3
+
4
+ require:
5
+ - rubocop-performance
6
+ - rubocop-rspec
7
+
8
+ Rails:
9
+ Enabled: true
10
+
11
+ RSpec:
12
+ Enabled: true
13
+
14
+ RSpec/MultipleExpectations:
15
+ Enabled: false
16
+
17
+ Performance:
18
+ Enabled: true
19
+
20
+ Bundler:
21
+ Enabled: true
22
+
23
+ Gemspec:
24
+ Enabled: true
25
+
26
+ Style/StringLiterals:
27
+ Enabled: true
28
+ EnforcedStyle: single_quotes
29
+
30
+ Style/FrozenStringLiteralComment:
31
+ Enabled: false
32
+
33
+ Metrics/LineLength:
34
+ Max: 80
35
+
36
+ Metrics/MethodLength:
37
+ Enabled: false
38
+
39
+ Metrics/AbcSize:
40
+ Enabled: false
41
+
42
+ Metrics/BlockLength:
43
+ Exclude:
44
+ - 'spec/**/*_spec.rb'
45
+ - '**/*.gemspec'
46
+
47
+ Style/BlockDelimiters:
48
+ Enabled: true
49
+
50
+ RSpec/MessageSpies:
51
+ Enabled: false
52
+
53
+ RSpec/ExampleLength:
54
+ Max: 10
55
+
56
+ RSpec/DescribedClass:
57
+ Enabled: false
@@ -0,0 +1,11 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.3
7
+ - ruby-head
8
+ matrix:
9
+ allow_failures:
10
+ - rvm: ruby-head
11
+ before_install: gem install bundler -v 2.0.2
@@ -0,0 +1,29 @@
1
+ ---
2
+ path: ['lib/**/*.rb']
3
+ threshold: 100
4
+ rules:
5
+ ApiTag::Presence:
6
+ enabled: false
7
+ ApiTag::Inclusion:
8
+ enabled: false
9
+ ApiTag::ProtectedMethod:
10
+ enabled: false
11
+ ApiTag::PrivateMethod:
12
+ enabled: false
13
+ ExampleTag:
14
+ enabled: false
15
+ ReturnTag:
16
+ enabled: true
17
+ exclude: []
18
+ Summary::Presence:
19
+ enabled: true
20
+ exclude: []
21
+ Summary::Length:
22
+ enabled: false
23
+ exclude: []
24
+ Summary::Delimiter:
25
+ enabled: true
26
+ exclude: []
27
+ Summary::SingleLine:
28
+ enabled: false
29
+ exclude: []
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at joel.azemar@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in enum_for.gemspec
4
+ gemspec
@@ -0,0 +1,103 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ enum_for (0.1.0)
5
+ activesupport
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activemodel (5.2.3)
11
+ activesupport (= 5.2.3)
12
+ activerecord (5.2.3)
13
+ activemodel (= 5.2.3)
14
+ activesupport (= 5.2.3)
15
+ arel (>= 9.0)
16
+ activesupport (5.2.3)
17
+ concurrent-ruby (~> 1.0, >= 1.0.2)
18
+ i18n (>= 0.7, < 2)
19
+ minitest (~> 5.1)
20
+ tzinfo (~> 1.1)
21
+ arel (9.0.0)
22
+ ast (2.4.0)
23
+ byebug (11.0.1)
24
+ coderay (1.1.2)
25
+ concurrent-ruby (1.1.5)
26
+ diff-lcs (1.3)
27
+ docile (1.3.2)
28
+ i18n (1.6.0)
29
+ concurrent-ruby (~> 1.0)
30
+ jaro_winkler (1.5.3)
31
+ json (2.2.0)
32
+ method_source (0.9.2)
33
+ minitest (5.11.3)
34
+ parallel (1.17.0)
35
+ parser (2.6.3.0)
36
+ ast (~> 2.4.0)
37
+ pry (0.12.2)
38
+ coderay (~> 1.1.0)
39
+ method_source (~> 0.9.0)
40
+ pry-byebug (3.7.0)
41
+ byebug (~> 11.0)
42
+ pry (~> 0.10)
43
+ rainbow (3.0.0)
44
+ rake (10.5.0)
45
+ rspec (3.8.0)
46
+ rspec-core (~> 3.8.0)
47
+ rspec-expectations (~> 3.8.0)
48
+ rspec-mocks (~> 3.8.0)
49
+ rspec-core (3.8.2)
50
+ rspec-support (~> 3.8.0)
51
+ rspec-expectations (3.8.4)
52
+ diff-lcs (>= 1.2.0, < 2.0)
53
+ rspec-support (~> 3.8.0)
54
+ rspec-mocks (3.8.1)
55
+ diff-lcs (>= 1.2.0, < 2.0)
56
+ rspec-support (~> 3.8.0)
57
+ rspec-support (3.8.2)
58
+ rubocop (0.73.0)
59
+ jaro_winkler (~> 1.5.1)
60
+ parallel (~> 1.10)
61
+ parser (>= 2.6)
62
+ rainbow (>= 2.2.2, < 4.0)
63
+ ruby-progressbar (~> 1.7)
64
+ unicode-display_width (>= 1.4.0, < 1.7)
65
+ rubocop-performance (1.4.0)
66
+ rubocop (>= 0.71.0)
67
+ rubocop-rspec (1.33.0)
68
+ rubocop (>= 0.60.0)
69
+ ruby-progressbar (1.10.1)
70
+ simplecov (0.17.0)
71
+ docile (~> 1.1)
72
+ json (>= 1.8, < 3)
73
+ simplecov-html (~> 0.10.0)
74
+ simplecov-html (0.10.2)
75
+ sqlite3 (1.4.1)
76
+ thread_safe (0.3.6)
77
+ tzinfo (1.2.5)
78
+ thread_safe (~> 0.1)
79
+ unicode-display_width (1.6.0)
80
+ with_model (2.1.2)
81
+ activerecord (>= 4.2, < 6.0)
82
+ yard (0.9.20)
83
+ yardstick (0.9.9)
84
+ yard (~> 0.8, >= 0.8.7.2)
85
+
86
+ PLATFORMS
87
+ ruby
88
+
89
+ DEPENDENCIES
90
+ bundler (~> 2.0)
91
+ enum_for!
92
+ pry-byebug
93
+ rake (~> 10.0)
94
+ rspec (~> 3.0)
95
+ rubocop-performance
96
+ rubocop-rspec
97
+ simplecov
98
+ sqlite3
99
+ with_model
100
+ yardstick
101
+
102
+ BUNDLED WITH
103
+ 2.0.2
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Joel AZEMAR
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.
@@ -0,0 +1,150 @@
1
+ # EnumFor
2
+
3
+ Alternative to ActiveRecord::Enum for API use.
4
+
5
+ ### Build
6
+
7
+ [![Travis CI](https://img.shields.io/travis/joel/enum_for.svg?branch=master)](https://travis-ci.org/joel/enum_for)
8
+
9
+ ### Maintainability
10
+
11
+ ![Maintenance](https://img.shields.io/maintenance/yes/2019.svg)
12
+ [![Maintainability](https://api.codeclimate.com/v1/badges/51aa08d8908ab501d537/maintainability)](https://codeclimate.com/github/joel/enum_for/maintainability)
13
+
14
+ ### Code Quality
15
+
16
+ [![Code Climate coverage](https://img.shields.io/codeclimate/coverage/joel/enum_for.svg)](https://codeclimate.com/github/joel/enum_for)
17
+ [![Coverage Status](https://coveralls.io/repos/github/joel/enum_for/badge.svg?branch=master)](https://coveralls.io/github/joel/enum_for?branch=master)
18
+ [![Code Climate issues](https://img.shields.io/codeclimate/issues/joel/enum_for.svg)](https://codeclimate.com/github/joel/enum_for/issues)
19
+ [![Code Climate maintainability](https://img.shields.io/codeclimate/maintainability/joel/enum_for.svg)](https://codeclimate.com/github/joel/enum_for/progress/maintainability)
20
+ [![Code Climate maintainability (percentage)](https://img.shields.io/codeclimate/maintainability-percentage/joel/enum_for.svg)](https://codeclimate.com/github/joel/enum_for/code)
21
+ [![Code Climate technical debt](https://img.shields.io/codeclimate/tech-debt/joel/enum_for.svg)](https://codeclimate.com/github/joel/enum_for/trends/technical_debt)
22
+
23
+ ### Size
24
+
25
+ ![GitHub code size in bytes](https://img.shields.io/github/languages/code-size/joel/enum_for.svg)
26
+ ![GitHub repo size in bytes](https://img.shields.io/github/repo-size/joel/enum_for.svg)
27
+
28
+ ### Usage
29
+
30
+ ![Gem](https://img.shields.io/gem/dv/enum_for/0.1.0.svg)
31
+ ![Gem](https://img.shields.io/gem/v/enum_for.svg)
32
+
33
+ ### Activity
34
+
35
+ ![GitHub All Releases](https://img.shields.io/github/downloads/joel/enum_for/total.svg)
36
+ ![GitHub last commit (master)](https://img.shields.io/github/last-commit/joel/enum_for/master.svg)
37
+ ![GitHub Release Date](https://img.shields.io/github/release-date/joel/enum_for.svg)
38
+
39
+ ### Documentation
40
+
41
+ [![Inline docs](http://inch-ci.org/github/joel/enum_for.svg?branch=master)](http://inch-ci.org/github/joel/enum_for)
42
+ [![License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](http://opensource.org/licenses/MIT)
43
+ [![Version](https://img.shields.io/gem/v/vcr.svg?style=flat-square)](https://rubygems.org/gems/enum_for)
44
+
45
+ ### Security
46
+
47
+ [![Libraries.io dependency status for latest release](https://img.shields.io/librariesio/release/joel/enum_for.svg)](https://libraries.io/github/joel/enum_for)
48
+
49
+ There are several concerns when using AR Enum with API, when you receive a payload it is hard to handle a proper error message.
50
+
51
+ ```ruby
52
+ class Conversation < ActiveRecord::Base
53
+ enum status: [ :draft, :active, :archived ]
54
+ end
55
+ ```
56
+
57
+ ```curl
58
+ curl -X POST \
59
+ {{domain}}/api/v1/conversations \
60
+ -H 'Accept: application/json' \
61
+ -H 'Content-Type: application/json;charset=UTF-8' \
62
+ -d '{
63
+ "conversation": {
64
+ "title": "Relativity theory",
65
+ "status": "unactive"
66
+ }'
67
+ ```
68
+
69
+ ```ruby
70
+ conversation = Conversation.new
71
+ conversation.status = "unactive"
72
+ => ArgumentError ('unactive' is not a valid status)
73
+ ```
74
+
75
+ instead with `EnumFor`
76
+
77
+ ```ruby
78
+ class Conversation < ActiveRecord::Base
79
+ extend EnumFor
80
+ enum_for status: { draft: 0, active: 1, archived: 2 }
81
+ validates :status, inclusion: statuses.keys
82
+ end
83
+ ```
84
+
85
+ ```curl
86
+ curl -X POST \
87
+ {{domain}}/api/v1/conversations \
88
+ -H 'Accept: application/json' \
89
+ -H 'Content-Type: application/json;charset=UTF-8' \
90
+ -d '{
91
+ "conversation": {
92
+ "title": "Relativity theory",
93
+ "status": "unactive"
94
+ }'
95
+ ```
96
+
97
+ ```ruby
98
+ conversation = Conversation.new
99
+ conversation.status = "unactive"
100
+ conversation.valid?
101
+ => false
102
+ conversation.errors.messages
103
+ => { status: [ 'is not included in the list' ] }
104
+ ```
105
+
106
+ ## Installation
107
+
108
+ Add this line to your application's Gemfile:
109
+
110
+ ```ruby
111
+ gem 'enum_for'
112
+ ```
113
+
114
+ And then execute:
115
+
116
+ $ bundle
117
+
118
+ Or install it yourself as:
119
+
120
+ $ gem install enum_for
121
+
122
+ ## Usage
123
+
124
+ You need to extend the AR class you want to use it:
125
+
126
+ ```ruby
127
+ class Conversation < ActiveRecord::Base
128
+ extend EnumFor
129
+ enum_for status: { draft: 0, active: 1, archived: 2 }
130
+ validates :status, inclusion: statuses.keys
131
+ end
132
+ ```
133
+
134
+ ## Development
135
+
136
+ 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.
137
+
138
+ 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 tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
139
+
140
+ ## Contributing
141
+
142
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/enum_for. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
143
+
144
+ ## License
145
+
146
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
147
+
148
+ ## Code of Conduct
149
+
150
+ Everyone interacting in the EnumFor project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/enum_for/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,30 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+ require 'rubocop/rake_task'
4
+ require 'yaml'
5
+ require 'yardstick'
6
+
7
+ desc('Documentation stats and measurements')
8
+ task('qa:docs') do
9
+ yaml = YAML.load_file(File.expand_path('../.yardstick.yml', __FILE__))
10
+ config = Yardstick::Config.coerce(yaml)
11
+ measure = Yardstick.measure(config)
12
+ measure.puts
13
+ coverage = Yardstick.round_percentage(measure.coverage * 100)
14
+ exit(1) if coverage < config.threshold
15
+ end
16
+
17
+ desc('Codestyle check and linter')
18
+ RuboCop::RakeTask.new('qa:code') do |task|
19
+ task.fail_on_error = true
20
+ task.patterns = [
21
+ 'lib/**/*.rb',
22
+ 'spec/**/*.rb'
23
+ ]
24
+ end
25
+
26
+ desc('Run CI QA tasks')
27
+ task(qa: ['qa:docs', 'qa:code'])
28
+
29
+ RSpec::Core::RakeTask.new(spec: :qa)
30
+ task(default: :spec)
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "enum_for"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -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,39 @@
1
+ lib = File.expand_path('lib', __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'enum_for/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'enum_for'
7
+ spec.version = EnumFor::VERSION
8
+ spec.authors = ['Joel AZEMAR']
9
+ spec.email = ['joel.azemar@gmail.com']
10
+
11
+ spec.summary = %q{Alternative to ActiveRecord::Enum}
12
+ spec.description = %q{ActiveRecord::Enum fire SQL request and bypass ActiveModel::Validations, this let you play with enum and validation}
13
+ spec.homepage = 'https://github.com/joel/enum_for'
14
+ spec.license = 'MIT'
15
+
16
+ # Specify which files should be added to the gem when it is released.
17
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
18
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
19
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
20
+ end
21
+ spec.bindir = 'exe'
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ['lib']
24
+
25
+ spec.add_dependency 'activesupport', '~> 5.2'
26
+
27
+ spec.add_development_dependency 'bundler', '~> 2.0'
28
+ spec.add_development_dependency 'rake', '~> 10.0'
29
+ spec.add_development_dependency 'rspec', '~> 3.0'
30
+
31
+ spec.add_development_dependency 'rubocop-performance', '~> 1.4'
32
+ spec.add_development_dependency 'rubocop-rspec', '~> 1.33'
33
+ spec.add_development_dependency 'simplecov', '~> 0.17'
34
+ spec.add_development_dependency 'yardstick', '~> 0.9'
35
+ spec.add_development_dependency 'pry-byebug', '~> 3.7'
36
+
37
+ spec.add_development_dependency 'sqlite3', '~> 1.4'
38
+ spec.add_development_dependency 'with_model', '~> 2.1'
39
+ end
@@ -0,0 +1,88 @@
1
+ require 'enum_for/version'
2
+
3
+ require 'active_support/hash_with_indifferent_access'
4
+ require 'active_support/core_ext/string'
5
+ require 'active_support/core_ext/hash'
6
+ require 'active_support/inflector/inflections'
7
+
8
+ # Public: Module that extends class in order to define enum methods using
9
+ # `enum_for` class method.
10
+ module EnumFor
11
+ class Error < StandardError; end
12
+
13
+ # Public: Defines few methods useful for handling integer attributes which
14
+ # have to be used by enums. There is ActiveRecord enum type, but it shouldn't
15
+ # be exposed in API as it doesn't handle validations well (it raises exception
16
+ # when wrong enum is sent, while we would like to validate enum)
17
+ #
18
+ # This method doesn't prevent enum names to be reserved ruby words so please
19
+ # use with caution.
20
+ #
21
+ # Methods generated by `enum_for` defined as:
22
+ #
23
+ # enum_for color: { red: 0, green: 1 }
24
+ #
25
+ # When enum is set to 1:
26
+ #
27
+ # object.color
28
+ # => 'green'
29
+ #
30
+ # object.red?
31
+ # => false
32
+ #
33
+ # object.green?
34
+ # => true
35
+ #
36
+ # object.class.colors
37
+ # => { red: 0, green: 1 }
38
+ #
39
+ # object.color_to_enum
40
+ # => 1
41
+ #
42
+ # It also adds validation:
43
+ #
44
+ # validates :color, presence: true, inclusion: colors.keys
45
+ #
46
+ # @return [NilClass]
47
+ def enum_for(hash)
48
+ enum_name = hash.keys.first.to_s
49
+ mapping = hash.values.first.with_indifferent_access
50
+
51
+ define_singleton_method(enum_name.pluralize) do
52
+ mapping
53
+ end
54
+
55
+ define_method(enum_name) do
56
+ return nil if self[enum_name].blank?
57
+
58
+ mapping.key(self[enum_name]).to_s
59
+ end
60
+
61
+ define_method("#{enum_name}=") do |value|
62
+ begin
63
+ self[enum_name] = Integer(value)
64
+ rescue ArgumentError, TypeError
65
+ self[enum_name] = mapping[value.to_s.downcase]
66
+ end
67
+ end
68
+
69
+ mapping.each do |name, _|
70
+ define_method "#{name}?" do
71
+ public_send(enum_name).to_s == name
72
+ end
73
+ end
74
+
75
+ mapping.each do |name, value|
76
+ define_method "#{name}!" do
77
+ public_send("#{enum_name}=", value)
78
+ save!
79
+ end
80
+ end
81
+
82
+ define_method("#{enum_name}_to_enum") do
83
+ self[enum_name]
84
+ end
85
+
86
+ nil
87
+ end
88
+ end
@@ -0,0 +1,3 @@
1
+ module EnumFor
2
+ VERSION = '0.1.0'.freeze
3
+ end
metadata ADDED
@@ -0,0 +1,214 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: enum_for
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Joel AZEMAR
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-07-22 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: '5.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '5.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop-performance
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.4'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.4'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop-rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.33'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.33'
97
+ - !ruby/object:Gem::Dependency
98
+ name: simplecov
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '0.17'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '0.17'
111
+ - !ruby/object:Gem::Dependency
112
+ name: yardstick
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '0.9'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '0.9'
125
+ - !ruby/object:Gem::Dependency
126
+ name: pry-byebug
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '3.7'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '3.7'
139
+ - !ruby/object:Gem::Dependency
140
+ name: sqlite3
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: '1.4'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: '1.4'
153
+ - !ruby/object:Gem::Dependency
154
+ name: with_model
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - "~>"
158
+ - !ruby/object:Gem::Version
159
+ version: '2.1'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - "~>"
165
+ - !ruby/object:Gem::Version
166
+ version: '2.1'
167
+ description: ActiveRecord::Enum fire SQL request and bypass ActiveModel::Validations,
168
+ this let you play with enum and validation
169
+ email:
170
+ - joel.azemar@gmail.com
171
+ executables: []
172
+ extensions: []
173
+ extra_rdoc_files: []
174
+ files:
175
+ - ".gitignore"
176
+ - ".rspec"
177
+ - ".rubocop.yml"
178
+ - ".travis.yml"
179
+ - ".yardstick.yml"
180
+ - CODE_OF_CONDUCT.md
181
+ - Gemfile
182
+ - Gemfile.lock
183
+ - LICENSE.txt
184
+ - README.md
185
+ - Rakefile
186
+ - bin/console
187
+ - bin/setup
188
+ - enum_for.gemspec
189
+ - lib/enum_for.rb
190
+ - lib/enum_for/version.rb
191
+ homepage: https://github.com/joel/enum_for
192
+ licenses:
193
+ - MIT
194
+ metadata: {}
195
+ post_install_message:
196
+ rdoc_options: []
197
+ require_paths:
198
+ - lib
199
+ required_ruby_version: !ruby/object:Gem::Requirement
200
+ requirements:
201
+ - - ">="
202
+ - !ruby/object:Gem::Version
203
+ version: '0'
204
+ required_rubygems_version: !ruby/object:Gem::Requirement
205
+ requirements:
206
+ - - ">="
207
+ - !ruby/object:Gem::Version
208
+ version: '0'
209
+ requirements: []
210
+ rubygems_version: 3.0.4
211
+ signing_key:
212
+ specification_version: 4
213
+ summary: Alternative to ActiveRecord::Enum
214
+ test_files: []