enumify 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +14 -0
- data/Appraisals +15 -0
- data/CHANGELOG.md +3 -0
- data/Rakefile +1 -0
- data/Readme.md +14 -0
- data/enumify.gemspec +2 -1
- data/gemfiles/rails30.gemfile +7 -0
- data/gemfiles/rails31.gemfile +7 -0
- data/gemfiles/rails32.gemfile +7 -0
- data/gemfiles/rails40.gemfile +7 -0
- data/lib/enumify/model.rb +13 -13
- data/lib/enumify/version.rb +1 -1
- data/spec/enumify/enum_spec.rb +33 -0
- metadata +24 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a8afbb3738f0c1aba03e6447384e9fe19c628905
|
4
|
+
data.tar.gz: 7a3e37ec4b0e53194e13005d8c46ffcd5c9ff4b6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cee67d6734399da89000f4a503acd71098842395bd5ef6d6fdd1e7d08bbed1fe7cc8debcff7b20dfcc3cc5a2533845f4b8ab38fce479519b210b31fd14cc9850
|
7
|
+
data.tar.gz: ccb3e32f9d9dd9c7fa8457097d3a23cd5899585a3edbce0a3491a1f268a80f4180b72e2d12b7e507e9895b1c3da44f10709a5fbd65d195a87b142a63ac001583
|
data/.travis.yml
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
language: ruby
|
2
|
+
rvm:
|
3
|
+
- 1.8.7
|
4
|
+
- 1.9.3
|
5
|
+
- 2.0.0
|
6
|
+
gemfile:
|
7
|
+
- gemfiles/rails30.gemfile
|
8
|
+
- gemfiles/rails31.gemfile
|
9
|
+
- gemfiles/rails32.gemfile
|
10
|
+
- gemfiles/rails40.gemfile
|
11
|
+
matrix:
|
12
|
+
exclude:
|
13
|
+
- rvm: 1.8.7
|
14
|
+
gemfile: gemfiles/rails40.gemfile
|
data/Appraisals
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
appraise "rails30" do
|
2
|
+
gem "activerecord", "~> 3.0.11"
|
3
|
+
end
|
4
|
+
|
5
|
+
appraise "rails31" do
|
6
|
+
gem "activerecord", "~> 3.1.12"
|
7
|
+
end
|
8
|
+
|
9
|
+
appraise "rails32" do
|
10
|
+
gem "activerecord", "~> 3.2.14"
|
11
|
+
end
|
12
|
+
|
13
|
+
appraise "rails40" do
|
14
|
+
gem "activerecord", "~> 4.0.0"
|
15
|
+
end
|
data/CHANGELOG.md
CHANGED
data/Rakefile
CHANGED
data/Readme.md
CHANGED
@@ -39,6 +39,20 @@ event.canceled! # changes the enum's value to canceled
|
|
39
39
|
Event::STATUSES # returns all available status of the enum
|
40
40
|
```
|
41
41
|
|
42
|
+
## Options
|
43
|
+
#### :allow_nil
|
44
|
+
By default the enum field does not support a nil value. In order to allow nil values add the `allow_nil` option (similar to the Rails validation option).
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
class Event < ActiveRecord::Base
|
48
|
+
enum :status, [:available, :canceled, :completed], :allow_nil => true
|
49
|
+
end
|
50
|
+
|
51
|
+
Event.create! # Is valid and does not throw an exception.
|
52
|
+
```
|
53
|
+
|
54
|
+
|
55
|
+
|
42
56
|
## Callbacks
|
43
57
|
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
|
44
58
|
This is cool to do stuff like log stuff or create behaviour on state changes
|
data/enumify.gemspec
CHANGED
@@ -31,6 +31,7 @@ Gem::Specification.new do |s|
|
|
31
31
|
# specify any dependencies here; for example:
|
32
32
|
s.add_development_dependency "rake"
|
33
33
|
s.add_development_dependency "rspec"
|
34
|
-
s.add_development_dependency "activerecord"
|
34
|
+
s.add_development_dependency "activerecord", '>= 3.0'
|
35
35
|
s.add_development_dependency "sqlite3"
|
36
|
+
s.add_development_dependency 'appraisal', '>= 0.3.8'
|
36
37
|
end
|
data/lib/enumify/model.rb
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
module Enumify
|
2
2
|
module Model
|
3
|
-
def enum(parameter,
|
3
|
+
def enum(parameter, vals=[], opts={})
|
4
4
|
|
5
|
-
validates_inclusion_of parameter, :in => opts
|
5
|
+
validates_inclusion_of parameter, :in => vals, :allow_nil => !!opts[:allow_nil]
|
6
6
|
|
7
|
-
const_set("#{parameter.to_s.pluralize.upcase}",
|
7
|
+
const_set("#{parameter.to_s.pluralize.upcase}", vals)
|
8
8
|
|
9
9
|
define_method "#{parameter.to_s}" do
|
10
10
|
attr = read_attribute(parameter)
|
@@ -31,28 +31,28 @@ module Enumify
|
|
31
31
|
|
32
32
|
end
|
33
33
|
|
34
|
-
|
35
|
-
raise "Collision in enum values method #{
|
34
|
+
vals.each do |val|
|
35
|
+
raise "Collision in enum values method #{val}" if respond_to?("#{val.to_s}?") or respond_to?("#{val.to_s}!") or respond_to?("#{val.to_s}")
|
36
36
|
|
37
|
-
define_method "#{
|
38
|
-
send("#{parameter.to_s}") ==
|
37
|
+
define_method "#{val.to_s}?" do
|
38
|
+
send("#{parameter.to_s}") == val
|
39
39
|
end
|
40
40
|
|
41
|
-
define_method "#{
|
42
|
-
send("_set_#{parameter.to_s}",
|
41
|
+
define_method "#{val.to_s}!" do
|
42
|
+
send("_set_#{parameter.to_s}", val, true)
|
43
43
|
end
|
44
44
|
|
45
|
-
scope
|
45
|
+
scope val.to_sym, lambda { where(parameter.to_sym => val.to_s) }
|
46
46
|
end
|
47
47
|
|
48
48
|
# We want to first define all the "positive" scopes and only then define
|
49
49
|
# the "negation scopes", to make sure they don't override previous scopes
|
50
|
-
|
50
|
+
vals.each do |val|
|
51
51
|
# We need to prefix the field with the table name since if this scope will
|
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
|
-
unless respond_to?("not_#{
|
55
|
-
scope "not_#{
|
54
|
+
unless respond_to?("not_#{val}")
|
55
|
+
scope "not_#{val}", lambda { where("#{self.table_name}.#{parameter} != ?", val.to_s) }
|
56
56
|
end
|
57
57
|
end
|
58
58
|
|
data/lib/enumify/version.rb
CHANGED
data/spec/enumify/enum_spec.rb
CHANGED
@@ -14,6 +14,17 @@ class OtherModel < ActiveRecord::Base
|
|
14
14
|
enum :status, [:active, :expired, :not_expired]
|
15
15
|
end
|
16
16
|
|
17
|
+
class ModelAllowingNil < ActiveRecord::Base
|
18
|
+
self.table_name = 'models'
|
19
|
+
|
20
|
+
extend Enumify::Model
|
21
|
+
|
22
|
+
belongs_to :model
|
23
|
+
|
24
|
+
enum :status, [:active, :expired, :not_expired], :allow_nil => true
|
25
|
+
end
|
26
|
+
|
27
|
+
|
17
28
|
describe :Enumify do
|
18
29
|
|
19
30
|
before(:each) do
|
@@ -29,6 +40,28 @@ describe :Enumify do
|
|
29
40
|
@not_expired_obj = OtherModel.create!(:status => :not_expired, :model => @canceled_obj)
|
30
41
|
end
|
31
42
|
|
43
|
+
describe "allow nil" do
|
44
|
+
|
45
|
+
before(:each) do
|
46
|
+
@obj_not_allowing_nil = Model.create
|
47
|
+
@obj_allowing_nil = ModelAllowingNil.create
|
48
|
+
end
|
49
|
+
|
50
|
+
describe "model allowing enum value to be nil" do
|
51
|
+
it "should be valid" do
|
52
|
+
@obj_allowing_nil.should be_valid
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
|
57
|
+
describe "model not allowing enum value to be nil" do
|
58
|
+
it "should be invalid" do
|
59
|
+
@obj_not_allowing_nil.should be_invalid
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
64
|
+
|
32
65
|
describe "short hand methods" do
|
33
66
|
describe "question mark (?)" do
|
34
67
|
it "should return true if value of enum equals a value" do
|
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.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- yon
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-
|
11
|
+
date: 2013-10-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -44,14 +44,14 @@ dependencies:
|
|
44
44
|
requirements:
|
45
45
|
- - '>='
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: '0'
|
47
|
+
version: '3.0'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
52
|
- - '>='
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version: '0'
|
54
|
+
version: '3.0'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: sqlite3
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -66,6 +66,20 @@ dependencies:
|
|
66
66
|
- - '>='
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: appraisal
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 0.3.8
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 0.3.8
|
69
83
|
description: |2
|
70
84
|
Enumify lets you add an enum command to ActiveRecord models
|
71
85
|
|
@@ -82,12 +96,18 @@ extra_rdoc_files: []
|
|
82
96
|
files:
|
83
97
|
- .gitignore
|
84
98
|
- .rspec
|
99
|
+
- .travis.yml
|
100
|
+
- Appraisals
|
85
101
|
- CHANGELOG.md
|
86
102
|
- Gemfile
|
87
103
|
- LICENSE
|
88
104
|
- Rakefile
|
89
105
|
- Readme.md
|
90
106
|
- enumify.gemspec
|
107
|
+
- gemfiles/rails30.gemfile
|
108
|
+
- gemfiles/rails31.gemfile
|
109
|
+
- gemfiles/rails32.gemfile
|
110
|
+
- gemfiles/rails40.gemfile
|
91
111
|
- lib/enumify.rb
|
92
112
|
- lib/enumify/model.rb
|
93
113
|
- lib/enumify/railtie.rb
|