jt-rails-enum 1.0.0 → 1.0.1

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: c87dea8a69c0c7c01c8b4886a1023854f04eedaf
4
- data.tar.gz: a84469d4627dcb5e7abd3bbfb1c438710990565a
3
+ metadata.gz: ca5be5149ec48c4e3f647633c7fc82afb7b1f1b1
4
+ data.tar.gz: ab1bbe3192442aa030c7f11b222659dd190599cd
5
5
  SHA512:
6
- metadata.gz: a94a3ab8b26e15f28e078a85a0ec7ba0787d70e6bb84d11cbd9204cb99f48b0a25045cafd14b712b367665bed78d5797cbf36ae2483eeb048b2e2ac8982233aa
7
- data.tar.gz: c3e8e09e89bd73b3ee5b900c38fa024b3a58fea84bb91805bee7d027e3ef129704c16026e7206a8d85c0c0021a186c5dbcd33d56bae35fea4538cde879c7d530
6
+ metadata.gz: 463c6a1d8c18fc548288dffda6a4ad3860112a463719241e2ca84af6efa2667240523ead8fdd7d08cdd47a18a1e4425fba343ca9721f27fd2a7263382455b334
7
+ data.tar.gz: 147fcb38b49c3c13d3cd273667525ca95356ae78baec8ff7989d116c18e34d43c3e05f6547ac920368a8e14d995bfedb49024630bc65b96acd926d3100242cf2
@@ -0,0 +1,2 @@
1
+ *.gem
2
+ .DS_Store
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2016 Jonathan Tribouharet
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
@@ -0,0 +1,50 @@
1
+ # JTRailsEnum
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/jt-rails-enum.svg)](http://badge.fury.io/rb/jt-rails-enum)
4
+
5
+ JTRailsEnum let you use enum in your models. JTRailsEnum doesn't works exactly like `enum` in Ruby On Rails. It always add a prefix, which is a better behavior when you use multiple enums in a model. The `prefix` option is also not present in the Ruby On Rails 4.0.
6
+
7
+ ## Installation
8
+
9
+ JTRailsEnum is distributed as a gem, which is how it should be used in your app.
10
+
11
+ Include the gem in your Gemfile:
12
+
13
+ gem 'jt-rails-enum', '~> 1.0'
14
+
15
+ ## Usage
16
+
17
+ ### Basic usage
18
+
19
+ ```ruby
20
+ class User < ActiveRecord::Base
21
+
22
+ jt_enum confirmation_status: [
23
+ :waiting,
24
+ :accepted,
25
+ :refused
26
+ ]
27
+
28
+ end
29
+ ```
30
+
31
+ Scopes and some basic methods are automatically created for each value in the enum.
32
+ ```ruby
33
+ # User.where(confirmation_status: User.confirmation_statuses[:waiting]).first
34
+ user = User.confirmation_status_waiting.first
35
+
36
+ # Equivalent to user.update!(confirmation_status: User.confirmation_statuses[:accepted])
37
+ user.confirmation_status_accepted!
38
+
39
+ # Equivalent to user.confirmation_status == User.confirmation_statuses[:accepted]
40
+ user.confirmation_status_accepted?
41
+
42
+ ```
43
+
44
+ ## Author
45
+
46
+ - [Jonathan Tribouharet](https://github.com/jonathantribouharet) ([@johntribouharet](https://twitter.com/johntribouharet))
47
+
48
+ ## License
49
+
50
+ JTRailsEnum is released under the MIT license. See the LICENSE file for more info.
@@ -0,0 +1,13 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'jt-rails-enum'
3
+ s.summary = "JTRailsEnum let you use enum in your models"
4
+ s.description = "JTRailsEnum let you use enum in your models"
5
+ s.homepage = 'https://github.com/jonathantribouharet/jt-rails-enum'
6
+ s.version = '1.0.1'
7
+ s.files = `git ls-files`.split("\n")
8
+ s.require_paths = ['lib']
9
+ s.authors = ['Jonathan TRIBOUHARET']
10
+ s.email = 'jonathan.tribouharet@gmail.com'
11
+ s.license = 'MIT'
12
+ s.platform = Gem::Platform::RUBY
13
+ end
@@ -0,0 +1,37 @@
1
+ module JT
2
+ module Rails
3
+ module Enum
4
+ extend ActiveSupport::Concern
5
+
6
+ class_methods do
7
+
8
+ def jt_enum(definitions)
9
+ klass = self
10
+
11
+ definitions.each do |field, values|
12
+
13
+ enum_values = ActiveSupport::HashWithIndifferentAccess.new
14
+
15
+ klass.singleton_class.send(:define_method, field.to_s.pluralize) { enum_values }
16
+
17
+ values.each_with_index do |value, i|
18
+ value_method_name = "#{field}_#{value}"
19
+
20
+ enum_values[value] = i
21
+
22
+ define_method("#{value_method_name}?") { self[field] == i }
23
+ define_method("#{value_method_name}!") { update! field => i }
24
+
25
+ klass.scope value_method_name, -> { klass.where field => i }
26
+ end
27
+
28
+ validates field, allow_nil: true, inclusion: { in: klass.send(field.to_s.pluralize).values }
29
+
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+
37
+ ActiveRecord::Base.send :include, JT::Rails::Enum
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jt-rails-enum
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jonathan TRIBOUHARET
@@ -15,7 +15,13 @@ email: jonathan.tribouharet@gmail.com
15
15
  executables: []
16
16
  extensions: []
17
17
  extra_rdoc_files: []
18
- files: []
18
+ files:
19
+ - .gitignore
20
+ - Gemfile
21
+ - LICENSE
22
+ - README.md
23
+ - jt-rails-enum.gemspec
24
+ - lib/jt-rails-enum.rb
19
25
  homepage: https://github.com/jonathantribouharet/jt-rails-enum
20
26
  licenses:
21
27
  - MIT