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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6b5e524f240a067b06540a32326291865c420b85
4
- data.tar.gz: ee00750660d876fce2e8f5e916cd9549c7e3153d
3
+ metadata.gz: a8afbb3738f0c1aba03e6447384e9fe19c628905
4
+ data.tar.gz: 7a3e37ec4b0e53194e13005d8c46ffcd5c9ff4b6
5
5
  SHA512:
6
- metadata.gz: 2ffc622bd738b6049023cc6b4f0b91ab63b2cfe193b48aed43aa379fd8dd1fa50aacca01969e011f66dd1c572c616995e85285ff30f50d511af59607cb727e0d
7
- data.tar.gz: 8af5a86231191e753b9436289801dc26c175757d21f28ac79757fb2e0535c6dc51f471f5f82581864eaf3fa8a1e3508b915a56b420c4ca0211383691d0232ecf
6
+ metadata.gz: cee67d6734399da89000f4a503acd71098842395bd5ef6d6fdd1e7d08bbed1fe7cc8debcff7b20dfcc3cc5a2533845f4b8ab38fce479519b210b31fd14cc9850
7
+ data.tar.gz: ccb3e32f9d9dd9c7fa8457097d3a23cd5899585a3edbce0a3491a1f268a80f4180b72e2d12b7e507e9895b1c3da44f10709a5fbd65d195a87b142a63ac001583
@@ -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
@@ -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
@@ -1,3 +1,6 @@
1
+ ## v0.0.5
2
+ * Added 'allow_nil' as an option.
3
+
1
4
  ## v0.0.3
2
5
  * Fixed bug in not_ scopes
3
6
 
data/Rakefile CHANGED
@@ -1,5 +1,6 @@
1
1
  require "bundler/gem_tasks"
2
2
  require 'rspec/core/rake_task'
3
+ require 'appraisal'
3
4
 
4
5
  RSpec::Core::RakeTask.new(:spec)
5
6
  task :default => :spec
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
@@ -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
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "http://rubygems.org"
4
+
5
+ gem "activerecord", "~> 3.0.11"
6
+
7
+ gemspec :path=>"../"
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "http://rubygems.org"
4
+
5
+ gem "activerecord", "~> 3.1.12"
6
+
7
+ gemspec :path=>"../"
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "http://rubygems.org"
4
+
5
+ gem "activerecord", "~> 3.2.14"
6
+
7
+ gemspec :path=>"../"
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "http://rubygems.org"
4
+
5
+ gem "activerecord", "~> 4.0.0"
6
+
7
+ gemspec :path=>"../"
@@ -1,10 +1,10 @@
1
1
  module Enumify
2
2
  module Model
3
- def enum(parameter, opts=[])
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}", opts)
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
- opts.each do |opt|
35
- raise "Collision in enum values method #{opt}" if respond_to?("#{opt.to_s}?") or respond_to?("#{opt.to_s}!") or respond_to?("#{opt.to_s}")
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 "#{opt.to_s}?" do
38
- send("#{parameter.to_s}") == opt
37
+ define_method "#{val.to_s}?" do
38
+ send("#{parameter.to_s}") == val
39
39
  end
40
40
 
41
- define_method "#{opt.to_s}!" do
42
- send("_set_#{parameter.to_s}", opt, true)
41
+ define_method "#{val.to_s}!" do
42
+ send("_set_#{parameter.to_s}", val, true)
43
43
  end
44
44
 
45
- scope opt.to_sym, lambda { where(parameter.to_sym => opt.to_s) }
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
- opts.each do |opt|
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_#{opt}")
55
- scope "not_#{opt}", lambda { where("#{self.table_name}.#{parameter} != ?", opt.to_s) }
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
 
@@ -1,3 +1,3 @@
1
1
  module Enumify
2
- VERSION = '0.0.4'
2
+ VERSION = '0.0.5'
3
3
  end
@@ -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
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-08-06 00:00:00.000000000 Z
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