activerecord_composed_of_enum 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +30 -0
- data/Rakefile +2 -0
- data/activerecord_composed_of_enum.gemspec +25 -0
- data/lib/active_record/composed_of_enum/version.rb +7 -0
- data/lib/active_record/composed_of_enum.rb +51 -0
- data/spec/active_record/composed_of_enum_spec.rb +112 -0
- metadata +120 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Rob Hanlon
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# ActiveRecord::ComposedOfEnum
|
2
|
+
|
3
|
+
Provides a ```composed_of_enum``` method for ActiveRecord models that binds
|
4
|
+
enum classes with an enum integer column.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
gem 'composed_of_enum'
|
11
|
+
|
12
|
+
And then execute:
|
13
|
+
|
14
|
+
$ bundle
|
15
|
+
|
16
|
+
Or install it yourself as:
|
17
|
+
|
18
|
+
$ gem install composed_of_enum
|
19
|
+
|
20
|
+
## Usage
|
21
|
+
|
22
|
+
See ```lib/active_record/composed_of_enum.rb for details```.
|
23
|
+
|
24
|
+
## Contributing
|
25
|
+
|
26
|
+
1. Fork it
|
27
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
28
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
29
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
30
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/active_record/composed_of_enum/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ['Rob Hanlon']
|
6
|
+
gem.email = ['rob@mediapiston.com']
|
7
|
+
gem.description = 'Provides syntax for composing models of enums.'
|
8
|
+
gem.summary = 'Provides a composed_of_enum method for ActiveRecord '\
|
9
|
+
'models that binds enum classes with an enum integer '\
|
10
|
+
'column.'
|
11
|
+
gem.homepage = 'http://www.github.com/ohwillie/activerecord_composed_of_enum'
|
12
|
+
|
13
|
+
gem.files = `git ls-files`.split($\)
|
14
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
15
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
16
|
+
gem.name = 'activerecord_composed_of_enum'
|
17
|
+
gem.require_paths = %w[lib]
|
18
|
+
gem.version = ActiveRecord::ComposedOfEnum::VERSION
|
19
|
+
|
20
|
+
gem.add_development_dependency 'rspec', '2.10.0'
|
21
|
+
gem.add_development_dependency 'activerecord', '3.2.6'
|
22
|
+
gem.add_development_dependency 'sqlite3', '1.3.6'
|
23
|
+
|
24
|
+
gem.add_dependency 'rbx-require-relative', '0.0.9'
|
25
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require 'require_relative'
|
2
|
+
|
3
|
+
require_relative 'composed_of_enum/version'
|
4
|
+
|
5
|
+
module ActiveRecord
|
6
|
+
module ComposedOfEnum
|
7
|
+
def composed_of_enum(part, options = {})
|
8
|
+
unless options.has_key?(:base)
|
9
|
+
raise ArgumentError, 'must provide :base option'
|
10
|
+
end
|
11
|
+
|
12
|
+
unless options.has_key?(:enumeration)
|
13
|
+
raise ArgumentError, 'must provide :enumeration option'
|
14
|
+
end
|
15
|
+
|
16
|
+
base = options[:base]
|
17
|
+
enum_column = :"#{part}_cd"
|
18
|
+
|
19
|
+
base.class_attribute(
|
20
|
+
enum_column,
|
21
|
+
:instance_reader => false,
|
22
|
+
:instance_reader => false
|
23
|
+
)
|
24
|
+
|
25
|
+
enumeration = options[:enumeration]
|
26
|
+
|
27
|
+
enumeration.each_with_index do |enum, cd|
|
28
|
+
enum.send(:"#{enum_column}=", cd)
|
29
|
+
end
|
30
|
+
|
31
|
+
validates(
|
32
|
+
enum_column,
|
33
|
+
:presence => true,
|
34
|
+
:inclusion => 0...enumeration.size
|
35
|
+
)
|
36
|
+
|
37
|
+
if options.has_key?(:default)
|
38
|
+
after_initialize do
|
39
|
+
send(:"#{part}=", options[:default]) unless send(part).present?
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
composed_of(
|
44
|
+
part,
|
45
|
+
:class_name => base.name,
|
46
|
+
:mapping => [enum_column.to_s],
|
47
|
+
:constructor => lambda { |cd| cd.present? ? enumeration[cd] : nil }
|
48
|
+
)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,112 @@
|
|
1
|
+
require 'require_relative'
|
2
|
+
require 'active_record'
|
3
|
+
|
4
|
+
require_relative '../../lib/active_record/composed_of_enum'
|
5
|
+
|
6
|
+
describe ActiveRecord::ComposedOfEnum do
|
7
|
+
let(:model_class) do
|
8
|
+
Class.new(ActiveRecord::Base) do
|
9
|
+
self.table_name = 'models'
|
10
|
+
|
11
|
+
# Needed because this is an anonymous class.
|
12
|
+
def self.model_name
|
13
|
+
ActiveModel::Name.new(nil, nil, 'Model')
|
14
|
+
end
|
15
|
+
|
16
|
+
extend ActiveRecord::ComposedOfEnum
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
before do
|
21
|
+
ActiveRecord::Base.establish_connection(
|
22
|
+
:adapter => 'sqlite3',
|
23
|
+
:database => ':memory:'
|
24
|
+
)
|
25
|
+
|
26
|
+
ActiveRecord::Base.connection.create_table(:models) do |t|
|
27
|
+
t.integer(:enum_cd)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
after do
|
32
|
+
ActiveRecord::Base.connection.disconnect!
|
33
|
+
end
|
34
|
+
|
35
|
+
context 'ArgumentErrors' do
|
36
|
+
it 'should raise an ArgumentError if the :base option is missing' do
|
37
|
+
expect {
|
38
|
+
model_class.composed_of_enum(:enum, :enumeration => [Object])
|
39
|
+
}.to raise_error(ArgumentError, /:base/)
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'should raise an ArgumentError if the :enumeration option is missing' do
|
43
|
+
expect {
|
44
|
+
model_class.composed_of_enum(:enum, :base => Object)
|
45
|
+
}.to raise_error(ArgumentError, /:enumeration/)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
context 'success' do
|
50
|
+
let(:base) do
|
51
|
+
# This hack is here because we need an anonymous class, but it has to
|
52
|
+
# return itself when name.constantize is called.
|
53
|
+
Class.new.tap { |c| c.stub(:name) { double(:constantize => c) } }
|
54
|
+
end
|
55
|
+
let(:enumeration) { 3.times.map { Class.new(base) } }
|
56
|
+
|
57
|
+
context 'without a default' do
|
58
|
+
before do
|
59
|
+
model_class.composed_of_enum(
|
60
|
+
:enum,
|
61
|
+
:base => base,
|
62
|
+
:enumeration => enumeration
|
63
|
+
)
|
64
|
+
end
|
65
|
+
|
66
|
+
it 'should make an enum_cd method on base' do
|
67
|
+
base.enum_cd.should be_nil
|
68
|
+
end
|
69
|
+
|
70
|
+
it 'should set enum_cds for all enumeration classes' do
|
71
|
+
enumeration.each_with_index do |enum, index|
|
72
|
+
enum.enum_cd.should == index
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
it 'should not allow a model instance with a negative enum_cd value' do
|
77
|
+
model_class.new(:enum_cd => -1).should_not be_valid
|
78
|
+
end
|
79
|
+
|
80
|
+
it 'should allow a model instance with a valid enum_cd value' do
|
81
|
+
enumeration.each_with_index do |enum, index|
|
82
|
+
model_class.new(:enum_cd => index).should be_valid
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
it 'should not allow a model instance with too large an enum_cd value' do
|
87
|
+
model_class.new(:enum_cd => enumeration.size).should_not be_valid
|
88
|
+
end
|
89
|
+
|
90
|
+
it 'should set the enum and enum_cd if fed an enum' do
|
91
|
+
enumeration.each do |enum|
|
92
|
+
model = model_class.create(:enum => enum)
|
93
|
+
model.enum.should == enum
|
94
|
+
model.enum_cd.should == enum.enum_cd
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
it 'should set a default after initialize if provided' do
|
100
|
+
model_class.composed_of_enum(
|
101
|
+
:enum,
|
102
|
+
:base => base,
|
103
|
+
:enumeration => enumeration,
|
104
|
+
:default => enumeration.first
|
105
|
+
)
|
106
|
+
|
107
|
+
model = model_class.create
|
108
|
+
model.enum.should == enumeration.first
|
109
|
+
model.enum_cd.should == enumeration.first.enum_cd
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
metadata
ADDED
@@ -0,0 +1,120 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: activerecord_composed_of_enum
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Rob Hanlon
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-06-14 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - '='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 2.10.0
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - '='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 2.10.0
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: activerecord
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - '='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: 3.2.6
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - '='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 3.2.6
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: sqlite3
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - '='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: 1.3.6
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - '='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 1.3.6
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: rbx-require-relative
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - '='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: 0.0.9
|
70
|
+
type: :runtime
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - '='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: 0.0.9
|
78
|
+
description: Provides syntax for composing models of enums.
|
79
|
+
email:
|
80
|
+
- rob@mediapiston.com
|
81
|
+
executables: []
|
82
|
+
extensions: []
|
83
|
+
extra_rdoc_files: []
|
84
|
+
files:
|
85
|
+
- .gitignore
|
86
|
+
- Gemfile
|
87
|
+
- LICENSE
|
88
|
+
- README.md
|
89
|
+
- Rakefile
|
90
|
+
- activerecord_composed_of_enum.gemspec
|
91
|
+
- lib/active_record/composed_of_enum.rb
|
92
|
+
- lib/active_record/composed_of_enum/version.rb
|
93
|
+
- spec/active_record/composed_of_enum_spec.rb
|
94
|
+
homepage: http://www.github.com/ohwillie/activerecord_composed_of_enum
|
95
|
+
licenses: []
|
96
|
+
post_install_message:
|
97
|
+
rdoc_options: []
|
98
|
+
require_paths:
|
99
|
+
- lib
|
100
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
101
|
+
none: false
|
102
|
+
requirements:
|
103
|
+
- - ! '>='
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '0'
|
106
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
107
|
+
none: false
|
108
|
+
requirements:
|
109
|
+
- - ! '>='
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
version: '0'
|
112
|
+
requirements: []
|
113
|
+
rubyforge_project:
|
114
|
+
rubygems_version: 1.8.24
|
115
|
+
signing_key:
|
116
|
+
specification_version: 3
|
117
|
+
summary: Provides a composed_of_enum method for ActiveRecord models that binds enum
|
118
|
+
classes with an enum integer column.
|
119
|
+
test_files:
|
120
|
+
- spec/active_record/composed_of_enum_spec.rb
|