enumerate_it 0.4.0 → 0.5.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/README.rdoc +30 -1
- data/VERSION +1 -1
- data/enumerate_it.gemspec +4 -2
- data/lib/enumerate_it.rb +13 -5
- data/spec/enumerate_it_spec.rb +17 -1
- data/spec/i18n/en.yml +4 -0
- data/spec/i18n/pt.yml +4 -0
- data/spec/spec_helper.rb +1 -0
- metadata +5 -3
data/README.rdoc
CHANGED
@@ -47,7 +47,7 @@ applications, I put them inside models/.
|
|
47
47
|
:single => [1, 'Single'],
|
48
48
|
:married => [2, 'Married'],
|
49
49
|
:widow => [3, 'Widow'],
|
50
|
-
:divorced => [4, 'Divorced']
|
50
|
+
:divorced => [4, 'Divorced']
|
51
51
|
)
|
52
52
|
end
|
53
53
|
|
@@ -129,6 +129,35 @@ This will create:
|
|
129
129
|
Remember that in Rails 3 you can add validations to any kind of class and not only to those derived from
|
130
130
|
ActiveRecord::Base.
|
131
131
|
|
132
|
+
== I18n
|
133
|
+
|
134
|
+
I18n lookup is provided on both '_humanized' and 'Enumeration#to_a' methods, given the hash key is a Symbol. The I18n strings are
|
135
|
+
located on enumerations.'enumeration_name'.'key' :
|
136
|
+
|
137
|
+
class RelationshipStatus < EnumerateIt::Base
|
138
|
+
associate_values(
|
139
|
+
:married => 1,
|
140
|
+
:single => 2,
|
141
|
+
:divorced => [3, 'He's divorced']
|
142
|
+
)
|
143
|
+
end
|
144
|
+
|
145
|
+
# your locale file
|
146
|
+
pt:
|
147
|
+
enumerations:
|
148
|
+
relationship_status:
|
149
|
+
single: Casado
|
150
|
+
|
151
|
+
p = Person.new
|
152
|
+
p.relationship_status = RelationshipStatus::MARRIED
|
153
|
+
p.relationship_status_humanize # => 'Casado'
|
154
|
+
|
155
|
+
p.relationship_status = RelationshipStatus::SINGLE
|
156
|
+
p.relationship_status_humanize # => 'Single' => nonexistent key
|
157
|
+
|
158
|
+
p.relationship_status = RelationshipStatus::DIVORCED
|
159
|
+
p.relationship_status_humanize # => 'He's divorced' => uses the provided string
|
160
|
+
|
132
161
|
== Installation
|
133
162
|
|
134
163
|
gem install enumerate_it
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.5.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.5.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["C\303\241ssio Marques"]
|
12
|
-
s.date = %q{2010-04-
|
12
|
+
s.date = %q{2010-04-14}
|
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 = [
|
@@ -26,6 +26,8 @@ Gem::Specification.new do |s|
|
|
26
26
|
"enumerate_it.gemspec",
|
27
27
|
"lib/enumerate_it.rb",
|
28
28
|
"spec/enumerate_it_spec.rb",
|
29
|
+
"spec/i18n/en.yml",
|
30
|
+
"spec/i18n/pt.yml",
|
29
31
|
"spec/spec.opts",
|
30
32
|
"spec/spec_helper.rb"
|
31
33
|
]
|
data/lib/enumerate_it.rb
CHANGED
@@ -167,7 +167,7 @@ module EnumerateIt
|
|
167
167
|
def self.normalize_enumeration(values_hash)
|
168
168
|
values_hash.each_pair do |key, value|
|
169
169
|
unless value.is_a? Array
|
170
|
-
values_hash[key] = [value, key
|
170
|
+
values_hash[key] = [value, key]
|
171
171
|
end
|
172
172
|
end
|
173
173
|
values_hash
|
@@ -183,15 +183,22 @@ module EnumerateIt
|
|
183
183
|
|
184
184
|
def self.define_enumeration_list(values_hash)
|
185
185
|
def self.list
|
186
|
-
@@registered_enumerations[self].values.map { |value| value[0] }.sort
|
186
|
+
@@registered_enumerations[self].values.map { |value| translate(value[0]) }.sort
|
187
187
|
end
|
188
188
|
|
189
189
|
def self.enumeration
|
190
190
|
@@registered_enumerations[self]
|
191
191
|
end
|
192
|
-
|
192
|
+
|
193
193
|
def self.to_a
|
194
|
-
@@registered_enumerations[self].values.map {|value| value
|
194
|
+
@@registered_enumerations[self].values.map {|value| [translate(value[1]), value[0]] }.sort_by { |value| value[0] }
|
195
|
+
end
|
196
|
+
|
197
|
+
def self.translate(value)
|
198
|
+
return value unless value.is_a? Symbol
|
199
|
+
|
200
|
+
default = value.to_s.to_s.gsub(/_/, ' ').split.map(&:capitalize).join(' ')
|
201
|
+
I18n.t("enumerations.#{self.name.underscore}.#{value.to_s.underscore}", :default => default)
|
195
202
|
end
|
196
203
|
end
|
197
204
|
end
|
@@ -213,7 +220,8 @@ module EnumerateIt
|
|
213
220
|
class_eval do
|
214
221
|
define_method "#{attribute_name}_humanize" do
|
215
222
|
values = klass.enumeration.values.detect { |v| v[0] == self.send(attribute_name) }
|
216
|
-
|
223
|
+
|
224
|
+
values ? klass.translate(values[1]) : nil
|
217
225
|
end
|
218
226
|
end
|
219
227
|
end
|
data/spec/enumerate_it_spec.rb
CHANGED
@@ -24,6 +24,7 @@ describe EnumerateIt do
|
|
24
24
|
has_enumeration_for :foobar, :with => TestEnumeration
|
25
25
|
|
26
26
|
def initialize(foobar); @foobar = foobar; end
|
27
|
+
I18n.locale = :en
|
27
28
|
end
|
28
29
|
|
29
30
|
@target = TestClass.new(TestEnumeration::VALUE_2)
|
@@ -55,10 +56,19 @@ describe EnumerateIt do
|
|
55
56
|
|
56
57
|
@target = TestClassForEnumerationWithoutArray.new(TestEnumerationWithoutArray::VALUE_TWO)
|
57
58
|
end
|
58
|
-
|
59
|
+
|
59
60
|
it "humanizes the respective hash key" do
|
60
61
|
@target.foobar_humanize.should == 'Value Two'
|
61
62
|
end
|
63
|
+
|
64
|
+
it "translates the respective hash key when a translation is found" do
|
65
|
+
@target.foobar = TestEnumerationWithoutArray::VALUE_ONE
|
66
|
+
@target.foobar_humanize.should == 'First Value'
|
67
|
+
I18n.locale = :pt
|
68
|
+
|
69
|
+
@target.foobar_humanize.should == 'Primeiro Valor'
|
70
|
+
end
|
71
|
+
|
62
72
|
end
|
63
73
|
|
64
74
|
context "without passing the enumeration class" do
|
@@ -117,6 +127,12 @@ describe EnumerateIt do
|
|
117
127
|
it "returns an array with the values and human representations" do
|
118
128
|
TestEnumeration.to_a.should == [['Hey, I am 1!', '1'], ['Hey, I am 2!', '2'], ['Hey, I am 3!', '3']]
|
119
129
|
end
|
130
|
+
|
131
|
+
it "translates the available values" do
|
132
|
+
TestEnumerationWithoutArray.to_a.should == [['First Value', '1'], ['Value Two', '2']]
|
133
|
+
I18n.locale = :pt
|
134
|
+
TestEnumerationWithoutArray.to_a.should == [['Primeiro Valor', '1'], ['Value Two', '2']]
|
135
|
+
end
|
120
136
|
end
|
121
137
|
|
122
138
|
context "when included in ActiveRecord::Base" do
|
data/spec/i18n/en.yml
ADDED
data/spec/i18n/pt.yml
ADDED
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
-
-
|
7
|
+
- 5
|
8
8
|
- 0
|
9
|
-
version: 0.
|
9
|
+
version: 0.5.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-04-
|
17
|
+
date: 2010-04-14 00:00:00 -03:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -50,6 +50,8 @@ files:
|
|
50
50
|
- enumerate_it.gemspec
|
51
51
|
- lib/enumerate_it.rb
|
52
52
|
- spec/enumerate_it_spec.rb
|
53
|
+
- spec/i18n/en.yml
|
54
|
+
- spec/i18n/pt.yml
|
53
55
|
- spec/spec.opts
|
54
56
|
- spec/spec_helper.rb
|
55
57
|
has_rdoc: true
|