enumify 0.0.3 → 0.0.4
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 +7 -0
- data/LICENSE +20 -0
- data/Readme.md +34 -22
- data/enumify.gemspec +11 -1
- data/lib/enumify/model.rb +2 -2
- data/lib/enumify/version.rb +1 -1
- metadata +84 -94
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 6b5e524f240a067b06540a32326291865c420b85
|
4
|
+
data.tar.gz: ee00750660d876fce2e8f5e916cd9549c7e3153d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2ffc622bd738b6049023cc6b4f0b91ab63b2cfe193b48aed43aa379fd8dd1fa50aacca01969e011f66dd1c572c616995e85285ff30f50d511af59607cb727e0d
|
7
|
+
data.tar.gz: 8af5a86231191e753b9436289801dc26c175757d21f28ac79757fb2e0535c6dc51f471f5f82581864eaf3fa8a1e3508b915a56b420c4ca0211383691d0232ecf
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2013 Yonatan Bergman
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
6
|
+
this software and associated documentation files (the "Software"), to deal in
|
7
|
+
the Software without restriction, including without limitation the rights to
|
8
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
9
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
10
|
+
subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
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, FITNESS
|
17
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
18
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
19
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
20
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Readme.md
CHANGED
@@ -1,36 +1,43 @@
|
|
1
1
|
# Enumify [](http://travis-ci.org/yonbergman/enumify)
|
2
2
|
|
3
|
-
|
4
|
-
enumify adds an enum command to all ActiveRecord models which enables you to work with string attributes as if they were enums
|
3
|
+
Enumify adds an enum command to all ActiveRecord models which enables you to work with string attributes as if they were enums
|
5
4
|
|
6
5
|
## Installing
|
7
6
|
|
8
7
|
Just add the enumify gem to your GemFile
|
9
8
|
|
9
|
+
```ruby
|
10
|
+
gem 'enumify'
|
11
|
+
```
|
12
|
+
|
10
13
|
## How to use
|
11
14
|
|
12
15
|
Just call the enum function in any ActiveRecord object, the function accepts the field name as the first variable and the possible values as an array
|
13
16
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
+
```ruby
|
18
|
+
class Event < ActiveRecord::Base
|
19
|
+
enum :status, [:available, :canceled, :completed]
|
20
|
+
end
|
21
|
+
```
|
17
22
|
|
18
23
|
After that you get several autogenerated commands to use with the enum
|
19
24
|
|
20
|
-
|
25
|
+
```ruby
|
26
|
+
# Access through field name
|
21
27
|
|
22
|
-
|
23
|
-
|
28
|
+
event.status # returns the enum's current value as a symbol
|
29
|
+
event.status = :canceled # sets the enum's value to canceled (can also get a string)
|
24
30
|
|
25
31
|
|
26
|
-
|
32
|
+
# Shorthand methods, access through the possible values
|
27
33
|
|
28
|
-
|
29
|
-
|
34
|
+
event.available? # returns true if enum's current status is available
|
35
|
+
event.canceled! # changes the enum's value to canceled
|
30
36
|
|
31
|
-
|
32
|
-
|
33
|
-
|
37
|
+
# Get all the possible values
|
38
|
+
|
39
|
+
Event::STATUSES # returns all available status of the enum
|
40
|
+
```
|
34
41
|
|
35
42
|
## Callbacks
|
36
43
|
Another cool feature of enumify is the option to add a callback function that will be called each time the value of the field changes
|
@@ -38,26 +45,31 @@ This is cool to do stuff like log stuff or create behaviour on state changes
|
|
38
45
|
|
39
46
|
All you need to do is add a x_changed method in your class and the enumify will call it
|
40
47
|
|
41
|
-
|
42
|
-
|
48
|
+
```ruby
|
49
|
+
class Event < ActiveRecord::Base
|
50
|
+
enum :status, [:available, :canceled, :completed]
|
43
51
|
|
44
|
-
|
45
|
-
|
46
|
-
end
|
52
|
+
def status_changed(old, new)
|
53
|
+
puts "status changed from #{old} to #{new}"
|
47
54
|
end
|
48
|
-
|
55
|
+
end
|
56
|
+
```
|
49
57
|
|
50
58
|
## Scopes
|
51
59
|
One last thing that the enumify gem does is created scope (formerly nested_scopes) so you can easly query by the enum
|
52
60
|
|
53
61
|
For example if you want to count all the events that are canceled you can just run
|
54
62
|
|
55
|
-
|
63
|
+
```ruby
|
64
|
+
Event.canceled.count
|
65
|
+
```
|
56
66
|
|
57
67
|
In addition you can also use a negation scope to retrieve all the records that are not set to the given value.
|
58
68
|
For example to count all the events that are not canceled you can run
|
59
69
|
|
60
|
-
|
70
|
+
```ruby
|
71
|
+
Event.not_canceled.count
|
72
|
+
```
|
61
73
|
|
62
74
|
---
|
63
75
|
|
data/enumify.gemspec
CHANGED
@@ -9,7 +9,17 @@ Gem::Specification.new do |s|
|
|
9
9
|
s.email = ["yonatanbergman@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
|
-
s.description =
|
12
|
+
s.description = <<-END
|
13
|
+
Enumify lets you add an enum command to ActiveRecord models
|
14
|
+
|
15
|
+
There are four things that the enumify gems adds to your model
|
16
|
+
Validation - The enumify adds a validation to make sure that the field only receives accepted values
|
17
|
+
Super Cool Methods - adds ? and ! functions for each enum value (canceled? - is it canceled, canceled! - change the state to canceled)
|
18
|
+
Callback support - you can add a x_callback method which will be called each time the status changes
|
19
|
+
Scopes - you can easily query for values of the enum
|
20
|
+
END
|
21
|
+
s.license = 'MIT'
|
22
|
+
|
13
23
|
|
14
24
|
s.rubyforge_project = "enumify"
|
15
25
|
|
data/lib/enumify/model.rb
CHANGED
@@ -42,7 +42,7 @@ module Enumify
|
|
42
42
|
send("_set_#{parameter.to_s}", opt, true)
|
43
43
|
end
|
44
44
|
|
45
|
-
scope opt.to_sym, where(parameter.to_sym => opt.to_s)
|
45
|
+
scope opt.to_sym, lambda { where(parameter.to_sym => opt.to_s) }
|
46
46
|
end
|
47
47
|
|
48
48
|
# We want to first define all the "positive" scopes and only then define
|
@@ -52,7 +52,7 @@ module Enumify
|
|
52
52
|
# be used in a joined query with other models that have the same enum field then
|
53
53
|
# it will fail on ambiguous column name.
|
54
54
|
unless respond_to?("not_#{opt}")
|
55
|
-
scope "not_#{opt}", where("#{self.table_name}.#{parameter} != ?", opt.to_s)
|
55
|
+
scope "not_#{opt}", lambda { where("#{self.table_name}.#{parameter} != ?", opt.to_s) }
|
56
56
|
end
|
57
57
|
end
|
58
58
|
|
data/lib/enumify/version.rb
CHANGED
metadata
CHANGED
@@ -1,92 +1,90 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: enumify
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
prerelease:
|
6
|
-
segments:
|
7
|
-
- 0
|
8
|
-
- 0
|
9
|
-
- 3
|
10
|
-
version: 0.0.3
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.4
|
11
5
|
platform: ruby
|
12
|
-
authors:
|
6
|
+
authors:
|
13
7
|
- yon
|
14
8
|
autorequire:
|
15
9
|
bindir: bin
|
16
10
|
cert_chain: []
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
- !ruby/object:Gem::Dependency
|
11
|
+
date: 2013-08-06 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
21
14
|
name: rake
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
- !ruby/object:Gem::Version
|
28
|
-
hash: 3
|
29
|
-
segments:
|
30
|
-
- 0
|
31
|
-
version: "0"
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
32
20
|
type: :development
|
33
|
-
version_requirements: *id001
|
34
|
-
- !ruby/object:Gem::Dependency
|
35
|
-
name: rspec
|
36
21
|
prerelease: false
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
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: rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
46
34
|
type: :development
|
47
|
-
version_requirements: *id002
|
48
|
-
- !ruby/object:Gem::Dependency
|
49
|
-
name: activerecord
|
50
35
|
prerelease: false
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
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: activerecord
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
60
48
|
type: :development
|
61
|
-
version_requirements: *id003
|
62
|
-
- !ruby/object:Gem::Dependency
|
63
|
-
name: sqlite3
|
64
49
|
prerelease: false
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
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: sqlite3
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
74
62
|
type: :development
|
75
|
-
|
76
|
-
|
77
|
-
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: |2
|
70
|
+
Enumify lets you add an enum command to ActiveRecord models
|
71
|
+
|
72
|
+
There are four things that the enumify gems adds to your model
|
73
|
+
Validation - The enumify adds a validation to make sure that the field only receives accepted values
|
74
|
+
Super Cool Methods - adds ? and ! functions for each enum value (canceled? - is it canceled, canceled! - change the state to canceled)
|
75
|
+
Callback support - you can add a x_callback method which will be called each time the status changes
|
76
|
+
Scopes - you can easily query for values of the enum
|
77
|
+
email:
|
78
78
|
- yonatanbergman@gmail.com
|
79
79
|
executables: []
|
80
|
-
|
81
80
|
extensions: []
|
82
|
-
|
83
81
|
extra_rdoc_files: []
|
84
|
-
|
85
|
-
files:
|
82
|
+
files:
|
86
83
|
- .gitignore
|
87
84
|
- .rspec
|
88
85
|
- CHANGELOG.md
|
89
86
|
- Gemfile
|
87
|
+
- LICENSE
|
90
88
|
- Rakefile
|
91
89
|
- Readme.md
|
92
90
|
- enumify.gemspec
|
@@ -97,38 +95,30 @@ files:
|
|
97
95
|
- spec/enumify/enum_spec.rb
|
98
96
|
- spec/spec_helper.rb
|
99
97
|
homepage: http://github.com/yonbergman/enumify
|
100
|
-
licenses:
|
101
|
-
|
98
|
+
licenses:
|
99
|
+
- MIT
|
100
|
+
metadata: {}
|
102
101
|
post_install_message:
|
103
102
|
rdoc_options: []
|
104
|
-
|
105
|
-
require_paths:
|
103
|
+
require_paths:
|
106
104
|
- lib
|
107
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
none: false
|
118
|
-
requirements:
|
119
|
-
- - ">="
|
120
|
-
- !ruby/object:Gem::Version
|
121
|
-
hash: 3
|
122
|
-
segments:
|
123
|
-
- 0
|
124
|
-
version: "0"
|
105
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - '>='
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
111
|
+
requirements:
|
112
|
+
- - '>='
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
version: '0'
|
125
115
|
requirements: []
|
126
|
-
|
127
116
|
rubyforge_project: enumify
|
128
|
-
rubygems_version:
|
117
|
+
rubygems_version: 2.0.6
|
129
118
|
signing_key:
|
130
|
-
specification_version:
|
131
|
-
summary: enumify adds an enum command to all ActiveRecord models which enables you
|
132
|
-
|
119
|
+
specification_version: 4
|
120
|
+
summary: enumify adds an enum command to all ActiveRecord models which enables you
|
121
|
+
to work with string attributes as if they were enums
|
122
|
+
test_files:
|
133
123
|
- spec/enumify/enum_spec.rb
|
134
124
|
- spec/spec_helper.rb
|