enumerate_it 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +51 -34
- data/Rakefile +2 -0
- data/VERSION +1 -1
- data/enumerate_it.gemspec +3 -3
- data/lib/enumerate_it.rb +26 -3
- data/spec/enumerate_it_spec.rb +26 -3
- metadata +3 -3
data/README.rdoc
CHANGED
@@ -42,74 +42,87 @@ Enter EnumerateIt.
|
|
42
42
|
Enumerations are created as models, but you can put then anywhere in your application. In Rails
|
43
43
|
applications, I put them inside models/.
|
44
44
|
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
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
53
|
|
54
54
|
This will create some nice stuff:
|
55
55
|
|
56
56
|
* Each enumeration's value will turn into a constant:
|
57
57
|
|
58
|
-
|
59
|
-
|
58
|
+
RelationshipsStatus::SINGLE # returns 1
|
59
|
+
RelationshipStatus::MARRIED # returns 2 and so on...
|
60
60
|
|
61
61
|
* You can retrieve a list with all the enumeration codes:
|
62
62
|
|
63
|
-
|
63
|
+
RelationshipStatus.list # [1,2,3,4]
|
64
64
|
|
65
65
|
* You can get an array of options, ready to use with the 'select', 'select_tag', etc family of Rails helpers.
|
66
66
|
|
67
|
-
|
67
|
+
RelationshipStatus.to_a # [["Divorced", 4],["Married", 2],["Single", 1],["Widow", 3]]
|
68
68
|
|
69
69
|
* You can manipulate the hash used to create the enumeration:
|
70
70
|
|
71
|
-
|
71
|
+
RelationshipStatus.enumeration # returns the exact hash used to define the enumeration
|
72
72
|
|
73
73
|
== Using enumerations
|
74
74
|
|
75
75
|
The cool part is that you can use these enumerations with any class, be it an ActiveRecord instance
|
76
76
|
or not.
|
77
77
|
|
78
|
-
|
79
|
-
|
80
|
-
|
78
|
+
class Person
|
79
|
+
include EnumerateIt
|
80
|
+
attr_accessor :relationship_status
|
81
81
|
|
82
|
-
|
83
|
-
|
82
|
+
has_enumeration_for :relationship_status, :with => RelationshipStatus
|
83
|
+
end
|
84
84
|
|
85
85
|
This will create:
|
86
86
|
|
87
87
|
* A humanized description for the values of the enumerated attribute:
|
88
88
|
|
89
|
-
|
90
|
-
|
91
|
-
|
89
|
+
p = Person.new
|
90
|
+
p.relationship_status = RelationshipStatus::DIVORCED
|
91
|
+
p.relationsip_status_humanize # => 'Divorced'
|
92
|
+
|
93
|
+
* If you don't supply a humanized string to represent an option, EnumerateIt will use a 'humanized' version of the hash's key to humanize the attribute's value:
|
94
|
+
|
95
|
+
class RelationshipStatus < EnumerateIt::Base
|
96
|
+
associate_values(
|
97
|
+
:married => 1,
|
98
|
+
:single => 2
|
99
|
+
)
|
100
|
+
end
|
101
|
+
|
102
|
+
p = Person.new
|
103
|
+
p.relationship_status = RelationshipStatus::MARRIED
|
104
|
+
p.relationship_status_humanize # => 'Married'
|
92
105
|
|
93
106
|
* If you pass the :create_helpers option as 'true', it will create a helper method for each enumeration option (this option defaults to false):
|
94
107
|
|
95
|
-
|
96
|
-
|
97
|
-
|
108
|
+
class Person < ActiveRecord::Base
|
109
|
+
has_enumeration_for :relationship_status, :with => RelationshipStatus, :create_helpers => true
|
110
|
+
end
|
98
111
|
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
112
|
+
p = Person.new
|
113
|
+
p.relationship_status = RelationshipStatus::MARRIED
|
114
|
+
p.married? #=> true
|
115
|
+
p.divorced? #=> false
|
103
116
|
|
104
117
|
* If your class can manage validations and responds to :validates_inclusion_of, it will create this validation:
|
105
118
|
|
106
|
-
|
107
|
-
|
108
|
-
|
119
|
+
class Person < ActiveRecord::Base
|
120
|
+
has_enumeration_for :relationship_status, :with => RelationshipStatus
|
121
|
+
end
|
109
122
|
|
110
|
-
|
111
|
-
|
112
|
-
|
123
|
+
p = Person.new :relationship_status => 6 # => there is no '6' value in the enumeration
|
124
|
+
p.valid? # => false
|
125
|
+
p.errors[:relationship_status] # => "is not included in the list"
|
113
126
|
|
114
127
|
Remember that in Rails 3 you can add validations to any kind of class and not only to those derived from
|
115
128
|
ActiveRecord::Base.
|
@@ -126,6 +139,10 @@ ActiveRecord::Base.
|
|
126
139
|
|
127
140
|
* Add the 'enumerate_it' gem as a dependency in your environment.rb (Rails 2.3.x) or Gemfile (if you're using Bundler)
|
128
141
|
|
142
|
+
== Ruby 1.9
|
143
|
+
|
144
|
+
EnumerateIt is fully compatible with Ruby 1.9.1 (all tests pass)
|
145
|
+
|
129
146
|
== Why did you reinvent the wheel?
|
130
147
|
|
131
148
|
There are other similar solutions to the problem out there, but I could not find one that
|
data/Rakefile
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.3.0
|
data/enumerate_it.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{enumerate_it}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.3.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
-
s.authors = ["
|
12
|
-
s.date = %q{2010-03-
|
11
|
+
s.authors = ["Cássio Marques"]
|
12
|
+
s.date = %q{2010-03-22}
|
13
13
|
s.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.}
|
14
14
|
s.email = %q{cassiommc@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
data/lib/enumerate_it.rb
CHANGED
@@ -88,7 +88,21 @@
|
|
88
88
|
#
|
89
89
|
# p = Person.new
|
90
90
|
# p.relationship_status = RelationshipStatus::DIVORCED
|
91
|
-
# p.
|
91
|
+
# p.relationship_status_humanize # => 'Divorced'
|
92
|
+
#
|
93
|
+
# - If you don't supply a humanized string to represent an option, EnumerateIt will use a 'humanized'
|
94
|
+
# version of the hash's key to humanize the attribute's value
|
95
|
+
#
|
96
|
+
# class RelationshipStatus < EnumerateIt::Base
|
97
|
+
# associate_values(
|
98
|
+
# :married => 1,
|
99
|
+
# :single => 2
|
100
|
+
# )
|
101
|
+
# end
|
102
|
+
#
|
103
|
+
# p = Person.new
|
104
|
+
# p.relationship_status = RelationshipStatus::MARRIED
|
105
|
+
# p.relationship_status_humanize # => 'Married'
|
92
106
|
#
|
93
107
|
# - If you pass the :create_helpers option as 'true', it will create a helper method for each enumeration
|
94
108
|
# option (this option defaults to false):
|
@@ -101,7 +115,7 @@
|
|
101
115
|
# p.relationship_status = RelationshipStatus::MARRIED
|
102
116
|
# p.married? #=> true
|
103
117
|
# p.divorced? #=> false
|
104
|
-
#
|
118
|
+
#
|
105
119
|
# - If your class can manage validations and responds to :validates_inclusion_of, it will create this
|
106
120
|
# validation:
|
107
121
|
#
|
@@ -141,12 +155,21 @@ module EnumerateIt
|
|
141
155
|
@@registered_enumerations = {}
|
142
156
|
|
143
157
|
def self.associate_values(values_hash)
|
144
|
-
register_enumeration values_hash
|
158
|
+
register_enumeration normalize_enumeration(values_hash)
|
145
159
|
values_hash.each_pair { |value_name, attributes| define_enumeration_constant value_name, attributes[0] }
|
146
160
|
define_enumeration_list values_hash
|
147
161
|
end
|
148
162
|
|
149
163
|
private
|
164
|
+
def self.normalize_enumeration(values_hash)
|
165
|
+
values_hash.each_pair do |key, value|
|
166
|
+
unless value.is_a? Array
|
167
|
+
values_hash[key] = [value, key.to_s.gsub(/_/, ' ').split.map(&:capitalize).join(' ')]
|
168
|
+
end
|
169
|
+
end
|
170
|
+
values_hash
|
171
|
+
end
|
172
|
+
|
150
173
|
def self.register_enumeration(values_hash)
|
151
174
|
@@registered_enumerations[self] = values_hash
|
152
175
|
end
|
data/spec/enumerate_it_spec.rb
CHANGED
@@ -9,6 +9,13 @@ class TestEnumeration < EnumerateIt::Base
|
|
9
9
|
)
|
10
10
|
end
|
11
11
|
|
12
|
+
class TestEnumerationWithoutArray < EnumerateIt::Base
|
13
|
+
associate_values(
|
14
|
+
:value_one => '1',
|
15
|
+
:value_two => '2'
|
16
|
+
)
|
17
|
+
end
|
18
|
+
|
12
19
|
describe EnumerateIt do
|
13
20
|
before :each do
|
14
21
|
class TestClass
|
@@ -16,9 +23,7 @@ describe EnumerateIt do
|
|
16
23
|
attr_accessor :foobar
|
17
24
|
has_enumeration_for :foobar, :with => TestEnumeration
|
18
25
|
|
19
|
-
def initialize(foobar)
|
20
|
-
@foobar = foobar
|
21
|
-
end
|
26
|
+
def initialize(foobar); @foobar = foobar; end
|
22
27
|
end
|
23
28
|
|
24
29
|
@target = TestClass.new(TestEnumeration::VALUE_2)
|
@@ -37,6 +42,24 @@ describe EnumerateIt do
|
|
37
42
|
it "defaults to not creating helper methods" do
|
38
43
|
@target.should_not respond_to(:value_1?)
|
39
44
|
end
|
45
|
+
|
46
|
+
context "passing the value of each option without the human string (just the value, without an array)" do
|
47
|
+
before :each do
|
48
|
+
class TestClassForEnumerationWithoutArray
|
49
|
+
include EnumerateIt
|
50
|
+
attr_accessor :foobar
|
51
|
+
has_enumeration_for :foobar, :with => TestEnumerationWithoutArray
|
52
|
+
|
53
|
+
def initialize(foobar); @foobar = foobar; end
|
54
|
+
end
|
55
|
+
|
56
|
+
@target = TestClassForEnumerationWithoutArray.new(TestEnumerationWithoutArray::VALUE_TWO)
|
57
|
+
end
|
58
|
+
|
59
|
+
it "humanizes the respective hash key" do
|
60
|
+
@target.foobar_humanize.should == 'Value Two'
|
61
|
+
end
|
62
|
+
end
|
40
63
|
end
|
41
64
|
|
42
65
|
context "using the option :create_helpers option" do
|
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
-
-
|
7
|
+
- 3
|
8
8
|
- 0
|
9
|
-
version: 0.
|
9
|
+
version: 0.3.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- "C\xC3\xA1ssio Marques"
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-03-
|
17
|
+
date: 2010-03-22 00:00:00 -03:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|