classy_enum 3.3.2 → 3.4.0.beta1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/LICENSE +1 -1
- data/README.md +17 -15
- data/classy_enum.gemspec +2 -2
- data/lib/classy_enum/base.rb +12 -1
- data/lib/classy_enum/collection.rb +13 -1
- data/lib/classy_enum/version.rb +1 -1
- data/spec/classy_enum/active_record_spec.rb +16 -4
- data/spec/classy_enum/base_spec.rb +10 -0
- data/spec/classy_enum/collection_spec.rb +12 -0
- data/spec/classy_enum_inheritance_spec.rb +5 -6
- data/spec/spec_helper.rb +0 -4
- metadata +20 -20
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 39e6a9bb72f8a98c0517d42b6bf3d3dba7f7832e
|
4
|
+
data.tar.gz: 1ba0072e00fd99397f99c5fdc216f5866f0ef9a6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1e2abe6cc896cfc7d681636fff656115e7f9d81407dcbeedfa90fec647e9b787564aa9a2da7065a7449dbb717f728bbab651a81f0f12ed4dcd5a0296b48614df
|
7
|
+
data.tar.gz: f5479eafdb914aa5feb877a7f51eebf786281414365dee10564e7c92b09e8d76e698cd3d2e0c08d8b0a127c59a5a52bca11d13efb51dd9b234a2ecd310bb44f0
|
data/CHANGELOG.md
CHANGED
data/LICENSE
CHANGED
data/README.md
CHANGED
@@ -25,7 +25,9 @@ See the [wiki](https://github.com/beerlington/classy_enum/wiki/Upgrading) for no
|
|
25
25
|
|
26
26
|
## Getting Started & Example Usage
|
27
27
|
|
28
|
-
The most common use for ClassyEnum is to replace database lookup tables where the content and behavior is mostly static and has multiple "types".
|
28
|
+
The most common use for ClassyEnum is to replace database lookup tables where the content and behavior is mostly static and has multiple "types". Please see the Wiki for a short discussion on use cases [comparing ClassyEnum to other gems](https://github.com/beerlington/classy_enum/wiki/ClassyEnum-vs-other-gems).
|
29
|
+
|
30
|
+
In this example, I have an Active Record model called `Alarm` with an attribute called `priority`. Priority is stored as a string (VARCHAR) type in the database and is converted to an enum value when requested.
|
29
31
|
|
30
32
|
### 1. Generate the Enum
|
31
33
|
|
@@ -130,6 +132,19 @@ With this setup, I can now do the following:
|
|
130
132
|
|
131
133
|
The enum field works like any other model attribute. It can be mass-assigned using `#update_attributes`.
|
132
134
|
|
135
|
+
#### What if your enum class name is not the same as your model's attribute name?
|
136
|
+
|
137
|
+
Just provide an optional `enum` argument to declare the attribute name. In this case, the model's attribute is called *alarm_priority*.
|
138
|
+
|
139
|
+
```ruby
|
140
|
+
class Alarm < ActiveRecord::Base
|
141
|
+
classy_enum_attr :alarm_priority, enum: 'Priority'
|
142
|
+
end
|
143
|
+
|
144
|
+
@alarm = Alarm.create(alarm_priority: :medium)
|
145
|
+
@alarm.alarm_priority # => Priority::Medium
|
146
|
+
```
|
147
|
+
|
133
148
|
## Internationalization
|
134
149
|
|
135
150
|
ClassyEnum provides built-in support for translations using Ruby's I18n
|
@@ -292,19 +307,6 @@ end
|
|
292
307
|
@alarm.to_json.should == "{\"alarm\":{\"priority\":{}}}"
|
293
308
|
```
|
294
309
|
|
295
|
-
## Special Cases
|
296
|
-
|
297
|
-
What if your enum class name is not the same as your model's attribute name? No problem! Just use a second argument in `classy_enum_attr` to declare the attribute name. In this case, the model's attribute is called *alarm_priority*.
|
298
|
-
|
299
|
-
```ruby
|
300
|
-
class Alarm < ActiveRecord::Base
|
301
|
-
classy_enum_attr :alarm_priority, enum: 'Priority'
|
302
|
-
end
|
303
|
-
|
304
|
-
@alarm = Alarm.create(alarm_priority: :medium)
|
305
|
-
@alarm.alarm_priority # => Priority::Medium
|
306
|
-
```
|
307
|
-
|
308
310
|
## Model Validation
|
309
311
|
|
310
312
|
An Active Record validator `validates_inclusion_of :field, in: ENUM` is automatically added to your model when you use `classy_enum_attr`.
|
@@ -359,4 +361,4 @@ available but needs to be enabled manually. To enable support visit
|
|
359
361
|
|
360
362
|
## Copyright
|
361
363
|
|
362
|
-
Copyright (c) 2010-
|
364
|
+
Copyright (c) 2010-2014 [Peter Brown](https://github.com/beerlington). See LICENSE for details.
|
data/classy_enum.gemspec
CHANGED
@@ -15,9 +15,9 @@ Gem::Specification.new do |gem|
|
|
15
15
|
gem.require_paths = ["lib"]
|
16
16
|
gem.version = ClassyEnum::VERSION
|
17
17
|
|
18
|
-
gem.add_dependency('
|
18
|
+
gem.add_dependency('activerecord', '>= 3.0')
|
19
19
|
|
20
|
-
gem.add_development_dependency('rspec
|
20
|
+
gem.add_development_dependency('rspec', '>= 2.11')
|
21
21
|
gem.add_development_dependency('sqlite3', '>= 1.3')
|
22
22
|
gem.add_development_dependency('json', '>= 1.6')
|
23
23
|
gem.add_development_dependency('debugger')
|
data/lib/classy_enum/base.rb
CHANGED
@@ -8,10 +8,21 @@ module ClassyEnum
|
|
8
8
|
include Translation
|
9
9
|
include Collection
|
10
10
|
|
11
|
-
class_attribute :base_class
|
12
11
|
attr_accessor :owner, :serialize_as_json, :allow_blank
|
13
12
|
|
13
|
+
def base_class
|
14
|
+
self.class.base_class
|
15
|
+
end
|
16
|
+
|
14
17
|
class << self
|
18
|
+
def base_class
|
19
|
+
@base_class ||= superclass.base_class
|
20
|
+
end
|
21
|
+
|
22
|
+
def base_class=(klass)
|
23
|
+
@base_class = klass
|
24
|
+
end
|
25
|
+
|
15
26
|
def inherited(klass)
|
16
27
|
return if klass.anonymous?
|
17
28
|
|
@@ -26,6 +26,10 @@ module ClassyEnum
|
|
26
26
|
index <=> other.index
|
27
27
|
end
|
28
28
|
|
29
|
+
def enum_options
|
30
|
+
self.class.enum_options
|
31
|
+
end
|
32
|
+
|
29
33
|
def self.included(klass)
|
30
34
|
klass.extend ClassMethods
|
31
35
|
end
|
@@ -36,7 +40,15 @@ module ClassyEnum
|
|
36
40
|
|
37
41
|
def inherited(klass)
|
38
42
|
if self == ClassyEnum::Base
|
39
|
-
klass.
|
43
|
+
klass.class_eval do
|
44
|
+
def self.enum_options
|
45
|
+
@enum_options ||= superclass.enum_options
|
46
|
+
end
|
47
|
+
|
48
|
+
def self.enum_options=(options)
|
49
|
+
@enum_options = options
|
50
|
+
end
|
51
|
+
end
|
40
52
|
klass.enum_options = []
|
41
53
|
else
|
42
54
|
enum_options << klass
|
data/lib/classy_enum/version.rb
CHANGED
@@ -78,7 +78,10 @@ describe DefaultDog do
|
|
78
78
|
context "with invalid breed options" do
|
79
79
|
subject { DefaultDog.new(:breed => :fake_breed) }
|
80
80
|
it { should_not be_valid }
|
81
|
-
it
|
81
|
+
it 'has an error on :breed' do
|
82
|
+
subject.valid?
|
83
|
+
subject.errors[:breed].size.should eql(1)
|
84
|
+
end
|
82
85
|
end
|
83
86
|
end
|
84
87
|
|
@@ -95,7 +98,10 @@ describe "A ClassyEnum that allows blanks" do
|
|
95
98
|
context "with invalid breed options" do
|
96
99
|
subject { AllowBlankBreedDog.new(:breed => :fake_breed) }
|
97
100
|
it { should_not be_valid }
|
98
|
-
it
|
101
|
+
it 'has an error on :breed' do
|
102
|
+
subject.valid?
|
103
|
+
subject.errors[:breed].size.should eql(1)
|
104
|
+
end
|
99
105
|
end
|
100
106
|
end
|
101
107
|
|
@@ -112,7 +118,10 @@ describe "A ClassyEnum that allows nils" do
|
|
112
118
|
context "with invalid breed options" do
|
113
119
|
subject { AllowNilBreedDog.new(:breed => :fake_breed) }
|
114
120
|
it { should_not be_valid }
|
115
|
-
it
|
121
|
+
it 'has an error on :breed' do
|
122
|
+
subject.valid?
|
123
|
+
subject.errors[:breed].size.should eql(1)
|
124
|
+
end
|
116
125
|
end
|
117
126
|
end
|
118
127
|
|
@@ -137,7 +146,10 @@ describe ActiveDog do
|
|
137
146
|
ActiveDog.create!(:name => 'Kitteh', :breed => :husky, :color => :black)
|
138
147
|
end
|
139
148
|
|
140
|
-
it
|
149
|
+
it 'has an error on :name' do
|
150
|
+
subject.valid?
|
151
|
+
subject.errors[:name].size.should eql(1)
|
152
|
+
end
|
141
153
|
end
|
142
154
|
end
|
143
155
|
|
@@ -55,6 +55,16 @@ describe ClassyEnum::Base do
|
|
55
55
|
}.should raise_error(ClassyEnum::SubclassNameError)
|
56
56
|
end
|
57
57
|
end
|
58
|
+
|
59
|
+
context '#base_class' do
|
60
|
+
let(:base_class) { double }
|
61
|
+
|
62
|
+
it 'returns class base_class' do
|
63
|
+
enum = ClassyEnumBase.build(:two)
|
64
|
+
enum.class.base_class = base_class
|
65
|
+
enum.base_class.should == base_class
|
66
|
+
end
|
67
|
+
end
|
58
68
|
end
|
59
69
|
|
60
70
|
describe ClassyEnum::Base, 'Arel visitor' do
|
@@ -71,6 +71,18 @@ describe ClassyEnum::Collection do
|
|
71
71
|
enum.select(&:odd?).should == [ClassyEnumCollection::One.new, ClassyEnumCollection::Three.new]
|
72
72
|
end
|
73
73
|
end
|
74
|
+
|
75
|
+
context '#enum_options' do
|
76
|
+
let(:options) { double }
|
77
|
+
|
78
|
+
before do
|
79
|
+
subject.enum_options = options
|
80
|
+
end
|
81
|
+
|
82
|
+
it 'returns class enum_options' do
|
83
|
+
subject.new.enum_options.should == options
|
84
|
+
end
|
85
|
+
end
|
74
86
|
end
|
75
87
|
|
76
88
|
describe ClassyEnum::Collection, Comparable do
|
@@ -1,25 +1,24 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
2
|
|
3
3
|
class ProjectTier < ClassyEnum::Base
|
4
|
-
|
4
|
+
def self.hello
|
5
|
+
'world'
|
6
|
+
end
|
5
7
|
end
|
6
8
|
|
7
9
|
class ProjectTier::One < ProjectTier
|
8
|
-
self.inherited_properties = [1,2,3]
|
9
10
|
end
|
10
11
|
|
11
12
|
class ProjectTier::Two < ProjectTier::One
|
12
|
-
self.inherited_properties += [4,5,6]
|
13
13
|
end
|
14
14
|
|
15
15
|
describe 'Classy Enum inheritance' do
|
16
16
|
it 'should inherit from the previous class' do
|
17
|
-
ProjectTier::
|
18
|
-
ProjectTier::Two.inherited_properties.should eql([1,2,3,4,5,6])
|
17
|
+
ProjectTier::Two.hello.should eq(ProjectTier::One.hello)
|
19
18
|
end
|
20
19
|
|
21
20
|
it 'should instantiate the subclass' do
|
22
|
-
ProjectTier
|
21
|
+
ProjectTier.build(:two).should == ProjectTier::Two.new
|
23
22
|
end
|
24
23
|
|
25
24
|
it 'should have the right index' do
|
data/spec/spec_helper.rb
CHANGED
@@ -2,11 +2,7 @@ $LOAD_PATH.unshift(File.dirname(__FILE__))
|
|
2
2
|
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
3
3
|
|
4
4
|
require 'rubygems'
|
5
|
-
require 'rails'
|
6
5
|
require 'active_record'
|
7
|
-
require 'action_view'
|
8
|
-
require 'action_controller'
|
9
|
-
require 'rspec/rails'
|
10
6
|
require 'classy_enum'
|
11
7
|
|
12
8
|
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
|
metadata
CHANGED
@@ -1,83 +1,83 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: classy_enum
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.
|
4
|
+
version: 3.4.0.beta1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Peter Brown
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2014-01-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: activerecord
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '3.0'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- -
|
24
|
+
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '3.0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
|
-
name: rspec
|
28
|
+
name: rspec
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- -
|
31
|
+
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '2.11'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- -
|
38
|
+
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '2.11'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: sqlite3
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- -
|
45
|
+
- - ">="
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
version: '1.3'
|
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
54
|
version: '1.3'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: json
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
|
-
- -
|
59
|
+
- - ">="
|
60
60
|
- !ruby/object:Gem::Version
|
61
61
|
version: '1.6'
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
|
-
- -
|
66
|
+
- - ">="
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '1.6'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: debugger
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
|
-
- -
|
73
|
+
- - ">="
|
74
74
|
- !ruby/object:Gem::Version
|
75
75
|
version: '0'
|
76
76
|
type: :development
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
|
-
- -
|
80
|
+
- - ">="
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '0'
|
83
83
|
description: A utility that adds class based enum functionality to ActiveRecord attributes
|
@@ -87,8 +87,8 @@ executables: []
|
|
87
87
|
extensions: []
|
88
88
|
extra_rdoc_files: []
|
89
89
|
files:
|
90
|
-
- .gitignore
|
91
|
-
- .travis.yml
|
90
|
+
- ".gitignore"
|
91
|
+
- ".travis.yml"
|
92
92
|
- CHANGELOG.md
|
93
93
|
- Gemfile
|
94
94
|
- LICENSE
|
@@ -133,17 +133,17 @@ require_paths:
|
|
133
133
|
- lib
|
134
134
|
required_ruby_version: !ruby/object:Gem::Requirement
|
135
135
|
requirements:
|
136
|
-
- -
|
136
|
+
- - ">="
|
137
137
|
- !ruby/object:Gem::Version
|
138
138
|
version: '0'
|
139
139
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
140
140
|
requirements:
|
141
|
-
- -
|
141
|
+
- - ">"
|
142
142
|
- !ruby/object:Gem::Version
|
143
|
-
version:
|
143
|
+
version: 1.3.1
|
144
144
|
requirements: []
|
145
145
|
rubyforge_project:
|
146
|
-
rubygems_version: 2.
|
146
|
+
rubygems_version: 2.2.0
|
147
147
|
signing_key:
|
148
148
|
specification_version: 4
|
149
149
|
summary: A class based enumerator utility for Ruby on Rails
|