enumerate_it 0.7.3 → 0.7.4

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,23 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
22
+ enumerate_it-*.gem
23
+ enumerate_it.gemspec
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in eblood_compomat.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,39 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ enumerate_it (0.7.4)
5
+
6
+ GEM
7
+ remote: http://rubygems.org/
8
+ specs:
9
+ activemodel (3.0.5)
10
+ activesupport (= 3.0.5)
11
+ builder (~> 2.1.2)
12
+ i18n (~> 0.4)
13
+ activerecord (3.0.5)
14
+ activemodel (= 3.0.5)
15
+ activesupport (= 3.0.5)
16
+ arel (~> 2.0.2)
17
+ tzinfo (~> 0.3.23)
18
+ activesupport (3.0.5)
19
+ arel (2.0.9)
20
+ builder (2.1.2)
21
+ diff-lcs (1.1.2)
22
+ i18n (0.5.0)
23
+ rspec (2.5.0)
24
+ rspec-core (~> 2.5.0)
25
+ rspec-expectations (~> 2.5.0)
26
+ rspec-mocks (~> 2.5.0)
27
+ rspec-core (2.5.1)
28
+ rspec-expectations (2.5.0)
29
+ diff-lcs (~> 1.1.2)
30
+ rspec-mocks (2.5.0)
31
+ tzinfo (0.3.24)
32
+
33
+ PLATFORMS
34
+ ruby
35
+
36
+ DEPENDENCIES
37
+ activerecord (= 3.0.5)
38
+ enumerate_it!
39
+ rspec (= 2.5.0)
data/README.rdoc CHANGED
@@ -6,41 +6,41 @@ Author: Cássio Marques - cassiommc at gmail
6
6
 
7
7
  Ok, I know there are a lot of different solutions to this problem. But none of them solved my problem,
8
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.
9
+ filled with those small, unchangeable tables used to create foreign key constraints everywhere.
10
10
 
11
11
  === For example:
12
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)
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
20
 
21
- select * from relationshipstatus;
22
- code | description
23
- -------+--------------
24
- 1 | Single
25
- 2 | Married
26
- 3 | Widow
27
- 4 | Divorced
21
+ select * from relationshipstatus;
22
+ code | description
23
+ -------+--------------
24
+ 1 | Single
25
+ 2 | Married
26
+ 3 | Widow
27
+ 4 | Divorced
28
28
 
29
29
 
30
- And then I had things like a people table with a 'relationship_status' column with a foreign key
30
+ And then I had things like a people table with a 'relationship_status' column with a foreign key
31
31
  pointing to the relationshipstatus table.
32
32
 
33
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:
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
36
  What does it mean when we say that someone or something is '2'?
37
37
 
38
38
  Enter EnumerateIt.
39
39
 
40
40
  == Creating enumerations
41
41
 
42
- Enumerations are created as models, but you can put then anywhere in your application. In Rails
43
- applications, you can put them inside models/.
42
+ Enumerations are created as models, but you can put then anywhere in your application. In Rails
43
+ applications, you can put them inside models/.
44
44
 
45
45
  class RelationshipStatus < EnumerateIt::Base
46
46
  associate_values(
@@ -66,7 +66,7 @@ This will create some nice stuff:
66
66
 
67
67
  RelationshipStatus.to_a # [["Divorced", 4],["Married", 2],["Single", 1],["Widow", 3]]
68
68
 
69
- * You can retrieve a list with values for a group of enumeration constants.
69
+ * You can retrieve a list with values for a group of enumeration constants.
70
70
 
71
71
  RelationshipStatus.values_for %w(MARRIED SINGLE) # [2, 1]
72
72
 
@@ -86,9 +86,9 @@ or not.
86
86
  has_enumeration_for :relationship_status, :with => RelationshipStatus
87
87
  end
88
88
 
89
- The :with option is not required. If you ommit it, EnumerateIt will try to load an enumeration class based on the camelized attribute name.
89
+ The :with option is not required. If you ommit it, EnumerateIt will try to load an enumeration class based on the camelized attribute name.
90
90
 
91
- This will create:
91
+ This will create:
92
92
 
93
93
  * A humanized description for the values of the enumerated attribute:
94
94
 
@@ -102,13 +102,17 @@ This will create:
102
102
  associate_values(
103
103
  :married => 1,
104
104
  :single => 2
105
- )
105
+ )
106
106
  end
107
107
 
108
108
  p = Person.new
109
109
  p.relationship_status = RelationshipStatus::MARRIED
110
110
  p.relationship_status_humanize # => 'Married'
111
111
 
112
+ * The associated enumerations can be retrieved with the 'enumerations' class method.
113
+
114
+ Person.enumerations[:relationship_status] # => RelationshipStatus
115
+
112
116
  * If you pass the :create_helpers option as 'true', it will create a helper method for each enumeration option (this option defaults to false):
113
117
 
114
118
  class Person < ActiveRecord::Base
@@ -120,6 +124,17 @@ This will create:
120
124
  p.married? #=> true
121
125
  p.divorced? #=> false
122
126
 
127
+ * The :create_helpers also creates some mutator helper methods, that can be used to change the attribute's value.
128
+
129
+ class Person < ActiveRecord::Base
130
+ has_enumeration_for :relationship_status, :with => RelationshipStatus, :create_helpers => true
131
+ end
132
+
133
+ p = Person.new
134
+ p.married!
135
+ p.married? #=> true
136
+ p.divorced? #=> false
137
+
123
138
  * If your class can manage validations and responds to :validates_inclusion_of, it will create this validation:
124
139
 
125
140
  class Person < ActiveRecord::Base
@@ -141,7 +156,7 @@ This will create:
141
156
  p.errors[:relationship_status] # => "can't be blank"
142
157
 
143
158
 
144
- Remember that in Rails 3 you can add validations to any kind of class and not only to those derived from
159
+ Remember that in Rails 3 you can add validations to any kind of class and not only to those derived from
145
160
  ActiveRecord::Base.
146
161
 
147
162
  == I18n
@@ -162,16 +177,16 @@ located on enumerations.'enumeration_name'.'key' :
162
177
  enumerations:
163
178
  relationship_status:
164
179
  married: Casado
165
-
180
+
166
181
  p = Person.new
167
182
  p.relationship_status = RelationshipStatus::MARRIED
168
- p.relationship_status_humanize # => 'Casado'
169
-
183
+ p.relationship_status_humanize # => 'Casado'
184
+
170
185
  p.relationship_status = RelationshipStatus::SINGLE
171
- p.relationship_status_humanize # => 'Single' => nonexistent key
172
-
186
+ p.relationship_status_humanize # => 'Single' => nonexistent key
187
+
173
188
  p.relationship_status = RelationshipStatus::DIVORCED
174
- p.relationship_status_humanize # => 'He's divorced' => uses the provided string
189
+ p.relationship_status_humanize # => 'He's divorced' => uses the provided string
175
190
 
176
191
  You can also translate specific values:
177
192
 
@@ -185,7 +200,7 @@ You can also translate specific values:
185
200
 
186
201
  * Create an initializer with the following code:
187
202
 
188
- ActiveRecord::Base.send :include, EnumerateIt
203
+ ActiveRecord::Base.send :include, EnumerateIt
189
204
 
190
205
  * Add the 'enumerate_it' gem as a dependency in your environment.rb (Rails 2.3.x) or Gemfile (if you're using Bundler)
191
206
 
@@ -201,11 +216,24 @@ An interesting approach to use it in Rails apps is to create an app/models/enume
201
216
 
202
217
  EnumerateIt is fully compatible with Ruby 1.9.1 and 1.9.2 (all tests pass)
203
218
 
219
+ * Note: on ruby 1.9.2, if you are using the enumerations in a separate folder like app/models/enumerations, and have to use the :with parameter, you have to clear the enum class namespace to a global scope by using ::EnumClass instead of EnumClass:
220
+
221
+ # 1.8.7
222
+ class Person < ActiveRecord::Base
223
+ has_enumeration_for :relationship_status, :with => EnumClass
224
+ end
225
+
226
+ # 1.9.2
227
+ class Person < ActiveRecord::Base
228
+ has_enumeration_for :relationship_status, :with => ::EnumClass
229
+ end
230
+
231
+
204
232
  == Why did you reinvent the wheel?
205
233
 
206
234
  There are other similar solutions to the problem out there, but I could not find one that
207
- worked both with strings and integers as the enumerations' codes. I had both situations in
208
- my legacy database.
235
+ worked both with strings and integers as the enumerations' codes. I had both situations in
236
+ my legacy database.
209
237
 
210
238
  == Why defining enumerations outside the class that use it?
211
239
 
@@ -214,7 +242,7 @@ my legacy database.
214
242
  * You can reuse the enumeration inside other classes.
215
243
 
216
244
  == Note on Patches/Pull Requests
217
-
245
+
218
246
  * Fork the project.
219
247
  * Make your feature addition or bug fix.
220
248
  * Add tests for it. This is important so I don't break it in a
data/Rakefile CHANGED
@@ -1,47 +1,12 @@
1
1
  # encoding: utf-8
2
2
 
3
- require 'rubygems'
4
- require 'rake'
3
+ require 'bundler'
4
+ Bundler::GemHelper.install_tasks
5
5
 
6
- begin
7
- require 'jeweler'
8
- Jeweler::Tasks.new do |gem|
9
- gem.name = "enumerate_it"
10
- gem.summary = %Q{Ruby Enumerations}
11
- 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.}
12
- gem.email = "cassiommc@gmail.com"
13
- gem.homepage = "http://github.com/cassiomarques/enumerate_it"
14
- gem.authors = ["Cássio Marques"]
15
- gem.add_development_dependency "rspec", ">= 1.2.9"
16
- # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
17
- end
18
- Jeweler::GemcutterTasks.new
19
- rescue LoadError
20
- puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
6
+ require 'rspec/core'
7
+ require 'rspec/core/rake_task'
8
+ RSpec::Core::RakeTask.new(:spec) do |spec|
9
+ spec.pattern = FileList['spec/**/*_spec.rb']
21
10
  end
22
11
 
23
- require 'spec/rake/spectask'
24
- Spec::Rake::SpecTask.new(:spec) do |spec|
25
- spec.libs << 'lib' << 'spec'
26
- spec.spec_files = FileList['spec/**/*_spec.rb']
27
- end
28
-
29
- Spec::Rake::SpecTask.new(:rcov) do |spec|
30
- spec.libs << 'lib' << 'spec'
31
- spec.pattern = 'spec/**/*_spec.rb'
32
- spec.rcov = true
33
- end
34
-
35
- task :spec => :check_dependencies
36
-
37
12
  task :default => :spec
38
-
39
- require 'rake/rdoctask'
40
- Rake::RDocTask.new do |rdoc|
41
- version = File.exist?('VERSION') ? File.read('VERSION') : ""
42
-
43
- rdoc.rdoc_dir = 'rdoc'
44
- rdoc.title = "enumerate_it #{version}"
45
- rdoc.rdoc_files.include('README*')
46
- rdoc.rdoc_files.include('lib/**/*.rb')
47
- end
data/enumerate_it.gemspec CHANGED
@@ -1,36 +1,22 @@
1
- # Generated by jeweler
2
- # DO NOT EDIT THIS FILE DIRECTLY
3
- # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
1
  # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "version"
5
4
 
6
5
  Gem::Specification.new do |s|
7
6
  s.name = %q{enumerate_it}
8
- s.version = "0.7.3"
9
-
10
- s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
7
+ s.version = EnumerateIt::VERSION
8
+ s.platform = Gem::Platform::RUBY
11
9
  s.authors = ["C\303\241ssio Marques"]
12
- s.date = %q{2011-02-03}
13
10
  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
11
  s.email = %q{cassiommc@gmail.com}
12
+ s.homepage = %q{http://github.com/cassiomarques/enumerate_it}
15
13
  s.extra_rdoc_files = [
16
14
  "LICENSE",
17
15
  "README.rdoc"
18
16
  ]
19
- s.files = [
20
- ".document",
21
- "LICENSE",
22
- "README.rdoc",
23
- "Rakefile",
24
- "VERSION",
25
- "enumerate_it.gemspec",
26
- "lib/enumerate_it.rb",
27
- "spec/enumerate_it_spec.rb",
28
- "spec/i18n/en.yml",
29
- "spec/i18n/pt.yml",
30
- "spec/spec.opts",
31
- "spec/spec_helper.rb"
32
- ]
33
- s.homepage = %q{http://github.com/cassiomarques/enumerate_it}
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
34
20
  s.require_paths = ["lib"]
35
21
  s.rubygems_version = %q{1.3.7}
36
22
  s.summary = %q{Ruby Enumerations}
@@ -39,17 +25,8 @@ Gem::Specification.new do |s|
39
25
  "spec/spec_helper.rb"
40
26
  ]
41
27
 
42
- if s.respond_to? :specification_version then
43
- current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
44
- s.specification_version = 3
45
-
46
- if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
47
- s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
48
- else
49
- s.add_dependency(%q<rspec>, [">= 1.2.9"])
50
- end
51
- else
52
- s.add_dependency(%q<rspec>, [">= 1.2.9"])
53
- end
28
+ s.add_development_dependency "rspec", "=2.5.0"
29
+ s.add_development_dependency "activerecord", "=3.0.5"
30
+ s.add_development_dependency "activerecord", "=3.0.5"
54
31
  end
55
32
 
data/lib/enumerate_it.rb CHANGED
@@ -111,6 +111,9 @@
111
111
  # p.relationship_status = RelationshipStatus::MARRIED
112
112
  # p.relationship_status_humanize # => 'Married'
113
113
  #
114
+ # - The associated enumerations can be retrieved with the 'enumerations' class method.
115
+ # Person.enumerations[:relationship_status] # => RelationshipStatus
116
+ #
114
117
  # - If you pass the :create_helpers option as 'true', it will create a helper method for each enumeration
115
118
  # option (this option defaults to false):
116
119
  #
@@ -229,13 +232,23 @@ module EnumerateIt
229
232
  define_enumeration_class attribute, options
230
233
  set_validations attribute, options
231
234
  create_enumeration_humanize_method options[:with], attribute
235
+ store_enumeration options[:with], attribute
232
236
  if options[:create_helpers]
233
- create_helper_methods(options[:with], attribute)
237
+ create_helper_methods(options[:with], attribute)
234
238
  create_mutator_methods(options[:with], attribute)
235
239
  end
236
240
  end
237
241
 
242
+ def enumerations
243
+ @@_enumerations ||= {}
244
+ end
245
+
238
246
  private
247
+
248
+ def store_enumeration(klass, attribute)
249
+ enumerations[attribute] = klass
250
+ end
251
+
239
252
  def create_enumeration_humanize_method(klass, attribute_name)
240
253
  class_eval do
241
254
  define_method "#{attribute_name}_humanize" do
@@ -267,7 +280,7 @@ module EnumerateIt
267
280
  end
268
281
 
269
282
  def define_enumeration_class(attribute, options)
270
- if options[:with].blank?
283
+ if options[:with].nil?
271
284
  options[:with] = attribute.to_s.camelize.constantize
272
285
  end
273
286
  end
data/lib/version.rb ADDED
@@ -0,0 +1,4 @@
1
+ module EnumerateIt
2
+ VERSION = "0.7.4"
3
+ end
4
+
@@ -54,6 +54,10 @@ describe EnumerateIt do
54
54
  @target.should_not respond_to(:value_1?)
55
55
  end
56
56
 
57
+ it "stores the enumeration class in a class-level hash" do
58
+ TestClass.enumerations[:foobar].should == TestEnumeration
59
+ end
60
+
57
61
  context "passing the value of each option without the human string (just the value, without an array)" do
58
62
  before :each do
59
63
  class TestClassForEnumerationWithoutArray
@@ -116,7 +120,7 @@ describe EnumerateIt do
116
120
  it "creates a mutator method for each enumeration value" do
117
121
  [:value_1, :value_2, :value_3].each do |value|
118
122
  TestClass.new(TestEnumeration::VALUE_1).should respond_to(:"#{value}!")
119
- end
123
+ end
120
124
  end
121
125
 
122
126
  it "changes the attribute's value through mutator methods" do
data/spec/spec_helper.rb CHANGED
@@ -1,10 +1,11 @@
1
1
  $LOAD_PATH.unshift(File.dirname(__FILE__))
2
2
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
3
  require 'enumerate_it'
4
- require 'spec'
5
- require 'spec/autorun'
4
+ require 'rspec'
5
+ require 'rspec/autorun'
6
6
 
7
7
  require 'rubygems'
8
- require 'active_record'
8
+ require "active_support"
9
+ require "active_support/core_ext/string/inflections"
9
10
 
10
- I18n.load_path = Dir['spec/i18n/*.yml']
11
+ I18n.load_path = Dir['spec/i18n/*.yml']
metadata CHANGED
@@ -1,13 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: enumerate_it
3
3
  version: !ruby/object:Gem::Version
4
- hash: 5
5
- prerelease: false
6
- segments:
7
- - 0
8
- - 7
9
- - 3
10
- version: 0.7.3
4
+ prerelease:
5
+ version: 0.7.4
11
6
  platform: ruby
12
7
  authors:
13
8
  - "C\xC3\xA1ssio Marques"
@@ -15,7 +10,7 @@ autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
12
 
18
- date: 2011-02-03 00:00:00 -02:00
13
+ date: 2011-03-11 00:00:00 -03:00
19
14
  default_executable:
20
15
  dependencies:
21
16
  - !ruby/object:Gem::Dependency
@@ -24,16 +19,33 @@ dependencies:
24
19
  requirement: &id001 !ruby/object:Gem::Requirement
25
20
  none: false
26
21
  requirements:
27
- - - ">="
22
+ - - "="
28
23
  - !ruby/object:Gem::Version
29
- hash: 13
30
- segments:
31
- - 1
32
- - 2
33
- - 9
34
- version: 1.2.9
24
+ version: 2.5.0
35
25
  type: :development
36
26
  version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: activerecord
29
+ prerelease: false
30
+ requirement: &id002 !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - "="
34
+ - !ruby/object:Gem::Version
35
+ version: 3.0.5
36
+ type: :development
37
+ version_requirements: *id002
38
+ - !ruby/object:Gem::Dependency
39
+ name: activerecord
40
+ prerelease: false
41
+ requirement: &id003 !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - "="
45
+ - !ruby/object:Gem::Version
46
+ version: 3.0.5
47
+ type: :development
48
+ version_requirements: *id003
37
49
  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.
38
50
  email: cassiommc@gmail.com
39
51
  executables: []
@@ -45,12 +57,15 @@ extra_rdoc_files:
45
57
  - README.rdoc
46
58
  files:
47
59
  - .document
60
+ - .gitignore
61
+ - Gemfile
62
+ - Gemfile.lock
48
63
  - LICENSE
49
64
  - README.rdoc
50
65
  - Rakefile
51
- - VERSION
52
66
  - enumerate_it.gemspec
53
67
  - lib/enumerate_it.rb
68
+ - lib/version.rb
54
69
  - spec/enumerate_it_spec.rb
55
70
  - spec/i18n/en.yml
56
71
  - spec/i18n/pt.yml
@@ -70,23 +85,17 @@ required_ruby_version: !ruby/object:Gem::Requirement
70
85
  requirements:
71
86
  - - ">="
72
87
  - !ruby/object:Gem::Version
73
- hash: 3
74
- segments:
75
- - 0
76
88
  version: "0"
77
89
  required_rubygems_version: !ruby/object:Gem::Requirement
78
90
  none: false
79
91
  requirements:
80
92
  - - ">="
81
93
  - !ruby/object:Gem::Version
82
- hash: 3
83
- segments:
84
- - 0
85
94
  version: "0"
86
95
  requirements: []
87
96
 
88
97
  rubyforge_project:
89
- rubygems_version: 1.3.7
98
+ rubygems_version: 1.5.2
90
99
  signing_key:
91
100
  specification_version: 3
92
101
  summary: Ruby Enumerations
data/VERSION DELETED
@@ -1 +0,0 @@
1
- 0.7.3