enum_type 1.0.0
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.
- data/.document +5 -0
- data/.gitignore +25 -0
- data/.rspec +1 -0
- data/Gemfile +16 -0
- data/Gemfile.lock +53 -0
- data/LICENSE +20 -0
- data/README.textile +45 -0
- data/Rakefile +46 -0
- data/VERSION +1 -0
- data/enum_type.gemspec +58 -0
- data/lib/enum_type.rb +42 -0
- data/spec/enum_type_spec.rb +97 -0
- data/spec/factories.rb +1 -0
- metadata +91 -0
data/.document
ADDED
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
-cfs
|
data/Gemfile
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
source 'http://rubygems.org'
|
2
|
+
|
3
|
+
# DEPENDENCIES
|
4
|
+
gem 'activerecord', require: 'active_record'
|
5
|
+
gem 'activesupport', require: 'active_support/core_ext/object/try'
|
6
|
+
|
7
|
+
# DEVELOPMENT
|
8
|
+
gem 'jeweler'
|
9
|
+
gem 'yard'
|
10
|
+
gem 'RedCloth', require: 'redcloth'
|
11
|
+
|
12
|
+
# TEST
|
13
|
+
gem 'rspec'
|
14
|
+
group :test do
|
15
|
+
gem 'factory_girl'
|
16
|
+
end
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
RedCloth (4.2.3)
|
5
|
+
activemodel (3.0.1)
|
6
|
+
activesupport (= 3.0.1)
|
7
|
+
builder (~> 2.1.2)
|
8
|
+
i18n (~> 0.4.1)
|
9
|
+
activerecord (3.0.1)
|
10
|
+
activemodel (= 3.0.1)
|
11
|
+
activesupport (= 3.0.1)
|
12
|
+
arel (~> 1.0.0)
|
13
|
+
tzinfo (~> 0.3.23)
|
14
|
+
activesupport (3.0.1)
|
15
|
+
arel (1.0.1)
|
16
|
+
activesupport (~> 3.0.0)
|
17
|
+
builder (2.1.2)
|
18
|
+
diff-lcs (1.1.2)
|
19
|
+
factory_girl (1.3.2)
|
20
|
+
gemcutter (0.6.1)
|
21
|
+
git (1.2.5)
|
22
|
+
i18n (0.4.2)
|
23
|
+
jeweler (1.4.0)
|
24
|
+
gemcutter (>= 0.1.0)
|
25
|
+
git (>= 1.2.5)
|
26
|
+
rubyforge (>= 2.0.0)
|
27
|
+
json_pure (1.4.6)
|
28
|
+
rspec (2.0.1)
|
29
|
+
rspec-core (~> 2.0.1)
|
30
|
+
rspec-expectations (~> 2.0.1)
|
31
|
+
rspec-mocks (~> 2.0.1)
|
32
|
+
rspec-core (2.0.1)
|
33
|
+
rspec-expectations (2.0.1)
|
34
|
+
diff-lcs (>= 1.1.2)
|
35
|
+
rspec-mocks (2.0.1)
|
36
|
+
rspec-core (~> 2.0.1)
|
37
|
+
rspec-expectations (~> 2.0.1)
|
38
|
+
rubyforge (2.0.4)
|
39
|
+
json_pure (>= 1.1.7)
|
40
|
+
tzinfo (0.3.23)
|
41
|
+
yard (0.6.1)
|
42
|
+
|
43
|
+
PLATFORMS
|
44
|
+
ruby
|
45
|
+
|
46
|
+
DEPENDENCIES
|
47
|
+
RedCloth
|
48
|
+
activerecord
|
49
|
+
activesupport
|
50
|
+
factory_girl
|
51
|
+
jeweler
|
52
|
+
rspec
|
53
|
+
yard
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 Tim Morgan
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.textile
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
h1. enum_type -- Enumerated Types in ActiveRecord
|
2
|
+
|
3
|
+
| *Author* | Tim Morgan |
|
4
|
+
| *Version* | 1.0 (Oct 29, 2010) |
|
5
|
+
| *License* | Released under the MIT license. |
|
6
|
+
|
7
|
+
h2. About
|
8
|
+
|
9
|
+
@enum_type@ allows you to effectively use the PostgreSQL @ENUM@ data type in
|
10
|
+
your ActiveRecord models. It's a really simple gem that just adds a convenience
|
11
|
+
method to take care of the usual "witch chant" that accompanies building an
|
12
|
+
enumerated type in Rails.
|
13
|
+
|
14
|
+
h2. Installation
|
15
|
+
|
16
|
+
*Important Note:* This gem requires Ruby 1.9. Ruby 1.8 is not supported, and
|
17
|
+
will never be.
|
18
|
+
|
19
|
+
First, add the @enum_type@ gem to your @Gemfile@:
|
20
|
+
|
21
|
+
<pre><code>
|
22
|
+
gem 'enum_type'
|
23
|
+
</code></pre>
|
24
|
+
|
25
|
+
Then, extend your model with the @EnumType@ module:
|
26
|
+
|
27
|
+
<pre><code>
|
28
|
+
class MyModel < ActiveRecord::Base
|
29
|
+
extend EnumType
|
30
|
+
end
|
31
|
+
</code></pre>
|
32
|
+
|
33
|
+
h2. Usage
|
34
|
+
|
35
|
+
In your model, call the @enum_type@ method, providing one or more enumerated
|
36
|
+
fields, and any additional options:
|
37
|
+
|
38
|
+
<pre><code>
|
39
|
+
class MyModel < ActiveRecord::Base
|
40
|
+
extend EnumType
|
41
|
+
enum_type :status, %w( active pending admin superadmin banned )
|
42
|
+
end
|
43
|
+
</code></pre>
|
44
|
+
|
45
|
+
See the @enum_type@ method documentation for more information.
|
data/Rakefile
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'rake'
|
2
|
+
begin
|
3
|
+
require 'bundler'
|
4
|
+
rescue LoadError
|
5
|
+
puts "Bundler is not installed; install with `gem install bundler`."
|
6
|
+
exit 1
|
7
|
+
end
|
8
|
+
|
9
|
+
Bundler.require :default
|
10
|
+
|
11
|
+
Jeweler::Tasks.new do |gem|
|
12
|
+
gem.name = "enum_type"
|
13
|
+
gem.summary = %Q{PostgreSQL enumerated types in ActiveRecord}
|
14
|
+
gem.description = %Q{Allows ActiveRecord to better use PostgreSQL's ENUM types.}
|
15
|
+
gem.email = "git@timothymorgan.info"
|
16
|
+
gem.homepage = "http://github.com/riscfuture/enum_type"
|
17
|
+
gem.authors = [ "Tim Morgan" ]
|
18
|
+
gem.required_ruby_version = '>= 1.9'
|
19
|
+
gem.add_dependency "activerecord", ">= 0"
|
20
|
+
end
|
21
|
+
Jeweler::GemcutterTasks.new
|
22
|
+
|
23
|
+
desc "Build API documentation"
|
24
|
+
YARD::Rake::YardocTask.new('doc') do |doc|
|
25
|
+
doc.options << "-m" << "textile"
|
26
|
+
doc.options << "--protected"
|
27
|
+
doc.options << "-r" << "README.textile"
|
28
|
+
doc.options << "-o" << "doc"
|
29
|
+
doc.options << "--title" << "enum_type Documentation".inspect
|
30
|
+
|
31
|
+
doc.files = [ 'lib/**/*', 'README.textile' ]
|
32
|
+
end
|
33
|
+
|
34
|
+
require 'rspec/core/rake_task'
|
35
|
+
|
36
|
+
desc 'Run all RSpecs'
|
37
|
+
task(default: :spec)
|
38
|
+
|
39
|
+
desc 'Run all RSpecs'
|
40
|
+
RSpec::Core::RakeTask.new
|
41
|
+
|
42
|
+
desc "Generate code coverage"
|
43
|
+
RSpec::Core::RakeTask.new(:coverage) do |t|
|
44
|
+
t.rcov = true
|
45
|
+
t.rcov_opts = %w( --exclude spec )
|
46
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.0.0
|
data/enum_type.gemspec
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{enum_type}
|
8
|
+
s.version = "1.0.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Tim Morgan"]
|
12
|
+
s.date = %q{2010-10-29}
|
13
|
+
s.description = %q{Allows ActiveRecord to better use PostgreSQL's ENUM types.}
|
14
|
+
s.email = %q{git@timothymorgan.info}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.textile"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".gitignore",
|
22
|
+
".rspec",
|
23
|
+
"Gemfile",
|
24
|
+
"Gemfile.lock",
|
25
|
+
"LICENSE",
|
26
|
+
"README.textile",
|
27
|
+
"Rakefile",
|
28
|
+
"VERSION",
|
29
|
+
"enum_type.gemspec",
|
30
|
+
"lib/enum_type.rb",
|
31
|
+
"spec/enum_type_spec.rb",
|
32
|
+
"spec/factories.rb"
|
33
|
+
]
|
34
|
+
s.homepage = %q{http://github.com/riscfuture/enum_type}
|
35
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
36
|
+
s.require_paths = ["lib"]
|
37
|
+
s.required_ruby_version = Gem::Requirement.new(">= 1.9")
|
38
|
+
s.rubygems_version = %q{1.3.7}
|
39
|
+
s.summary = %q{PostgreSQL enumerated types in ActiveRecord}
|
40
|
+
s.test_files = [
|
41
|
+
"spec/enum_type_spec.rb",
|
42
|
+
"spec/factories.rb"
|
43
|
+
]
|
44
|
+
|
45
|
+
if s.respond_to? :specification_version then
|
46
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
47
|
+
s.specification_version = 3
|
48
|
+
|
49
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
50
|
+
s.add_runtime_dependency(%q<activerecord>, [">= 0"])
|
51
|
+
else
|
52
|
+
s.add_dependency(%q<activerecord>, [">= 0"])
|
53
|
+
end
|
54
|
+
else
|
55
|
+
s.add_dependency(%q<activerecord>, [">= 0"])
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
data/lib/enum_type.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
# Adds the @enum_type@ method to a model.
|
2
|
+
#
|
3
|
+
# @example Basic usage
|
4
|
+
# class MyModel < ActiveRecord::Base
|
5
|
+
# extend EnumType
|
6
|
+
# enum_type :status, %w( open closed flagged deleted )
|
7
|
+
# end
|
8
|
+
|
9
|
+
module EnumType
|
10
|
+
|
11
|
+
# Defines a field whose type is an enumeration. Values are stored as strings
|
12
|
+
# in the backend (or your database's enumerated type), however in the Rails
|
13
|
+
# level they are represented as symbols.
|
14
|
+
#
|
15
|
+
# @overload enum_type(field, ..., options={})
|
16
|
+
# @param [Symbol] field An enumerated field.
|
17
|
+
# @param [Hash] options A hash of options.
|
18
|
+
# @option options [true, false] :allow_nil (false) If @true@, a nil value
|
19
|
+
# is allowed.
|
20
|
+
# @option options [Array<String>] :values If given, restricts valid values
|
21
|
+
# to those in the given array.
|
22
|
+
#
|
23
|
+
# @example Enumerated field with restricted types
|
24
|
+
# enum_type :color, values: %w( red orange yellow green blue purple )
|
25
|
+
|
26
|
+
def enum_type(*fields)
|
27
|
+
options = fields.extract_options!
|
28
|
+
fields.each do |field|
|
29
|
+
define_method field do
|
30
|
+
value = read_attribute(field)
|
31
|
+
value ? value.to_sym : value
|
32
|
+
end
|
33
|
+
|
34
|
+
define_method :"#{field}=" do |value|
|
35
|
+
write_attribute field, value.try(:to_s)
|
36
|
+
end
|
37
|
+
|
38
|
+
validates_presence_of(field) unless options[:allow_nil]
|
39
|
+
validates_inclusion_of(field, in: options[:values]) if options[:values]
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,97 @@
|
|
1
|
+
Bundler.require :default, :test
|
2
|
+
require "#{File.dirname __FILE__}/../lib/enum_type"
|
3
|
+
require 'factories'
|
4
|
+
|
5
|
+
module SpecSupport
|
6
|
+
class EnumTypeTester
|
7
|
+
include ActiveModel::Validations
|
8
|
+
extend EnumType
|
9
|
+
|
10
|
+
def self.set_field(field)
|
11
|
+
@field = field
|
12
|
+
reset_callbacks :validate
|
13
|
+
_validators.clear
|
14
|
+
enum_type @field
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.field() @field end
|
18
|
+
|
19
|
+
def read_attribute(_)
|
20
|
+
instance_variable_get :"@#{self.class.field}"
|
21
|
+
end
|
22
|
+
|
23
|
+
def write_attribute(_, value)
|
24
|
+
instance_variable_set :"@#{self.class.field}", value
|
25
|
+
end
|
26
|
+
|
27
|
+
def set(value)
|
28
|
+
instance_variable_set :"@#{self.class.field}", value
|
29
|
+
end
|
30
|
+
|
31
|
+
def get
|
32
|
+
instance_variable_get :"@#{self.class.field}"
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
describe EnumType do
|
38
|
+
before :each do
|
39
|
+
@field = Factory.next(:enum_field)
|
40
|
+
SpecSupport::EnumTypeTester.set_field(@field)
|
41
|
+
@model = SpecSupport::EnumTypeTester.new
|
42
|
+
end
|
43
|
+
|
44
|
+
describe "#enum_type" do
|
45
|
+
context "getter" do
|
46
|
+
it "should return a symbol" do
|
47
|
+
@model.set 'string'
|
48
|
+
@model.send(@field).should eql(:string)
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should return nil if the value is nil" do
|
52
|
+
@model.set nil
|
53
|
+
@model.send(@field).should be_nil
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
context "setter" do
|
58
|
+
it "should typecast the value to a string" do
|
59
|
+
@model.send :"#{@field}=", :string
|
60
|
+
@model.get.should eql('string')
|
61
|
+
end
|
62
|
+
|
63
|
+
it "should leave nil as nil" do
|
64
|
+
@model.send :"#{@field}=", nil
|
65
|
+
@model.get.should be_nil
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
it "should validate inclusion if :values option is given" do
|
70
|
+
SpecSupport::EnumTypeTester.enum_type @field, values: [ :a, :b ]
|
71
|
+
@model.send :"#{@field}=", :a
|
72
|
+
@model.get.should eql('a')
|
73
|
+
@model.should be_valid
|
74
|
+
@model.send :"#{@field}=", :c
|
75
|
+
@model.should_not be_valid
|
76
|
+
end
|
77
|
+
|
78
|
+
it "should not validate presence if :allow_nil is true" do
|
79
|
+
pending "Doesn't work"
|
80
|
+
SpecSupport::EnumTypeTester.enum_type @field, allow_nil: true
|
81
|
+
@model.send :"#{@field}=", nil
|
82
|
+
@model.should be_valid
|
83
|
+
end
|
84
|
+
|
85
|
+
it "should validate presence if :allow_nil is not given" do
|
86
|
+
SpecSupport::EnumTypeTester.enum_type @field
|
87
|
+
@model.send :"#{@field}=", nil
|
88
|
+
@model.should_not be_valid
|
89
|
+
end
|
90
|
+
|
91
|
+
it "should validate presence if :allow_nil is false" do
|
92
|
+
SpecSupport::EnumTypeTester.enum_type @field, allow_nil: false
|
93
|
+
@model.send :"#{@field}=", nil
|
94
|
+
@model.should_not be_valid
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
data/spec/factories.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
Factory.sequence(:enum_field) { |i| :"field#{i}" }
|
metadata
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: enum_type
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 1
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
version: 1.0.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Tim Morgan
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-10-29 00:00:00 -07:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: activerecord
|
22
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
23
|
+
none: false
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 0
|
29
|
+
version: "0"
|
30
|
+
type: :runtime
|
31
|
+
prerelease: false
|
32
|
+
version_requirements: *id001
|
33
|
+
description: Allows ActiveRecord to better use PostgreSQL's ENUM types.
|
34
|
+
email: git@timothymorgan.info
|
35
|
+
executables: []
|
36
|
+
|
37
|
+
extensions: []
|
38
|
+
|
39
|
+
extra_rdoc_files:
|
40
|
+
- LICENSE
|
41
|
+
- README.textile
|
42
|
+
files:
|
43
|
+
- .document
|
44
|
+
- .gitignore
|
45
|
+
- .rspec
|
46
|
+
- Gemfile
|
47
|
+
- Gemfile.lock
|
48
|
+
- LICENSE
|
49
|
+
- README.textile
|
50
|
+
- Rakefile
|
51
|
+
- VERSION
|
52
|
+
- enum_type.gemspec
|
53
|
+
- lib/enum_type.rb
|
54
|
+
- spec/enum_type_spec.rb
|
55
|
+
- spec/factories.rb
|
56
|
+
has_rdoc: true
|
57
|
+
homepage: http://github.com/riscfuture/enum_type
|
58
|
+
licenses: []
|
59
|
+
|
60
|
+
post_install_message:
|
61
|
+
rdoc_options:
|
62
|
+
- --charset=UTF-8
|
63
|
+
require_paths:
|
64
|
+
- lib
|
65
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
66
|
+
none: false
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
segments:
|
71
|
+
- 1
|
72
|
+
- 9
|
73
|
+
version: "1.9"
|
74
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
75
|
+
none: false
|
76
|
+
requirements:
|
77
|
+
- - ">="
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
segments:
|
80
|
+
- 0
|
81
|
+
version: "0"
|
82
|
+
requirements: []
|
83
|
+
|
84
|
+
rubyforge_project:
|
85
|
+
rubygems_version: 1.3.7
|
86
|
+
signing_key:
|
87
|
+
specification_version: 3
|
88
|
+
summary: PostgreSQL enumerated types in ActiveRecord
|
89
|
+
test_files:
|
90
|
+
- spec/enum_type_spec.rb
|
91
|
+
- spec/factories.rb
|