enumerate_it 0.1.0 → 0.2.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/.gitignore CHANGED
@@ -19,3 +19,4 @@ rdoc
19
19
  pkg
20
20
 
21
21
  ## PROJECT::SPECIFIC
22
+ enumerate_it-*.gem
data/README.rdoc CHANGED
@@ -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 manipulate the has used to create the enumeration:
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
 
@@ -90,16 +90,26 @@ This will create:
90
90
  p.relationship_status = RelationshipStatus::DIVORCED
91
91
  p.relationsip_status_humanize # => 'Divorced'
92
92
 
93
- * If your class can manage validations and responds to :validates_inclusion_of, it will create this
94
- validation:
93
+ * If you pass the :create_helpers option as 'true', it will create a helper method for each enumeration option (this option defaults to false):
95
94
 
96
- class Person < ActiveRecord::Base
97
- has_enumeration_for :relationship_status, :with => RelationshipStatus
98
- end
95
+ class Person < ActiveRecord::Base
96
+ has_enumeration_for :relationship_status, :with => RelationshipStatus, :create_helpers => true
97
+ end
98
+
99
+ p = Person.new
100
+ p.relationship_status = RelationshipStatus::MARRIED
101
+ p.married? #=> true
102
+ p.divorced? #=> false
103
+
104
+ * If your class can manage validations and responds to :validates_inclusion_of, it will create this validation:
105
+
106
+ class Person < ActiveRecord::Base
107
+ has_enumeration_for :relationship_status, :with => RelationshipStatus
108
+ end
99
109
 
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"
110
+ p = Person.new :relationship_status => 6 # => there is no '6' value in the enumeration
111
+ p.valid? # => false
112
+ p.errors[:relationship_status] # => "is not included in the list"
103
113
 
104
114
  Remember that in Rails 3 you can add validations to any kind of class and not only to those derived from
105
115
  ActiveRecord::Base.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.2.0
@@ -0,0 +1,55 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{enumerate_it}
8
+ s.version = "0.2.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["C\303\241ssio Marques"]
12
+ s.date = %q{2010-03-16}
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
+ s.email = %q{cassiommc@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "enumerate_it.gemspec",
27
+ "lib/enumerate_it.rb",
28
+ "spec/enumerate_it_spec.rb",
29
+ "spec/spec.opts",
30
+ "spec/spec_helper.rb"
31
+ ]
32
+ s.homepage = %q{http://github.com/cassiomarques/enumerate_it}
33
+ s.rdoc_options = ["--charset=UTF-8"]
34
+ s.require_paths = ["lib"]
35
+ s.rubygems_version = %q{1.3.6}
36
+ s.summary = %q{Ruby Enumerations}
37
+ s.test_files = [
38
+ "spec/enumerate_it_spec.rb",
39
+ "spec/spec_helper.rb"
40
+ ]
41
+
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::RubyGemsVersion) >= 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
54
+ end
55
+
data/lib/enumerate_it.rb CHANGED
@@ -89,6 +89,18 @@
89
89
  # p = Person.new
90
90
  # p.relationship_status = RelationshipStatus::DIVORCED
91
91
  # p.relationsip_status_humanize # => 'Divorced'
92
+ #
93
+ # - If you pass the :create_helpers option as 'true', it will create a helper method for each enumeration
94
+ # option (this option defaults to false):
95
+ #
96
+ # class Person < ActiveRecord::Base
97
+ # has_enumeration_for :relationship_status, :with => RelationshipStatus, :create_helpers => true
98
+ # end
99
+ #
100
+ # p = Person.new
101
+ # p.relationship_status = RelationshipStatus::MARRIED
102
+ # p.married? #=> true
103
+ # p.divorced? #=> false
92
104
  #
93
105
  # - If your class can manage validations and responds to :validates_inclusion_of, it will create this
94
106
  # validation:
@@ -164,6 +176,9 @@ module EnumerateIt
164
176
  validates_inclusion_of attribute, :in => options[:with].list, :allow_blank => true
165
177
  end
166
178
  create_enumeration_humanize_method options[:with], attribute
179
+ if options[:create_helpers]
180
+ create_helper_methods options[:with], attribute
181
+ end
167
182
  end
168
183
 
169
184
  private
@@ -175,6 +190,16 @@ module EnumerateIt
175
190
  end
176
191
  end
177
192
  end
193
+
194
+ def create_helper_methods(klass, attribute_name)
195
+ class_eval do
196
+ klass.enumeration.keys.each do |option|
197
+ define_method "#{option}?" do
198
+ self.send(attribute_name) == klass.enumeration[option].first
199
+ end
200
+ end
201
+ end
202
+ end
178
203
  end
179
204
 
180
205
  def self.included(receiver)
@@ -33,6 +33,24 @@ describe EnumerateIt do
33
33
  @target.foobar = nil
34
34
  @target.foobar_humanize.should be_nil
35
35
  end
36
+
37
+ it "defaults to not creating helper methods" do
38
+ @target.should_not respond_to(:value_1?)
39
+ end
40
+ end
41
+
42
+ context "using the option :create_helpers option" do
43
+ before :each do
44
+ class TestClass
45
+ has_enumeration_for :foobar, :with => TestEnumeration, :create_helpers => true
46
+ end
47
+ end
48
+
49
+ it "creates helpers methods with question marks for each enumeration option" do
50
+ target = TestClass.new(TestEnumeration::VALUE_2)
51
+ target.should be_value_2
52
+ target.should_not be_value_1
53
+ end
36
54
  end
37
55
 
38
56
  describe EnumerateIt::Base do
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 1
7
+ - 2
8
8
  - 0
9
- version: 0.1.0
9
+ version: 0.2.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - "C\xC3\xA1ssio Marques"
@@ -47,9 +47,9 @@ files:
47
47
  - README.rdoc
48
48
  - Rakefile
49
49
  - VERSION
50
+ - enumerate_it.gemspec
50
51
  - lib/enumerate_it.rb
51
52
  - spec/enumerate_it_spec.rb
52
- - spec/renun_spec.rb
53
53
  - spec/spec.opts
54
54
  - spec/spec_helper.rb
55
55
  has_rdoc: true
@@ -84,5 +84,4 @@ specification_version: 3
84
84
  summary: Ruby Enumerations
85
85
  test_files:
86
86
  - spec/enumerate_it_spec.rb
87
- - spec/renun_spec.rb
88
87
  - spec/spec_helper.rb
data/spec/renun_spec.rb DELETED
@@ -1,79 +0,0 @@
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