enumerate_it 0.1.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 +21 -0
- data/LICENSE +20 -0
- data/README.rdoc +143 -0
- data/Rakefile +45 -0
- data/VERSION +1 -0
- data/lib/enumerate_it.rb +185 -0
- data/spec/enumerate_it_spec.rb +79 -0
- data/spec/renun_spec.rb +79 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +9 -0
- metadata +88 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Cássio Marques
|
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.rdoc
ADDED
@@ -0,0 +1,143 @@
|
|
1
|
+
= EnumerateIt - Ruby Enumerations
|
2
|
+
|
3
|
+
Author: Cássio Marques - cassiommc at gmail
|
4
|
+
|
5
|
+
== Description
|
6
|
+
|
7
|
+
Ok, I know there are a lot of different solutions to this problem. But none of them solved my problem,
|
8
|
+
so here's EnumerateIt. I needed to build a Rails application around a legacy database and this database was
|
9
|
+
filled with those small, unchangeable tables used to create foreign key constraints everywhere.
|
10
|
+
|
11
|
+
=== For example:
|
12
|
+
|
13
|
+
Table "public.relationshipstatus"
|
14
|
+
Column | Type | Modifiers
|
15
|
+
-------------+---------------+-----------
|
16
|
+
code | character(1) | not null
|
17
|
+
description | character(11) |
|
18
|
+
Indexes:
|
19
|
+
"relationshipstatus_pkey" PRIMARY KEY, btree (code)
|
20
|
+
|
21
|
+
select * from relationshipstatus;
|
22
|
+
code | description
|
23
|
+
-------+--------------
|
24
|
+
1 | Single
|
25
|
+
2 | Married
|
26
|
+
3 | Widow
|
27
|
+
4 | Divorced
|
28
|
+
|
29
|
+
|
30
|
+
And then I had things like a people table with a 'relationship_status' column with a foreign key
|
31
|
+
pointing to the relationshipstatus table.
|
32
|
+
|
33
|
+
While this is a good thing from the database normalization perspective, managing this values in
|
34
|
+
my tests was very hard. Doing database joins just to get the description of some value was absurd.
|
35
|
+
And, more than this, referencing them in my code using magic numbers was terrible and meaningless:
|
36
|
+
What does it mean when we say that someone or something is '2'?
|
37
|
+
|
38
|
+
Enter EnumerateIt.
|
39
|
+
|
40
|
+
== Creating enumerations
|
41
|
+
|
42
|
+
Enumerations are created as models, but you can put then anywhere in your application. In Rails
|
43
|
+
applications, I put them inside models/.
|
44
|
+
|
45
|
+
class RelationshipStatus < EnumerateIt::Base
|
46
|
+
associate_values(
|
47
|
+
:single => [1, 'Single'],
|
48
|
+
:married => [2, 'Married'],
|
49
|
+
:widow => [3, 'Widow'],
|
50
|
+
:divorced => [4, 'Divorced'],
|
51
|
+
)
|
52
|
+
end
|
53
|
+
|
54
|
+
This will create some nice stuff:
|
55
|
+
|
56
|
+
* Each enumeration's value will turn into a constant:
|
57
|
+
|
58
|
+
RelationshipsStatus::SINGLE # returns 1
|
59
|
+
RelationshipStatus::MARRIED # returns 2 and so on...
|
60
|
+
|
61
|
+
* You can retrieve a list with all the enumeration codes:
|
62
|
+
|
63
|
+
RelationshipStatus.list # [1,2,3,4]
|
64
|
+
|
65
|
+
* You can get an array of options, ready to use with the 'select', 'select_tag', etc family of Rails helpers.
|
66
|
+
|
67
|
+
RelationshipStatus.to_a # [["Divorced", 4],["Married", 2],["Single", 1],["Widow", 3]]
|
68
|
+
|
69
|
+
* You can manipulate the has used to create the enumeration:
|
70
|
+
|
71
|
+
RelationshipStatus.enumeration # returns the exact hash used to define the enumeration
|
72
|
+
|
73
|
+
== Using enumerations
|
74
|
+
|
75
|
+
The cool part is that you can use these enumerations with any class, be it an ActiveRecord instance
|
76
|
+
or not.
|
77
|
+
|
78
|
+
class Person
|
79
|
+
include EnumerateIt
|
80
|
+
attr_accessor :relationship_status
|
81
|
+
|
82
|
+
has_enumeration_for :relationship_status, :with => RelationshipStatus
|
83
|
+
end
|
84
|
+
|
85
|
+
This will create:
|
86
|
+
|
87
|
+
* A humanized description for the values of the enumerated attribute:
|
88
|
+
|
89
|
+
p = Person.new
|
90
|
+
p.relationship_status = RelationshipStatus::DIVORCED
|
91
|
+
p.relationsip_status_humanize # => 'Divorced'
|
92
|
+
|
93
|
+
* If your class can manage validations and responds to :validates_inclusion_of, it will create this
|
94
|
+
validation:
|
95
|
+
|
96
|
+
class Person < ActiveRecord::Base
|
97
|
+
has_enumeration_for :relationship_status, :with => RelationshipStatus
|
98
|
+
end
|
99
|
+
|
100
|
+
p = Person.new :relationship_status => 6 # => there is no '6' value in the enumeration
|
101
|
+
p.valid? # => false
|
102
|
+
p.errors[:relationship_status] # => "is not included in the list"
|
103
|
+
|
104
|
+
Remember that in Rails 3 you can add validations to any kind of class and not only to those derived from
|
105
|
+
ActiveRecord::Base.
|
106
|
+
|
107
|
+
== Installation
|
108
|
+
|
109
|
+
gem install enumerate_it
|
110
|
+
|
111
|
+
== Using with Rails
|
112
|
+
|
113
|
+
* Create an initializer with the following code:
|
114
|
+
|
115
|
+
ActiveRecord::Base.send :include, EnumerateIt
|
116
|
+
|
117
|
+
* Add the 'enumerate_it' gem as a dependency in your environment.rb (Rails 2.3.x) or Gemfile (if you're using Bundler)
|
118
|
+
|
119
|
+
== Why did you reinvent the wheel?
|
120
|
+
|
121
|
+
There are other similar solutions to the problem out there, but I could not find one that
|
122
|
+
worked both with strings and integers as the enumerations' codes. I had both situations in
|
123
|
+
my legacy database.
|
124
|
+
|
125
|
+
== Why defining enumerations outside the class that use it?
|
126
|
+
|
127
|
+
* I think it's cleaner.
|
128
|
+
* You can add behaviour to the enumeration class.
|
129
|
+
* You can reuse the enumeration inside other classes.
|
130
|
+
|
131
|
+
== Note on Patches/Pull Requests
|
132
|
+
|
133
|
+
* Fork the project.
|
134
|
+
* Make your feature addition or bug fix.
|
135
|
+
* Add tests for it. This is important so I don't break it in a
|
136
|
+
future version unintentionally.
|
137
|
+
* Commit, do not mess with rakefile, version, or history.
|
138
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
139
|
+
* Send me a pull request. Bonus points for topic branches.
|
140
|
+
|
141
|
+
== Copyright
|
142
|
+
|
143
|
+
Copyright (c) 2010 Cássio Marques. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "enumerate_it"
|
8
|
+
gem.summary = %Q{Ruby Enumerations}
|
9
|
+
gem.description = %Q{Have a legacy database and need some enumerations in your models to match those stupid '4 rows/2 columns' tables with foreign keys and stop doing joins just to fetch a simple description? Or maybe use some integers instead of strings as the code for each value of your enumerations? Here's EnumerateIt.}
|
10
|
+
gem.email = "cassiommc@gmail.com"
|
11
|
+
gem.homepage = "http://github.com/cassiomarques/enumerate_it"
|
12
|
+
gem.authors = ["Cássio Marques"]
|
13
|
+
gem.add_development_dependency "rspec", ">= 1.2.9"
|
14
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
15
|
+
end
|
16
|
+
Jeweler::GemcutterTasks.new
|
17
|
+
rescue LoadError
|
18
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
19
|
+
end
|
20
|
+
|
21
|
+
require 'spec/rake/spectask'
|
22
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
23
|
+
spec.libs << 'lib' << 'spec'
|
24
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
25
|
+
end
|
26
|
+
|
27
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
28
|
+
spec.libs << 'lib' << 'spec'
|
29
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
30
|
+
spec.rcov = true
|
31
|
+
end
|
32
|
+
|
33
|
+
task :spec => :check_dependencies
|
34
|
+
|
35
|
+
task :default => :spec
|
36
|
+
|
37
|
+
require 'rake/rdoctask'
|
38
|
+
Rake::RDocTask.new do |rdoc|
|
39
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
40
|
+
|
41
|
+
rdoc.rdoc_dir = 'rdoc'
|
42
|
+
rdoc.title = "enumerate_it #{version}"
|
43
|
+
rdoc.rdoc_files.include('README*')
|
44
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
45
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
data/lib/enumerate_it.rb
ADDED
@@ -0,0 +1,185 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
# EnumerateIt - Ruby Enumerations
|
4
|
+
#
|
5
|
+
# Author: Cássio Marques - cassiommc at gmail
|
6
|
+
#
|
7
|
+
# = Description
|
8
|
+
#
|
9
|
+
# Ok, I know there are a lot of different solutions to this problem. But none of them solved my problem,
|
10
|
+
# so here's EnumerateIt. I needed to build a Rails application around a legacy database and this database was
|
11
|
+
# filled with those small, unchangeable tables used to create foreign key constraints everywhere.
|
12
|
+
#
|
13
|
+
# == For example:
|
14
|
+
#
|
15
|
+
# Table "public.relationshipstatus"
|
16
|
+
# Column | Type | Modifiers
|
17
|
+
# -------------+---------------+-----------
|
18
|
+
# code | character(1) | not null
|
19
|
+
# description | character(11) |
|
20
|
+
# Indexes:
|
21
|
+
# "relationshipstatus_pkey" PRIMARY KEY, btree (code)
|
22
|
+
#
|
23
|
+
# select * from relationshipstatus;
|
24
|
+
# code | description
|
25
|
+
# --------+--------------
|
26
|
+
# 1 | Single
|
27
|
+
# 2 | Married
|
28
|
+
# 3 | Widow
|
29
|
+
# 4 | Divorced
|
30
|
+
#
|
31
|
+
# And then I had things like a people table with a 'relationship_status' column with a foreign key
|
32
|
+
# pointing to the relationshipstatus table.
|
33
|
+
#
|
34
|
+
# While this is a good thing from the database normalization perspective, managing this values in
|
35
|
+
# my tests was very hard. More than this, referencing them in my code using magic numbers was terrible
|
36
|
+
# and meaningless: What's does it mean when we say that someone or something is '2'?
|
37
|
+
#
|
38
|
+
# Enter EnumerateIt.
|
39
|
+
#
|
40
|
+
# = Creating enumerations
|
41
|
+
#
|
42
|
+
# Enumerations are created as models, but you can put then anywhere in your application. In Rails
|
43
|
+
# applications, I put them inside models/.
|
44
|
+
#
|
45
|
+
# class RelationshipStatus < EnumerateIt::Base
|
46
|
+
# associate_values(
|
47
|
+
# :single => [1, 'Single'],
|
48
|
+
# :married => [2, 'Married'],
|
49
|
+
# :widow => [3, 'Widow'],
|
50
|
+
# :divorced => [4, 'Divorced'],
|
51
|
+
# )
|
52
|
+
# end
|
53
|
+
#
|
54
|
+
# This will create some nice stuff:
|
55
|
+
#
|
56
|
+
# - Each enumeration's value will turn into a constant:
|
57
|
+
#
|
58
|
+
# RelationshipsStatus::SINGLE # returns 1
|
59
|
+
# RelationshipStatus::MARRIED # returns 2 and so on...
|
60
|
+
#
|
61
|
+
# - You can retrieve a list with all the enumeration codes:
|
62
|
+
#
|
63
|
+
# RelationshipStatus.list # [1,2,3,4]
|
64
|
+
#
|
65
|
+
# You can get an array of options, ready to use with the 'select', 'select_tag', etc family of Rails helpers.
|
66
|
+
#
|
67
|
+
# RelationshipStatus.to_a # [["Divorced", 4],["Married", 2],["Single", 1],["Widow", 3]]
|
68
|
+
#
|
69
|
+
# - You can manipulate the has used to create the enumeration:
|
70
|
+
#
|
71
|
+
# RelationshipStatus.enumeration # returns the exact hash used to define the enumeration
|
72
|
+
#
|
73
|
+
# = Using enumerations
|
74
|
+
#
|
75
|
+
# The cool part is that you can use these enumerations with any class, be it an ActiveRecord instance
|
76
|
+
# or not.
|
77
|
+
#
|
78
|
+
# class Person
|
79
|
+
# include EnumerateIt
|
80
|
+
# attr_accessor :relationship_status
|
81
|
+
#
|
82
|
+
# has_enumeration_for :relationship_status, :with => RelationshipStatus
|
83
|
+
# end
|
84
|
+
#
|
85
|
+
# This will create:
|
86
|
+
#
|
87
|
+
# - A humanized description for the values of the enumerated attribute:
|
88
|
+
#
|
89
|
+
# p = Person.new
|
90
|
+
# p.relationship_status = RelationshipStatus::DIVORCED
|
91
|
+
# p.relationsip_status_humanize # => 'Divorced'
|
92
|
+
#
|
93
|
+
# - If your class can manage validations and responds to :validates_inclusion_of, it will create this
|
94
|
+
# validation:
|
95
|
+
#
|
96
|
+
# class Person < ActiveRecord::Base
|
97
|
+
# has_enumeration_for :relationship_status, :with => RelationshipStatus
|
98
|
+
# end
|
99
|
+
#
|
100
|
+
# p = Person.new :relationship_status => 6 # => there is no '6' value in the enumeration
|
101
|
+
# p.valid? # => false
|
102
|
+
# p.errors[:relationship_status] # => "is not included in the list"
|
103
|
+
#
|
104
|
+
# Remember that in Rails 3 you can add validations to any kind of class and not only to those derived from
|
105
|
+
# ActiveRecord::Base.
|
106
|
+
#
|
107
|
+
# = Using with Rails/ActiveRecord
|
108
|
+
#
|
109
|
+
# * Create an initializer with the following code:
|
110
|
+
#
|
111
|
+
# ActiveRecord::Base.send :include, EnumerateIt
|
112
|
+
#
|
113
|
+
# * Add the 'enumerate_it' gem as a dependency in your environment.rb (Rails 2.3.x) or Gemfile (if you're using Bundler)
|
114
|
+
#
|
115
|
+
# = Why did you reinvent the wheel?
|
116
|
+
#
|
117
|
+
# There are other similar solutions to the problem out there, but I could not find one that
|
118
|
+
# worked both with strings and integers as the enumerations' codes. I had both situations in
|
119
|
+
# my legacy database.
|
120
|
+
#
|
121
|
+
# = Why defining enumerations outside the class that used it?
|
122
|
+
#
|
123
|
+
# - I think it's cleaner.
|
124
|
+
# - You can add behaviour to the enumeration class.
|
125
|
+
# - You can reuse the enumeration inside other classes.
|
126
|
+
#
|
127
|
+
module EnumerateIt
|
128
|
+
class Base
|
129
|
+
@@registered_enumerations = {}
|
130
|
+
|
131
|
+
def self.associate_values(values_hash)
|
132
|
+
register_enumeration values_hash
|
133
|
+
values_hash.each_pair { |value_name, attributes| define_enumeration_constant value_name, attributes[0] }
|
134
|
+
define_enumeration_list values_hash
|
135
|
+
end
|
136
|
+
|
137
|
+
private
|
138
|
+
def self.register_enumeration(values_hash)
|
139
|
+
@@registered_enumerations[self] = values_hash
|
140
|
+
end
|
141
|
+
|
142
|
+
def self.define_enumeration_constant(name, value)
|
143
|
+
const_set name.to_s.upcase, value
|
144
|
+
end
|
145
|
+
|
146
|
+
def self.define_enumeration_list(values_hash)
|
147
|
+
def self.list
|
148
|
+
@@registered_enumerations[self].values.map { |value| value[0] }.sort
|
149
|
+
end
|
150
|
+
|
151
|
+
def self.enumeration
|
152
|
+
@@registered_enumerations[self]
|
153
|
+
end
|
154
|
+
|
155
|
+
def self.to_a
|
156
|
+
@@registered_enumerations[self].values.map {|value| value.reverse }.sort_by { |value| value[0] }
|
157
|
+
end
|
158
|
+
end
|
159
|
+
end
|
160
|
+
|
161
|
+
module ClassMethods
|
162
|
+
def has_enumeration_for(attribute, options)
|
163
|
+
if self.respond_to? :validates_inclusion_of
|
164
|
+
validates_inclusion_of attribute, :in => options[:with].list, :allow_blank => true
|
165
|
+
end
|
166
|
+
create_enumeration_humanize_method options[:with], attribute
|
167
|
+
end
|
168
|
+
|
169
|
+
private
|
170
|
+
def create_enumeration_humanize_method(klass, attribute_name)
|
171
|
+
class_eval do
|
172
|
+
define_method "#{attribute_name}_humanize" do
|
173
|
+
values = klass.enumeration.values.detect { |v| v[0] == self.send(attribute_name) }
|
174
|
+
values ? values[1] : nil
|
175
|
+
end
|
176
|
+
end
|
177
|
+
end
|
178
|
+
end
|
179
|
+
|
180
|
+
def self.included(receiver)
|
181
|
+
receiver.extend ClassMethods
|
182
|
+
end
|
183
|
+
end
|
184
|
+
|
185
|
+
|
@@ -0,0 +1,79 @@
|
|
1
|
+
#encoding: utf-8
|
2
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
3
|
+
|
4
|
+
class TestEnumeration < EnumerateIt::Base
|
5
|
+
associate_values(
|
6
|
+
:value_1 => ['1', 'Hey, I am 1!'],
|
7
|
+
:value_2 => ['2', 'Hey, I am 2!'],
|
8
|
+
:value_3 => ['3', 'Hey, I am 3!']
|
9
|
+
)
|
10
|
+
end
|
11
|
+
|
12
|
+
describe EnumerateIt do
|
13
|
+
before :each do
|
14
|
+
class TestClass
|
15
|
+
include EnumerateIt
|
16
|
+
attr_accessor :foobar
|
17
|
+
has_enumeration_for :foobar, :with => TestEnumeration
|
18
|
+
|
19
|
+
def initialize(foobar)
|
20
|
+
@foobar = foobar
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
@target = TestClass.new(TestEnumeration::VALUE_2)
|
25
|
+
end
|
26
|
+
|
27
|
+
context "associating an enumeration with a class attribute" do
|
28
|
+
it "creates an humanized description for the attribute's value" do
|
29
|
+
@target.foobar_humanize.should == 'Hey, I am 2!'
|
30
|
+
end
|
31
|
+
|
32
|
+
it "if the attribute is blank, the humanize description is nil" do
|
33
|
+
@target.foobar = nil
|
34
|
+
@target.foobar_humanize.should be_nil
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
describe EnumerateIt::Base do
|
39
|
+
it "creates constants for each enumeration value" do
|
40
|
+
[TestEnumeration::VALUE_1, TestEnumeration::VALUE_2, TestEnumeration::VALUE_3].each_with_index do |constant, idx|
|
41
|
+
constant.should == (idx + 1).to_s
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
it "creates a method that returns the allowed values in the enumeration's class" do
|
46
|
+
TestEnumeration.list.should == ['1', '2', '3']
|
47
|
+
end
|
48
|
+
|
49
|
+
it "creates a method that returns the enumeration specification" do
|
50
|
+
TestEnumeration.enumeration.should == {
|
51
|
+
:value_1 => ['1', 'Hey, I am 1!'],
|
52
|
+
:value_2 => ['2', 'Hey, I am 2!'],
|
53
|
+
:value_3 => ['3', 'Hey, I am 3!']
|
54
|
+
}
|
55
|
+
end
|
56
|
+
|
57
|
+
describe ".to_a" do
|
58
|
+
it "returns an array with the values and human representations" do
|
59
|
+
TestEnumeration.to_a.should == [['Hey, I am 1!', '1'], ['Hey, I am 2!', '2'], ['Hey, I am 3!', '3']]
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
context "when included in ActiveRecord::Base" do
|
64
|
+
before :each do
|
65
|
+
class ActiveRecordStub; attr_accessor :bla; end
|
66
|
+
ActiveRecordStub.stub!(:respond_to?).with(:validates_inclusion_of).and_return(true)
|
67
|
+
ActiveRecordStub.stub!(:validates_inclusion_of).and_return(true)
|
68
|
+
ActiveRecordStub.send :include, EnumerateIt
|
69
|
+
end
|
70
|
+
|
71
|
+
it "creates a validation for inclusion" do
|
72
|
+
ActiveRecordStub.should_receive(:validates_inclusion_of).with(:bla, :in => TestEnumeration.list, :allow_blank => true)
|
73
|
+
class ActiveRecordStub
|
74
|
+
has_enumeration_for :bla, :with => TestEnumeration
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
data/spec/renun_spec.rb
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
#encoding: utf-8
|
2
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
3
|
+
|
4
|
+
class TestEnumeration < EnumerateIt::Base
|
5
|
+
associate_values(
|
6
|
+
:value_1 => ['1', 'Hey, I am 1!'],
|
7
|
+
:value_2 => ['2', 'Hey, I am 2!'],
|
8
|
+
:value_3 => ['3', 'Hey, I am 3!']
|
9
|
+
)
|
10
|
+
end
|
11
|
+
|
12
|
+
describe EnumerateIt do
|
13
|
+
before :each do
|
14
|
+
class TestClass
|
15
|
+
include EnumerateIt
|
16
|
+
attr_accessor :foobar
|
17
|
+
has_enumeration_for :foobar, :with => TestEnumeration
|
18
|
+
|
19
|
+
def initialize(foobar)
|
20
|
+
@foobar = foobar
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
@target = TestClass.new(TestEnumeration::VALUE_2)
|
25
|
+
end
|
26
|
+
|
27
|
+
context "associating an enumeration with a class attribute" do
|
28
|
+
it "creates an humanized description for the attribute's value" do
|
29
|
+
@target.foobar_humanize.should == 'Hey, I am 2!'
|
30
|
+
end
|
31
|
+
|
32
|
+
it "if the attribute is blank, the humanize description is nil" do
|
33
|
+
@target.foobar = nil
|
34
|
+
@target.foobar_humanize.should be_nil
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
describe EnumerateIt::Base do
|
39
|
+
it "creates constants for each enumeration value" do
|
40
|
+
[TestEnumeration::VALUE_1, TestEnumeration::VALUE_2, TestEnumeration::VALUE_3].each_with_index do |constant, idx|
|
41
|
+
constant.should == (idx + 1).to_s
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
it "creates a method that returns the allowed values in the enumeration's class" do
|
46
|
+
TestEnumeration.list.should == ['1', '2', '3']
|
47
|
+
end
|
48
|
+
|
49
|
+
it "creates a method that returns the enumeration specification" do
|
50
|
+
TestEnumeration.enumeration.should == {
|
51
|
+
:value_1 => ['1', 'Hey, I am 1!'],
|
52
|
+
:value_2 => ['2', 'Hey, I am 2!'],
|
53
|
+
:value_3 => ['3', 'Hey, I am 3!']
|
54
|
+
}
|
55
|
+
end
|
56
|
+
|
57
|
+
describe ".to_a" do
|
58
|
+
it "returns an array with the values and human representations" do
|
59
|
+
TestEnumeration.to_a.should == [['Hey, I am 1!', '1'], ['Hey, I am 2!', '2'], ['Hey, I am 3!', '3']]
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
context "when included in ActiveRecord::Base" do
|
64
|
+
before :each do
|
65
|
+
class ActiveRecordStub; attr_accessor :bla; end
|
66
|
+
ActiveRecordStub.stub!(:respond_to?).with(:validates_inclusion_of).and_return(true)
|
67
|
+
ActiveRecordStub.stub!(:validates_inclusion_of).and_return(true)
|
68
|
+
ActiveRecordStub.send :include, EnumerateIt
|
69
|
+
end
|
70
|
+
|
71
|
+
it "creates a validation for inclusion" do
|
72
|
+
ActiveRecordStub.should_receive(:validates_inclusion_of).with(:bla, :in => TestEnumeration.list, :allow_blank => true)
|
73
|
+
class ActiveRecordStub
|
74
|
+
has_enumeration_for :bla, :with => TestEnumeration
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
data/spec/spec.opts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: enumerate_it
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: 0.1.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- "C\xC3\xA1ssio Marques"
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-03-16 00:00:00 -03:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: rspec
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 1
|
29
|
+
- 2
|
30
|
+
- 9
|
31
|
+
version: 1.2.9
|
32
|
+
type: :development
|
33
|
+
version_requirements: *id001
|
34
|
+
description: Have a legacy database and need some enumerations in your models to match those stupid '4 rows/2 columns' tables with foreign keys and stop doing joins just to fetch a simple description? Or maybe use some integers instead of strings as the code for each value of your enumerations? Here's EnumerateIt.
|
35
|
+
email: cassiommc@gmail.com
|
36
|
+
executables: []
|
37
|
+
|
38
|
+
extensions: []
|
39
|
+
|
40
|
+
extra_rdoc_files:
|
41
|
+
- LICENSE
|
42
|
+
- README.rdoc
|
43
|
+
files:
|
44
|
+
- .document
|
45
|
+
- .gitignore
|
46
|
+
- LICENSE
|
47
|
+
- README.rdoc
|
48
|
+
- Rakefile
|
49
|
+
- VERSION
|
50
|
+
- lib/enumerate_it.rb
|
51
|
+
- spec/enumerate_it_spec.rb
|
52
|
+
- spec/renun_spec.rb
|
53
|
+
- spec/spec.opts
|
54
|
+
- spec/spec_helper.rb
|
55
|
+
has_rdoc: true
|
56
|
+
homepage: http://github.com/cassiomarques/enumerate_it
|
57
|
+
licenses: []
|
58
|
+
|
59
|
+
post_install_message:
|
60
|
+
rdoc_options:
|
61
|
+
- --charset=UTF-8
|
62
|
+
require_paths:
|
63
|
+
- lib
|
64
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
segments:
|
69
|
+
- 0
|
70
|
+
version: "0"
|
71
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
segments:
|
76
|
+
- 0
|
77
|
+
version: "0"
|
78
|
+
requirements: []
|
79
|
+
|
80
|
+
rubyforge_project:
|
81
|
+
rubygems_version: 1.3.6
|
82
|
+
signing_key:
|
83
|
+
specification_version: 3
|
84
|
+
summary: Ruby Enumerations
|
85
|
+
test_files:
|
86
|
+
- spec/enumerate_it_spec.rb
|
87
|
+
- spec/renun_spec.rb
|
88
|
+
- spec/spec_helper.rb
|