enumerate_it 0.7.8 → 0.7.9
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/Gemfile.lock +1 -1
- data/README.rdoc +18 -1
- data/lib/enumerate_it.rb +20 -2
- data/lib/version.rb +1 -1
- data/spec/enumerate_it_spec.rb +18 -1
- metadata +48 -52
data/Gemfile.lock
CHANGED
data/README.rdoc
CHANGED
@@ -74,6 +74,23 @@ This will create some nice stuff:
|
|
74
74
|
|
75
75
|
RelationshipStatus.enumeration # returns the exact hash used to define the enumeration
|
76
76
|
|
77
|
+
You can also create enumerations in the following ways:
|
78
|
+
|
79
|
+
* Passing an array of symbols, so that the respective value for each symbol will be the stringified version of the symbol itself:
|
80
|
+
|
81
|
+
class RelationshipStatus < EnumerateIt::Base
|
82
|
+
associate_values :married, :single
|
83
|
+
end
|
84
|
+
|
85
|
+
RelationshipStatus::MARRIED # returns "married" and so on
|
86
|
+
|
87
|
+
* Passing hashes where the value for each key/pair does not include a translation. In this case, the I18n feature will be used (more on this below):
|
88
|
+
|
89
|
+
class RelationshipStatus < EnumerateIt::Base
|
90
|
+
associate_values :married => 1, :single => 2
|
91
|
+
end
|
92
|
+
|
93
|
+
|
77
94
|
== Using enumerations
|
78
95
|
|
79
96
|
The cool part is that you can use these enumerations with any class, be it an ActiveRecord instance
|
@@ -111,7 +128,7 @@ This will create:
|
|
111
128
|
|
112
129
|
* The associated enumerations can be retrieved with the 'enumerations' class method.
|
113
130
|
|
114
|
-
|
131
|
+
Person.enumerations[:relationship_status] # => RelationshipStatus
|
115
132
|
|
116
133
|
* If you pass the :create_helpers option as 'true', it will create a helper method for each enumeration option (this option defaults to false):
|
117
134
|
|
data/lib/enumerate_it.rb
CHANGED
@@ -70,10 +70,26 @@
|
|
70
70
|
#
|
71
71
|
# RelationshipStatus.valus_for %w(MARRIED SINGLE) # [2, 1]
|
72
72
|
#
|
73
|
-
# - You can manipulate the
|
73
|
+
# - You can manipulate the hash used to create the enumeration:
|
74
74
|
#
|
75
75
|
# RelationshipStatus.enumeration # returns the exact hash used to define the enumeration
|
76
76
|
#
|
77
|
+
# You can also create enumerations in the following ways:
|
78
|
+
#
|
79
|
+
# * Passing an array of symbols, so that the respective value for each symbol will be the stringified version of the symbol itself:
|
80
|
+
#
|
81
|
+
# class RelationshipStatus < EnumerateIt::Base
|
82
|
+
# associate_values :married, :single
|
83
|
+
# end
|
84
|
+
#
|
85
|
+
# RelationshipStatus::MARRIED # returns "married" and so on
|
86
|
+
#
|
87
|
+
# * Passing hashes where the value for each key/pair does not include a translation. In this case, the I18n feature will be used (more on this below):
|
88
|
+
#
|
89
|
+
# class RelationshipStatus < EnumerateIt::Base
|
90
|
+
# associate_values :married => 1, :single => 2
|
91
|
+
# end
|
92
|
+
#
|
77
93
|
# = Using enumerations
|
78
94
|
#
|
79
95
|
# The cool part is that you can use these enumerations with any class, be it an ActiveRecord instance
|
@@ -181,7 +197,9 @@ module EnumerateIt
|
|
181
197
|
class Base
|
182
198
|
@@registered_enumerations = {}
|
183
199
|
|
184
|
-
def self.associate_values(
|
200
|
+
def self.associate_values(*args)
|
201
|
+
values_hash = args.first.is_a?(Hash) ? args.first : args.inject({}) { |h, v| h[v] = v.to_s; h }
|
202
|
+
|
185
203
|
register_enumeration normalize_enumeration(values_hash)
|
186
204
|
values_hash.each_pair { |value_name, attributes| define_enumeration_constant value_name, attributes[0] }
|
187
205
|
end
|
data/lib/version.rb
CHANGED
data/spec/enumerate_it_spec.rb
CHANGED
@@ -26,15 +26,21 @@ class TestEnumerationWithExtendedBehaviour < EnumerateIt::Base
|
|
26
26
|
end
|
27
27
|
end
|
28
28
|
|
29
|
+
class TestEnumerationWithList < EnumerateIt::Base
|
30
|
+
associate_values :first, :second
|
31
|
+
end
|
32
|
+
|
29
33
|
class Foobar < EnumerateIt::Base
|
30
34
|
associate_values(
|
31
35
|
:bar => 'foo'
|
32
36
|
)
|
33
37
|
end
|
38
|
+
|
34
39
|
class BaseClass
|
35
40
|
include EnumerateIt
|
36
41
|
has_enumeration_for :foobar, :with => TestEnumeration
|
37
42
|
end
|
43
|
+
|
38
44
|
describe EnumerateIt do
|
39
45
|
before :each do
|
40
46
|
class TestClass
|
@@ -66,6 +72,7 @@ describe EnumerateIt do
|
|
66
72
|
it "stores the enumeration class in a class-level hash" do
|
67
73
|
TestClass.enumerations[:foobar].should == TestEnumeration
|
68
74
|
end
|
75
|
+
|
69
76
|
context 'declaring a simple enum on an inherited class' do
|
70
77
|
before do
|
71
78
|
class SomeClass < BaseClass
|
@@ -104,7 +111,6 @@ describe EnumerateIt do
|
|
104
111
|
|
105
112
|
@target.foobar_humanize.should == 'Primeiro Valor'
|
106
113
|
end
|
107
|
-
|
108
114
|
end
|
109
115
|
|
110
116
|
context "without passing the enumeration class" do
|
@@ -245,6 +251,17 @@ describe EnumerateIt::Base do
|
|
245
251
|
end
|
246
252
|
end
|
247
253
|
|
254
|
+
context 'associate values with a list' do
|
255
|
+
it "creates constants for each enumeration value" do
|
256
|
+
TestEnumerationWithList::FIRST.should == "first"
|
257
|
+
TestEnumerationWithList::SECOND.should == "second"
|
258
|
+
end
|
259
|
+
|
260
|
+
it "returns an array with the values and human representations" do
|
261
|
+
TestEnumerationWithList.to_a.should == [['First', 'first'], ['Second', 'second']]
|
262
|
+
end
|
263
|
+
end
|
264
|
+
|
248
265
|
context "when included in ActiveRecord::Base" do
|
249
266
|
before :each do
|
250
267
|
class ActiveRecordStub
|
metadata
CHANGED
@@ -1,61 +1,60 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: enumerate_it
|
3
|
-
version: !ruby/object:Gem::Version
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.7.9
|
4
5
|
prerelease:
|
5
|
-
version: 0.7.8
|
6
6
|
platform: ruby
|
7
|
-
authors:
|
8
|
-
-
|
7
|
+
authors:
|
8
|
+
- Cássio Marques
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
dependencies:
|
16
|
-
- !ruby/object:Gem::Dependency
|
12
|
+
date: 2011-11-14 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
17
15
|
name: rspec
|
18
|
-
|
19
|
-
requirement: &id001 !ruby/object:Gem::Requirement
|
16
|
+
requirement: &2168378640 !ruby/object:Gem::Requirement
|
20
17
|
none: false
|
21
|
-
requirements:
|
22
|
-
- -
|
23
|
-
- !ruby/object:Gem::Version
|
18
|
+
requirements:
|
19
|
+
- - =
|
20
|
+
- !ruby/object:Gem::Version
|
24
21
|
version: 2.5.0
|
25
22
|
type: :development
|
26
|
-
version_requirements: *id001
|
27
|
-
- !ruby/object:Gem::Dependency
|
28
|
-
name: activerecord
|
29
23
|
prerelease: false
|
30
|
-
|
24
|
+
version_requirements: *2168378640
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: activerecord
|
27
|
+
requirement: &2168378140 !ruby/object:Gem::Requirement
|
31
28
|
none: false
|
32
|
-
requirements:
|
33
|
-
- -
|
34
|
-
- !ruby/object:Gem::Version
|
29
|
+
requirements:
|
30
|
+
- - =
|
31
|
+
- !ruby/object:Gem::Version
|
35
32
|
version: 3.0.5
|
36
33
|
type: :development
|
37
|
-
version_requirements: *id002
|
38
|
-
- !ruby/object:Gem::Dependency
|
39
|
-
name: activesupport
|
40
34
|
prerelease: false
|
41
|
-
|
35
|
+
version_requirements: *2168378140
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: activesupport
|
38
|
+
requirement: &2168377680 !ruby/object:Gem::Requirement
|
42
39
|
none: false
|
43
|
-
requirements:
|
44
|
-
- -
|
45
|
-
- !ruby/object:Gem::Version
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
46
43
|
version: 2.3.2
|
47
44
|
type: :runtime
|
48
|
-
|
49
|
-
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *2168377680
|
47
|
+
description: Have a legacy database and need some enumerations in your models to match
|
48
|
+
those stupid '4 rows/2 columns' tables with foreign keys and stop doing joins just
|
49
|
+
to fetch a simple description? Or maybe use some integers instead of strings as
|
50
|
+
the code for each value of your enumerations? Here's EnumerateIt.
|
50
51
|
email: cassiommc@gmail.com
|
51
52
|
executables: []
|
52
|
-
|
53
53
|
extensions: []
|
54
|
-
|
55
|
-
extra_rdoc_files:
|
54
|
+
extra_rdoc_files:
|
56
55
|
- LICENSE
|
57
56
|
- README.rdoc
|
58
|
-
files:
|
57
|
+
files:
|
59
58
|
- .document
|
60
59
|
- .gitignore
|
61
60
|
- Gemfile
|
@@ -71,34 +70,31 @@ files:
|
|
71
70
|
- spec/i18n/pt.yml
|
72
71
|
- spec/spec.opts
|
73
72
|
- spec/spec_helper.rb
|
74
|
-
has_rdoc: true
|
75
73
|
homepage: http://github.com/cassiomarques/enumerate_it
|
76
74
|
licenses: []
|
77
|
-
|
78
75
|
post_install_message:
|
79
76
|
rdoc_options: []
|
80
|
-
|
81
|
-
require_paths:
|
77
|
+
require_paths:
|
82
78
|
- lib
|
83
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
79
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
84
80
|
none: false
|
85
|
-
requirements:
|
86
|
-
- -
|
87
|
-
- !ruby/object:Gem::Version
|
88
|
-
version:
|
89
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - ! '>='
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
85
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
90
86
|
none: false
|
91
|
-
requirements:
|
92
|
-
- -
|
93
|
-
- !ruby/object:Gem::Version
|
94
|
-
version:
|
87
|
+
requirements:
|
88
|
+
- - ! '>='
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
95
91
|
requirements: []
|
96
|
-
|
97
92
|
rubyforge_project:
|
98
|
-
rubygems_version: 1.
|
93
|
+
rubygems_version: 1.8.10
|
99
94
|
signing_key:
|
100
95
|
specification_version: 3
|
101
96
|
summary: Ruby Enumerations
|
102
|
-
test_files:
|
97
|
+
test_files:
|
103
98
|
- spec/enumerate_it_spec.rb
|
104
99
|
- spec/spec_helper.rb
|
100
|
+
has_rdoc:
|