enumify 0.2.0 → 1.0.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 +4 -4
- data/.travis.yml +0 -2
- data/CHANGELOG.md +4 -0
- data/Gemfile +1 -0
- data/Readme.md +11 -22
- data/enumify.gemspec +2 -2
- data/lib/enumify/model.rb +0 -1
- data/lib/enumify/version.rb +1 -1
- data/spec/enumify/enum_spec.rb +9 -9
- data/spec/spec_helper.rb +0 -1
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b87f16577e0ad0f7e3764f92f0d069d07e7ac5a3
|
4
|
+
data.tar.gz: 2844722bb6c6023ca4ce0a1189b1603424da26de
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 265d1739e223e900024b5bedadfd223a8915ff01a4e8e8e746452fd864ab56524e65d763cf31e86e09b3e5956fb0a906d4ad36802f4165a17ad68bc3febbf30b
|
7
|
+
data.tar.gz: fff56ae04ccf2358be474acee0b7dd5ab5a871b9c407f021261d6cc9b4719f5ea43000546fa38b50b58871dadb7845afe3e5eab8aeecb36159a1efd94ec19c22
|
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
data/Gemfile
CHANGED
data/Readme.md
CHANGED
@@ -12,25 +12,14 @@ gem 'enumify'
|
|
12
12
|
|
13
13
|
## How to use
|
14
14
|
|
15
|
-
Just call the `
|
16
|
-
|
17
|
-
```ruby
|
18
|
-
class Event < ActiveRecord::Base
|
19
|
-
enum :status, [:available, :canceled, :completed]
|
20
|
-
end
|
21
|
-
```
|
22
|
-
|
23
|
-
## Rails 4.1+
|
24
|
-
|
25
|
-
You can also instantiate a _Enumify_ enum by calling the `enumify` method instead of `enum`.
|
26
|
-
This is especially helpful when your app is running Rail 4.1 or better which has it's own built in enum (although not as good)
|
15
|
+
Just call the `enumify` function in any ActiveRecord object, the function accepts the field name as the first variable and the possible values as an array
|
27
16
|
|
28
17
|
```ruby
|
29
18
|
class Event < ActiveRecord::Base
|
30
19
|
enumify :status, [:available, :canceled, :completed]
|
31
20
|
end
|
32
21
|
```
|
33
|
-
|
22
|
+
|
34
23
|
## Usage
|
35
24
|
|
36
25
|
After that you get several autogenerated commands to use with the enum
|
@@ -58,20 +47,20 @@ By default the enum field does not support a nil value. In order to allow nil va
|
|
58
47
|
|
59
48
|
```ruby
|
60
49
|
class Event < ActiveRecord::Base
|
61
|
-
|
50
|
+
enumify :status, [:available, :canceled, :completed], :allow_nil => true
|
62
51
|
end
|
63
52
|
|
64
53
|
Event.create! # Is valid and does not throw an exception.
|
65
54
|
```
|
66
55
|
|
67
56
|
#### :prefix
|
68
|
-
By default all enum values are available as scopes, bang and query methods based on the value.
|
69
|
-
You can add a prefix for the enum values in order to differentiate different enums on the same object.
|
57
|
+
By default all enum values are available as scopes, bang and query methods based on the value.
|
58
|
+
You can add a prefix for the enum values in order to differentiate different enums on the same object.
|
70
59
|
|
71
60
|
```ruby
|
72
61
|
class Event < ActiveRecord::Base
|
73
|
-
|
74
|
-
|
62
|
+
enumify :status, [:available, :canceled, :completed], :prefix => true
|
63
|
+
enumify :subtype, [:company, :personal], :prefix => 'type'
|
75
64
|
end
|
76
65
|
|
77
66
|
event.available? # Not available anymore
|
@@ -85,9 +74,9 @@ You can remove the constant by passing a falsy value (i.e: `nil`, `false`) or re
|
|
85
74
|
|
86
75
|
```ruby
|
87
76
|
class Event < ActiveRecord::Base
|
88
|
-
|
89
|
-
|
90
|
-
|
77
|
+
enumify :status, [:available, :canceled, :completed]
|
78
|
+
enumify :without_const, [:foo, :bar], :constant => false
|
79
|
+
enumify :custom_name, [:a, :b, :c], :constant => :special_name
|
91
80
|
end
|
92
81
|
|
93
82
|
event::STATUSES # returns [:available, :canceled, :completed]
|
@@ -104,7 +93,7 @@ All you need to do is add a x_changed method in your class and the enumify will
|
|
104
93
|
|
105
94
|
```ruby
|
106
95
|
class Event < ActiveRecord::Base
|
107
|
-
|
96
|
+
enumify :status, [:available, :canceled, :completed]
|
108
97
|
|
109
98
|
def status_changed(old, new)
|
110
99
|
puts "status changed from #{old} to #{new}"
|
data/enumify.gemspec
CHANGED
@@ -5,8 +5,8 @@ require "enumify/version"
|
|
5
5
|
Gem::Specification.new do |s|
|
6
6
|
s.name = "enumify"
|
7
7
|
s.version = Enumify::VERSION
|
8
|
-
s.authors = ["
|
9
|
-
s.email = ["
|
8
|
+
s.authors = ["yonbergman"]
|
9
|
+
s.email = ["yonbergman@gmail.com"]
|
10
10
|
s.homepage = "http://github.com/yonbergman/enumify"
|
11
11
|
s.summary = %q{enumify adds an enum command to all ActiveRecord models which enables you to work with string attributes as if they were enums}
|
12
12
|
s.description = <<-END
|
data/lib/enumify/model.rb
CHANGED
data/lib/enumify/version.rb
CHANGED
data/spec/enumify/enum_spec.rb
CHANGED
@@ -3,7 +3,7 @@ require 'spec_helper'
|
|
3
3
|
class Model < ActiveRecord::Base
|
4
4
|
include Enumify::Model
|
5
5
|
|
6
|
-
|
6
|
+
enumify :status, [:available, :canceled, :completed]
|
7
7
|
end
|
8
8
|
|
9
9
|
class OtherModel < ActiveRecord::Base
|
@@ -11,13 +11,13 @@ class OtherModel < ActiveRecord::Base
|
|
11
11
|
|
12
12
|
belongs_to :model
|
13
13
|
|
14
|
-
|
14
|
+
enumify :status, [:active, :expired, :not_expired]
|
15
15
|
end
|
16
16
|
|
17
17
|
class ModelAllowingNil < ActiveRecord::Base
|
18
18
|
include Enumify::Model
|
19
19
|
self.table_name = 'models'
|
20
|
-
|
20
|
+
enumify :status, [:available, :canceled, :completed], :allow_nil => true
|
21
21
|
end
|
22
22
|
|
23
23
|
|
@@ -201,19 +201,19 @@ describe :Enumify do
|
|
201
201
|
class ModelWithoutConst < ActiveRecord::Base
|
202
202
|
include Enumify::Model
|
203
203
|
self.table_name = 'models'
|
204
|
-
|
204
|
+
enumify :status, [:available, :canceled, :completed], :constant => false
|
205
205
|
end
|
206
206
|
|
207
207
|
class ModelWithSymbolNamedConst < ActiveRecord::Base
|
208
208
|
include Enumify::Model
|
209
209
|
self.table_name = 'models'
|
210
|
-
|
210
|
+
enumify :status, [:available, :canceled, :completed], :constant => :special_status
|
211
211
|
end
|
212
212
|
|
213
213
|
class ModelWithStringNamedConst < ActiveRecord::Base
|
214
214
|
include Enumify::Model
|
215
215
|
self.table_name = 'models'
|
216
|
-
|
216
|
+
enumify :status, [:available, :canceled, :completed], :constant => 'special_status'
|
217
217
|
end
|
218
218
|
|
219
219
|
it 'class should have a CONST that holds all the available options of the enum by default' do
|
@@ -240,7 +240,7 @@ describe :Enumify do
|
|
240
240
|
Class.new(ActiveRecord::Base) {
|
241
241
|
include Enumify::Model
|
242
242
|
self.table_name = 'models'
|
243
|
-
|
243
|
+
enumify :status, [:available, :canceled, :completed], :constant => invalid_name
|
244
244
|
}
|
245
245
|
}.to raise_error(NameError)
|
246
246
|
end
|
@@ -255,7 +255,7 @@ describe :Enumify do
|
|
255
255
|
class ModelWithPrefix < ActiveRecord::Base
|
256
256
|
include Enumify::Model
|
257
257
|
self.table_name = 'models'
|
258
|
-
|
258
|
+
enumify :status, [:available, :canceled, :completed], :prefix => 'foo'
|
259
259
|
end
|
260
260
|
|
261
261
|
subject { ModelWithPrefix.new(:status => :available) }
|
@@ -289,7 +289,7 @@ describe :Enumify do
|
|
289
289
|
class ModelWithPrefixTrue < ActiveRecord::Base
|
290
290
|
include Enumify::Model
|
291
291
|
self.table_name = 'models'
|
292
|
-
|
292
|
+
enumify :status, [:available, :canceled, :completed], :prefix => true
|
293
293
|
end
|
294
294
|
|
295
295
|
subject { ModelWithPrefixTrue.new(:status => :available) }
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: enumify
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
|
-
-
|
7
|
+
- yonbergman
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-12-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -75,7 +75,7 @@ description: |2
|
|
75
75
|
Callback support - you can add a x_callback method which will be called each time the status changes
|
76
76
|
Scopes - you can easily query for values of the enum
|
77
77
|
email:
|
78
|
-
-
|
78
|
+
- yonbergman@gmail.com
|
79
79
|
executables: []
|
80
80
|
extensions: []
|
81
81
|
extra_rdoc_files: []
|