has_enumeration 0.5.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +3 -3
- data/Gemfile.lock +37 -33
- data/HISTORY.txt +9 -0
- data/README.md +125 -0
- data/Rakefile +8 -1
- data/VERSION +1 -1
- data/features/step_definitions/has_enumeration_steps.rb +3 -3
- data/lib/has_enumeration/arel/table_extensions.rb +5 -0
- data/lib/has_enumeration/class_methods.rb +38 -2
- data/spec/has_enumeration/has_enumeration_spec.rb +67 -0
- data/spec/spec_helper.rb +23 -0
- metadata +13 -11
- data/README.txt +0 -53
- data/TODO +0 -5
data/Gemfile
CHANGED
@@ -3,12 +3,12 @@ source 'http://rubygems.org'
|
|
3
3
|
if ENV['AR_VERSION'] == '2.3'
|
4
4
|
gem 'activerecord', '~> 2.3.8'
|
5
5
|
else
|
6
|
-
gem 'activerecord', '
|
6
|
+
gem 'activerecord', '~> 3.0.1'
|
7
7
|
gem 'meta_where'
|
8
8
|
end
|
9
9
|
|
10
|
-
gem 'rspec', '~> 2.0.
|
11
|
-
gem 'cucumber', '~> 0.
|
10
|
+
gem 'rspec', '~> 2.0.1'
|
11
|
+
gem 'cucumber', '~> 0.9.3'
|
12
12
|
|
13
13
|
platforms :ruby do
|
14
14
|
gem 'sqlite3-ruby', :require => 'sqlite3'
|
data/Gemfile.lock
CHANGED
@@ -1,52 +1,56 @@
|
|
1
1
|
GEM
|
2
2
|
remote: http://rubygems.org/
|
3
3
|
specs:
|
4
|
-
activemodel (3.0.
|
5
|
-
activesupport (= 3.0.
|
4
|
+
activemodel (3.0.1)
|
5
|
+
activesupport (= 3.0.1)
|
6
6
|
builder (~> 2.1.2)
|
7
7
|
i18n (~> 0.4.1)
|
8
|
-
activerecord (3.0.
|
9
|
-
activemodel (= 3.0.
|
10
|
-
activesupport (= 3.0.
|
8
|
+
activerecord (3.0.1)
|
9
|
+
activemodel (= 3.0.1)
|
10
|
+
activesupport (= 3.0.1)
|
11
11
|
arel (~> 1.0.0)
|
12
12
|
tzinfo (~> 0.3.23)
|
13
13
|
activerecord-jdbc-adapter (0.9.7-java)
|
14
14
|
activerecord-jdbcsqlite3-adapter (0.9.7-java)
|
15
15
|
activerecord-jdbc-adapter (= 0.9.7)
|
16
16
|
jdbc-sqlite3 (>= 3.6.3.054)
|
17
|
-
activesupport (3.0.
|
17
|
+
activesupport (3.0.1)
|
18
18
|
arel (1.0.1)
|
19
19
|
activesupport (~> 3.0.0)
|
20
20
|
builder (2.1.2)
|
21
|
-
cucumber (0.
|
21
|
+
cucumber (0.9.3)
|
22
22
|
builder (~> 2.1.2)
|
23
23
|
diff-lcs (~> 1.1.2)
|
24
|
-
gherkin (~> 2.
|
25
|
-
|
26
|
-
term-ansicolor (~> 1.0.
|
24
|
+
gherkin (~> 2.2.9)
|
25
|
+
json (~> 1.4.6)
|
26
|
+
term-ansicolor (~> 1.0.5)
|
27
27
|
diff-lcs (1.1.2)
|
28
|
-
gherkin (2.
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
28
|
+
gherkin (2.2.9)
|
29
|
+
json (~> 1.4.6)
|
30
|
+
term-ansicolor (~> 1.0.5)
|
31
|
+
gherkin (2.2.9-java)
|
32
|
+
json (~> 1.4.6)
|
33
|
+
term-ansicolor (~> 1.0.5)
|
34
|
+
i18n (0.4.2)
|
35
|
+
jdbc-sqlite3 (3.6.14.2.056-java)
|
36
|
+
json (1.4.6)
|
37
|
+
json (1.4.6-java)
|
38
|
+
meta_where (0.9.6)
|
39
|
+
activerecord (~> 3.0.0)
|
40
|
+
activesupport (~> 3.0.0)
|
41
|
+
arel (~> 1.0.1)
|
42
|
+
rspec (2.0.1)
|
43
|
+
rspec-core (~> 2.0.1)
|
44
|
+
rspec-expectations (~> 2.0.1)
|
45
|
+
rspec-mocks (~> 2.0.1)
|
46
|
+
rspec-core (2.0.1)
|
47
|
+
rspec-expectations (2.0.1)
|
45
48
|
diff-lcs (>= 1.1.2)
|
46
|
-
rspec-mocks (2.0.
|
47
|
-
|
49
|
+
rspec-mocks (2.0.1)
|
50
|
+
rspec-core (~> 2.0.1)
|
51
|
+
rspec-expectations (~> 2.0.1)
|
52
|
+
sqlite3-ruby (1.3.2)
|
48
53
|
term-ansicolor (1.0.5)
|
49
|
-
trollop (1.16.2)
|
50
54
|
tzinfo (0.3.23)
|
51
55
|
|
52
56
|
PLATFORMS
|
@@ -54,10 +58,10 @@ PLATFORMS
|
|
54
58
|
ruby
|
55
59
|
|
56
60
|
DEPENDENCIES
|
57
|
-
activerecord (
|
61
|
+
activerecord (~> 3.0.1)
|
58
62
|
activerecord-jdbcsqlite3-adapter (~> 0.9.7)
|
59
|
-
cucumber (~> 0.
|
63
|
+
cucumber (~> 0.9.3)
|
60
64
|
jdbc-sqlite3
|
61
65
|
meta_where
|
62
|
-
rspec (~> 2.0.
|
66
|
+
rspec (~> 2.0.1)
|
63
67
|
sqlite3-ruby
|
data/HISTORY.txt
CHANGED
@@ -1,3 +1,12 @@
|
|
1
|
+
Version 1.0.0:
|
2
|
+
- Added #to_sym for generated enumeration class. #value is deprecated.
|
3
|
+
- Raises ArgumentError when assigning, or querying, with a symbol that is
|
4
|
+
not part of the enumeration
|
5
|
+
- A value of nil in underlying attribute results in a nil value for the
|
6
|
+
enumeration
|
7
|
+
- Assigning nil to the enumeration results in a value of nil for the underlying
|
8
|
+
attribute
|
9
|
+
|
1
10
|
Version 0.5.0:
|
2
11
|
- Default name of underlying attribute can be overridden with the :attribute
|
3
12
|
option
|
data/README.md
ADDED
@@ -0,0 +1,125 @@
|
|
1
|
+
# has_enumeration
|
2
|
+
## Overview
|
3
|
+
`has_enumeration` adds support for enumerated types to ActiveRecord. The values
|
4
|
+
of an enumerated attribute are represented as symbols regardless of the
|
5
|
+
attribute's underlying representation in the database. Predicates
|
6
|
+
(e.g. `car.color.red?`) are provided for each value of the enumerated type.
|
7
|
+
|
8
|
+
## Defining Enumerated Attributes
|
9
|
+
Enumerated attributes are declared in the model's class definition with the
|
10
|
+
`has_enumeration` class method. For example:
|
11
|
+
|
12
|
+
class Car < ActiveRecord::Base
|
13
|
+
has_enumeration :color, [:red, :green, :blue]
|
14
|
+
end
|
15
|
+
|
16
|
+
The above definition assumes that the `color` attribute is stored as a string
|
17
|
+
and that its values in the database match those obtained by calling `to_s` on
|
18
|
+
their corresponding symbols. I.e., :red is stored as 'red'.
|
19
|
+
|
20
|
+
If the underlying column is not a string or its values do not match the string
|
21
|
+
version of the enumeration's symbols, then the mapping between symbols and their
|
22
|
+
underlying values may be provided as a hash:
|
23
|
+
|
24
|
+
class Car < ActiveRecord::Base
|
25
|
+
has_enumeration :color, :red => 1, :green => 2, :blue => 3
|
26
|
+
end
|
27
|
+
|
28
|
+
By default, the underlying attribute is assumed to have the same name as the
|
29
|
+
enumeration. If this is not the case, the name of the underlying attibute may
|
30
|
+
be provided with the `:attribute` option:
|
31
|
+
|
32
|
+
class Car < ActiveRecord::Base
|
33
|
+
has_enumeration :color, {:red => 1, :green => 2, :blue => 3},
|
34
|
+
:attribute => :hue
|
35
|
+
end
|
36
|
+
|
37
|
+
## Using Enumerated Attributes
|
38
|
+
### Assignment
|
39
|
+
Enumerated attributes are assigned using symbols:
|
40
|
+
|
41
|
+
car = Car.new(:color => :red)
|
42
|
+
car.color = :blue
|
43
|
+
|
44
|
+
The symbols are coerced into an instance of a nested class (`Car::Color` in
|
45
|
+
this example) that is created by `has_enumeration`. If for some reason you
|
46
|
+
need to avoid the type coercion, you can assign a value of that class directly:
|
47
|
+
|
48
|
+
car.color = Car::Color.from_sym(:green)
|
49
|
+
|
50
|
+
### Querying
|
51
|
+
When constructing queries referencing the enumerated attribute, use the symbol
|
52
|
+
as its value:
|
53
|
+
|
54
|
+
Car.find(:all, :conditions => {:color => :red})
|
55
|
+
|
56
|
+
`has_enumeration` supports Rails 3 and is aware of the model's underlying Arel
|
57
|
+
representation:
|
58
|
+
|
59
|
+
Car.where(:color => :red)
|
60
|
+
Car.where(Car.arel_table[:color].not_in([:red, :green]))
|
61
|
+
|
62
|
+
If you are using MetaWhere, `has_enumeration` plays nicely with it:
|
63
|
+
|
64
|
+
# This example requires the meta_where:
|
65
|
+
Car.where(:color.not_in => [:red, :green])
|
66
|
+
|
67
|
+
### Testing Values
|
68
|
+
The primary means of interacting with enumerated attributes is through the
|
69
|
+
predicate methods that are automatically generated for each value in the
|
70
|
+
enumeration:
|
71
|
+
|
72
|
+
car = Car.new(:color => :red)
|
73
|
+
car.color.red?
|
74
|
+
# => true
|
75
|
+
car.color.green?
|
76
|
+
# => false
|
77
|
+
|
78
|
+
If the value of the attribute is needed as a symbol, e.g., for direct
|
79
|
+
comparison, it can be retrieved with `to_sym`:
|
80
|
+
|
81
|
+
car.color.to_sym
|
82
|
+
# => :red
|
83
|
+
|
84
|
+
## Installation
|
85
|
+
`has_enumeration` is packaged as a gem:
|
86
|
+
|
87
|
+
gem install has_enumeration
|
88
|
+
|
89
|
+
### Rails 3
|
90
|
+
To use `has_enumeration` with Rails 3, simply add it to your application's
|
91
|
+
Gemfile:
|
92
|
+
|
93
|
+
gem 'has_enumeration'
|
94
|
+
|
95
|
+
### Rails 2.x
|
96
|
+
To use `has_enumeration` with Rails 2, add it to your application's
|
97
|
+
environment.rb file:
|
98
|
+
|
99
|
+
config.gem 'has_enumeration'
|
100
|
+
|
101
|
+
## Supported Configurations
|
102
|
+
`has_enumeration` has been tested with the following versions of ActiveRecord:
|
103
|
+
|
104
|
+
* 2.3.10
|
105
|
+
* 3.0.1
|
106
|
+
|
107
|
+
and the following Ruby implementations:
|
108
|
+
|
109
|
+
* 1.8.7-p302
|
110
|
+
* 1.9.2-p0
|
111
|
+
* JRuby 1.5.3
|
112
|
+
* Rubinius 1.1.0
|
113
|
+
|
114
|
+
|
115
|
+
## Getting the Latest
|
116
|
+
`has_enumeration` is hosted on github at
|
117
|
+
[http://github.com/gregspurrier/has_enumeration](http://github.com/gregspurrier/has_enumeration).
|
118
|
+
|
119
|
+
You can make a local clone of the repository with the following command:
|
120
|
+
|
121
|
+
git clone git://github.com/gregspurrier/has_enumeration.git
|
122
|
+
|
123
|
+
## License
|
124
|
+
`has_enumeration` is Copyright 2010 by Greg Spurrier. It is released under
|
125
|
+
the MIT License. Please see LICENSE.txt for details.
|
data/Rakefile
CHANGED
@@ -1,6 +1,9 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
require 'cucumber'
|
3
3
|
require 'cucumber/rake/task'
|
4
|
+
require 'rspec'
|
5
|
+
require 'rspec/core/rake_task'
|
6
|
+
|
4
7
|
|
5
8
|
begin
|
6
9
|
require 'jeweler'
|
@@ -15,7 +18,8 @@ in the database. Predicates are provided for each symbol in the enumeration
|
|
15
18
|
and the symbols may be used in finder methods. When using ActiveRecord 3,
|
16
19
|
the symbols may also be used when interacting with the underlying Arel attribute
|
17
20
|
for the enumeration. has_enumeration has been tested with Ruby 1.8.7,
|
18
|
-
Ruby 1.9.2, JRuby 1.5.3, ActiveRecord 2.3.
|
21
|
+
Ruby 1.9.2, JRuby 1.5.3, Rubinius 1.1.0, ActiveRecord 2.3.10, and ActiveRecord
|
22
|
+
3.0.1.
|
19
23
|
EOF
|
20
24
|
gemspec.email = "greg@rujubu.com"
|
21
25
|
gemspec.homepage = "http://github.com/gregspurrier/has_enumeration"
|
@@ -43,3 +47,6 @@ namespace :features do
|
|
43
47
|
t.cucumber_opts = feature_files
|
44
48
|
end
|
45
49
|
end
|
50
|
+
|
51
|
+
desc "Run all specs"
|
52
|
+
RSpec::Core::RakeTask.new('spec')
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
1.0.0
|
@@ -25,7 +25,7 @@ When /^I save and reload the object$/ do
|
|
25
25
|
end
|
26
26
|
|
27
27
|
Then /^it should have the assigned value as its value$/ do
|
28
|
-
@object.color.
|
28
|
+
@object.color.to_sym.should == @assigned
|
29
29
|
end
|
30
30
|
|
31
31
|
Then /^the ([a-z_]+\?) predicate should be (true|false)$/ do |predicate, value|
|
@@ -65,9 +65,9 @@ When /^I query for objects with the value :([a-z_]+) via MetaWhere$/ do |value|
|
|
65
65
|
end
|
66
66
|
|
67
67
|
Then /^I should get all of the objects having that value$/ do
|
68
|
-
@results.should == @all_objects.select {|x| x.color.
|
68
|
+
@results.should == @all_objects.select {|x| x.color.to_sym == @desired_color}
|
69
69
|
end
|
70
70
|
|
71
71
|
Then /^I should not get any objects having other values$/ do
|
72
|
-
@results.reject {|x| x.color.
|
72
|
+
@results.reject {|x| x.color.to_sym == @desired_color}.should be_empty
|
73
73
|
end
|
@@ -32,6 +32,11 @@ module HasEnumeration
|
|
32
32
|
(class <<arel_attr;self;end).class_eval do
|
33
33
|
define_method :map_enumeration_arg do |arg|
|
34
34
|
if arg.is_a?(Symbol)
|
35
|
+
unless mapping.has_key?(arg)
|
36
|
+
raise ArgumentError.new(
|
37
|
+
"#{arg.inspect} is not one of {#{mapping.keys.map(&:inspect).sort.join(', ')}}"
|
38
|
+
)
|
39
|
+
end
|
35
40
|
mapping[arg]
|
36
41
|
elsif arg.is_a?(Array)
|
37
42
|
arg.map {|a| map_enumeration_arg(a)}
|
@@ -1,5 +1,26 @@
|
|
1
1
|
module HasEnumeration
|
2
2
|
module ClassMethods
|
3
|
+
# Declares an enumerated attribute called +enumeration+ consisting of
|
4
|
+
# the symbols defined in +mapping+.
|
5
|
+
#
|
6
|
+
# When the database representation of the attribute is a string, +mapping+
|
7
|
+
# can be an array of symbols. The string representation of the symbol
|
8
|
+
# will be stored in the databased. E.g.:
|
9
|
+
#
|
10
|
+
# has_enumeration :color, [:red, :green, :blue]
|
11
|
+
#
|
12
|
+
# When the database representation of the attribute is not a string, or
|
13
|
+
# if its values do not match up with the string versions of its symbols,
|
14
|
+
# an hash mapping symbols to their underlying values may be used:
|
15
|
+
#
|
16
|
+
# has_enumeration :color, :red => 1, :green => 2, :blue => 3
|
17
|
+
#
|
18
|
+
# By default, has_enumeration assumes that the column in the database
|
19
|
+
# has the same name as the enumeration. If this is not the case, the
|
20
|
+
# column can be specified with the :attribute option:
|
21
|
+
#
|
22
|
+
# has_enumeration :color, [:red, :green, :blue], :attribute => :hue
|
23
|
+
#
|
3
24
|
def has_enumeration(enumeration, mapping, options = {})
|
4
25
|
unless mapping.is_a?(Hash)
|
5
26
|
# Recast the mapping as a symbol -> string hash
|
@@ -25,7 +46,8 @@ module HasEnumeration
|
|
25
46
|
composed_of(enumeration,
|
26
47
|
:class_name => scoped_class_name,
|
27
48
|
:mapping => [attribute.to_s, 'raw_value'],
|
28
|
-
:converter => :from_sym
|
49
|
+
:converter => :from_sym,
|
50
|
+
:allow_nil => true
|
29
51
|
)
|
30
52
|
|
31
53
|
if ActiveRecord::VERSION::MAJOR >= 3
|
@@ -49,13 +71,22 @@ module HasEnumeration
|
|
49
71
|
def create_enumeration_mapping_class(mapping)
|
50
72
|
inverted_mapping = mapping.invert
|
51
73
|
Class.new do
|
52
|
-
attr_reader :raw_value
|
74
|
+
attr_reader :raw_value
|
53
75
|
|
54
76
|
define_method :initialize do |raw_value|
|
55
77
|
@raw_value = raw_value
|
56
78
|
@value = inverted_mapping[raw_value]
|
57
79
|
end
|
58
80
|
|
81
|
+
define_method :to_sym do
|
82
|
+
@value
|
83
|
+
end
|
84
|
+
|
85
|
+
define_method :value do
|
86
|
+
puts "#{self.class.name}#value is deprecated. Use #to_sym instead. (#{caller[0]})"
|
87
|
+
to_sym
|
88
|
+
end
|
89
|
+
|
59
90
|
mapping.keys.each do |sym|
|
60
91
|
predicate = "#{sym}?".to_sym
|
61
92
|
value = mapping[sym]
|
@@ -66,6 +97,11 @@ module HasEnumeration
|
|
66
97
|
|
67
98
|
(class <<self;self;end).class_eval do
|
68
99
|
define_method :from_sym do |sym|
|
100
|
+
unless mapping.has_key?(sym)
|
101
|
+
raise ArgumentError.new(
|
102
|
+
"#{sym.inspect} is not one of {#{mapping.keys.map(&:inspect).sort.join(', ')}}"
|
103
|
+
)
|
104
|
+
end
|
69
105
|
new(mapping[sym])
|
70
106
|
end
|
71
107
|
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
require File.expand_path('../spec_helper', File.dirname(__FILE__))
|
2
|
+
|
3
|
+
describe HasEnumeration, 'with invalid values' do
|
4
|
+
before(:each) do
|
5
|
+
@model = ExplicitlyMappedModel.new
|
6
|
+
end
|
7
|
+
|
8
|
+
it 'raises an exception when assigned an invalid value' do
|
9
|
+
lambda do
|
10
|
+
@model.color = :beige
|
11
|
+
end.should raise_error(ArgumentError, ':beige is not one of {:blue, :green, :red}')
|
12
|
+
end
|
13
|
+
|
14
|
+
if ActiveRecord::VERSION::MAJOR >= 3
|
15
|
+
context 'with ActiveRecord 3.x' do
|
16
|
+
it 'raises an exception when finding with an invalid value' do
|
17
|
+
lambda do
|
18
|
+
ExplicitlyMappedModel.where(:color => :beige).all
|
19
|
+
end.should raise_error(ArgumentError, ':beige is not one of {:blue, :green, :red}')
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'raises an exception when finding with an invalid value via meta_where' do
|
23
|
+
lambda do
|
24
|
+
ExplicitlyMappedModel.where(:color.not_eq => :beige).all
|
25
|
+
end.should raise_error(ArgumentError, ':beige is not one of {:blue, :green, :red}')
|
26
|
+
end
|
27
|
+
end
|
28
|
+
else
|
29
|
+
context 'With ActiveRecord 2.x' do
|
30
|
+
it 'raises an exception when finding with an invalid value' do
|
31
|
+
lambda do
|
32
|
+
ExplicitlyMappedModel.find(:all, :conditions => {:color => :beige})
|
33
|
+
end.should raise_error(ArgumentError, ':beige is not one of {:blue, :green, :red}')
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
describe HasEnumeration, 'with an uninitialied value' do
|
40
|
+
context 'in a newly-created object' do
|
41
|
+
it 'returns nil for the value of the enumeration' do
|
42
|
+
ExplicitlyMappedModel.new.color.should be_nil
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
context 'in an existing object' do
|
47
|
+
it 'returns nil for the value of the enumeration' do
|
48
|
+
object = ExplicitlyMappedModel.find(ExplicitlyMappedModel.create!.id)
|
49
|
+
object.color.should be_nil
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
describe HasEnumeration, 'assignment of nil' do
|
55
|
+
it 'sets the enumeration to nil' do
|
56
|
+
object = ExplicitlyMappedModel.new(:color => :red)
|
57
|
+
object.color = nil
|
58
|
+
object.color.should be_nil
|
59
|
+
end
|
60
|
+
|
61
|
+
it 'persists across a trip to the database' do
|
62
|
+
object = ExplicitlyMappedModel.create!(:color => :red)
|
63
|
+
object.color = nil
|
64
|
+
object.save!
|
65
|
+
ExplicitlyMappedModel.find(object.id).color.should be_nil
|
66
|
+
end
|
67
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
ENV['AR_VERSION'] ||= '3.0'
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler/setup'
|
5
|
+
require 'rspec'
|
6
|
+
|
7
|
+
require File.expand_path('../lib/has_enumeration', File.dirname(__FILE__))
|
8
|
+
require File.expand_path('../features/support/explicitly_mapped_model', File.dirname(__FILE__))
|
9
|
+
|
10
|
+
ActiveRecord::Base.establish_connection(
|
11
|
+
:adapter => defined?(JRUBY_VERSION) ? 'jdbcsqlite3': 'sqlite3',
|
12
|
+
:database => File.expand_path('../database', __FILE__)
|
13
|
+
)
|
14
|
+
|
15
|
+
if ActiveRecord::VERSION::MAJOR >= 3
|
16
|
+
require 'meta_where'
|
17
|
+
end
|
18
|
+
|
19
|
+
class CreateTables < ActiveRecord::Migration
|
20
|
+
create_table :explicitly_mapped_models, :force => true do |t|
|
21
|
+
t.integer :color
|
22
|
+
end
|
23
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: has_enumeration
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 23
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
|
+
- 1
|
7
8
|
- 0
|
8
|
-
- 5
|
9
9
|
- 0
|
10
|
-
version: 0.
|
10
|
+
version: 1.0.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Greg Spurrier
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-
|
18
|
+
date: 2010-11-07 00:00:00 -07:00
|
19
19
|
default_executable:
|
20
20
|
dependencies: []
|
21
21
|
|
@@ -27,7 +27,8 @@ description: |
|
|
27
27
|
and the symbols may be used in finder methods. When using ActiveRecord 3,
|
28
28
|
the symbols may also be used when interacting with the underlying Arel attribute
|
29
29
|
for the enumeration. has_enumeration has been tested with Ruby 1.8.7,
|
30
|
-
Ruby 1.9.2, JRuby 1.5.3, ActiveRecord 2.3.
|
30
|
+
Ruby 1.9.2, JRuby 1.5.3, Rubinius 1.1.0, ActiveRecord 2.3.10, and ActiveRecord
|
31
|
+
3.0.1.
|
31
32
|
|
32
33
|
email: greg@rujubu.com
|
33
34
|
executables: []
|
@@ -36,16 +37,14 @@ extensions: []
|
|
36
37
|
|
37
38
|
extra_rdoc_files:
|
38
39
|
- LICENSE.txt
|
39
|
-
- README.
|
40
|
-
- TODO
|
40
|
+
- README.md
|
41
41
|
files:
|
42
42
|
- Gemfile
|
43
43
|
- Gemfile.lock
|
44
44
|
- HISTORY.txt
|
45
45
|
- LICENSE.txt
|
46
|
-
- README.
|
46
|
+
- README.md
|
47
47
|
- Rakefile
|
48
|
-
- TODO
|
49
48
|
- VERSION
|
50
49
|
- features/arel_attributes.feature
|
51
50
|
- features/explicitly_mapped_enumeration.feature
|
@@ -61,6 +60,8 @@ files:
|
|
61
60
|
- lib/has_enumeration/aggregate_conditions_override.rb
|
62
61
|
- lib/has_enumeration/arel/table_extensions.rb
|
63
62
|
- lib/has_enumeration/class_methods.rb
|
63
|
+
- spec/has_enumeration/has_enumeration_spec.rb
|
64
|
+
- spec/spec_helper.rb
|
64
65
|
has_rdoc: true
|
65
66
|
homepage: http://github.com/gregspurrier/has_enumeration
|
66
67
|
licenses: []
|
@@ -95,5 +96,6 @@ rubygems_version: 1.3.7
|
|
95
96
|
signing_key:
|
96
97
|
specification_version: 3
|
97
98
|
summary: Support for symbol-based enumerations in ActiveRecord
|
98
|
-
test_files:
|
99
|
-
|
99
|
+
test_files:
|
100
|
+
- spec/has_enumeration/has_enumeration_spec.rb
|
101
|
+
- spec/spec_helper.rb
|
data/README.txt
DELETED
@@ -1,53 +0,0 @@
|
|
1
|
-
== Description
|
2
|
-
This ActiveRecord plugin enables attributes to be declared as enumerations
|
3
|
-
of symbols.
|
4
|
-
|
5
|
-
== Example
|
6
|
-
class TestObject < ActiveRecord::Base
|
7
|
-
# Integer attribute with explicit value mapping
|
8
|
-
has_enumeration :color, :red => 1, :green => 2, :blue => 3
|
9
|
-
|
10
|
-
# String attribute with implicit value mapping
|
11
|
-
# has_enumeration :color, [:red, :green, :blue]
|
12
|
-
|
13
|
-
# Attribute with a name other than the enumeration's name
|
14
|
-
# has_enumeration :color, {:red => 1, :green => 2, :blue => 3},
|
15
|
-
# :attribute => :hue
|
16
|
-
end
|
17
|
-
|
18
|
-
# Value and predicates
|
19
|
-
obj = TestObject.new
|
20
|
-
obj.color.value # => nil
|
21
|
-
obj.color = :red # => :red
|
22
|
-
obj.color.value # => :red
|
23
|
-
obj.color.red? # => true
|
24
|
-
obj.color.green? # => false
|
25
|
-
|
26
|
-
# Querying
|
27
|
-
TestObject.where(:color => :red)
|
28
|
-
|
29
|
-
# Querying with arel predicates (ActiveRecord 3 only)
|
30
|
-
attr = TestObject.arel_table[:color]
|
31
|
-
TestObject.where(attr.not_in([:red, :green]))
|
32
|
-
|
33
|
-
# Querying with MetaWhere (ActiveRecord 3 only, with MetaWhere gem)
|
34
|
-
TestObject.where(:color.not_in => [:red, :green])
|
35
|
-
|
36
|
-
== Installation
|
37
|
-
gem install has_enumeration
|
38
|
-
|
39
|
-
== Getting the Latest Version
|
40
|
-
The repository for has_enumeration is hosted at GitHub:
|
41
|
-
Web page: http://github.com/gregspurrier/has_enumeration
|
42
|
-
Clone URL: git://github.com/gregspurrier/has_enumeration.git
|
43
|
-
|
44
|
-
== Supported Versions
|
45
|
-
has_enumeration has been tested with all combinations for the following:
|
46
|
-
|
47
|
-
Rubies:
|
48
|
-
- 1.8.7-p302
|
49
|
-
- 1.9.2-p0
|
50
|
-
- JRuby 1.5.3
|
51
|
-
ActiveRecord:
|
52
|
-
- 2.3.9
|
53
|
-
- 3.0.0
|