enumerate_it 0.7.16 → 0.7.17

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- enumerate_it (0.7.14)
4
+ enumerate_it (0.7.17)
5
5
  activesupport (>= 3.0.0)
6
6
 
7
7
  GEM
@@ -1,5 +1,7 @@
1
1
  = EnumerateIt - Ruby Enumerations
2
2
 
3
+ {<img src="https://secure.travis-ci.org/cassiomarques/enumerate_it.png?branch=master" alt="Build Status" />}[http://travis-ci.org/cassiomarques/enumerate_it]
4
+
3
5
  Author: Cássio Marques - cassiommc at gmail
4
6
 
5
7
  == Description
@@ -161,6 +163,18 @@ This will create:
161
163
  p.married? #=> true
162
164
  p.divorced? #=> false
163
165
 
166
+ * It's also possible to "namespace" the created helper methods, passing a hash to the :create_helpers option.
167
+ This can be useful when two or more of the enumerations used share the same constants.
168
+
169
+ class Person < ActiveRecord::Base
170
+ has_enumeration_for :relationship_status, :with => RelationshipStatus, :create_helpers => { :prefix => true }
171
+ end
172
+
173
+ p = Person.new
174
+ p.relationship_status = RelationshipStatus::MARRIED
175
+ p.relationship_status_married? #=> true
176
+ p.relationship_status_divoced? #=> false
177
+
164
178
  * The :create_helpers also creates some mutator helper methods, that can be used to change the attribute's value.
165
179
 
166
180
  class Person < ActiveRecord::Base
@@ -158,6 +158,18 @@
158
158
  # p.married? #=> true
159
159
  # p.divorced? #=> false
160
160
  #
161
+ # - It's also possible to "namespace" the created helper methods, passing a hash to the :create_helpers option.
162
+ # This can be useful when two or more of the enumerations used share the same constants.
163
+ #
164
+ # class Person < ActiveRecord::Base
165
+ # has_enumeration_for :relationship_status, :with => RelationshipStatus, :create_helpers => { :prefix => true }
166
+ # end
167
+ #
168
+ # p = Person.new
169
+ # p.relationship_status = RelationshipStatus::MARRIED
170
+ # p.relationship_status_married? #=> true
171
+ # p.relationship_status_divoced? #=> false
172
+ #
161
173
  # - If you pass the :create_scopes option as 'true', it will create a scope method for each enumeration option (this option defaults to false):
162
174
  #
163
175
  # class Person < ActiveRecord::Base
@@ -305,8 +317,8 @@ module EnumerateIt
305
317
  create_enumeration_humanize_method options[:with], attribute
306
318
  store_enumeration options[:with], attribute
307
319
  if options[:create_helpers]
308
- create_helper_methods options[:with], attribute
309
- create_mutator_methods options[:with], attribute
320
+ create_helper_methods options[:with], attribute, options[:create_helpers]
321
+ create_mutator_methods options[:with], attribute, options[:create_helpers]
310
322
  end
311
323
 
312
324
  if options[:create_scopes]
@@ -329,10 +341,12 @@ module EnumerateIt
329
341
  end
330
342
  end
331
343
 
332
- def create_helper_methods(klass, attribute_name)
344
+ def create_helper_methods(klass, attribute_name, helpers)
345
+ prefix_name = "#{attribute_name}_" if helpers.is_a?(Hash) && helpers[:prefix]
346
+
333
347
  class_eval do
334
348
  klass.enumeration.keys.each do |option|
335
- define_method "#{option}?" do
349
+ define_method "#{prefix_name}#{option}?" do
336
350
  self.send(attribute_name) == klass.enumeration[option].first
337
351
  end
338
352
  end
@@ -347,10 +361,12 @@ module EnumerateIt
347
361
  end
348
362
  end
349
363
 
350
- def create_mutator_methods(klass, attribute_name)
364
+ def create_mutator_methods(klass, attribute_name, helpers)
365
+ prefix_name = "#{attribute_name}_" if helpers.is_a?(Hash) && helpers[:prefix]
366
+
351
367
  class_eval do
352
368
  klass.enumeration.each_pair do |key, values|
353
- define_method "#{key}!" do
369
+ define_method "#{prefix_name}#{key}!" do
354
370
  self.send "#{attribute_name}=", values.first
355
371
  end
356
372
  end
@@ -1,3 +1,3 @@
1
1
  module EnumerateIt
2
- VERSION = "0.7.16"
2
+ VERSION = "0.7.17"
3
3
  end
@@ -170,6 +170,31 @@ describe EnumerateIt do
170
170
  target.value_3!
171
171
  target.foobar.should == TestEnumeration::VALUE_3
172
172
  end
173
+
174
+ context "with :prefix option" do
175
+ before :each do
176
+ class TestClass
177
+ has_enumeration_for :foobar, :with => TestEnumeration, :create_helpers => { :prefix => true }
178
+ end
179
+ end
180
+
181
+ it "creates helpers methods with question marks and prefixes for each enumeration option" do
182
+ target = TestClass.new(TestEnumeration::VALUE_2)
183
+ target.should be_foobar_value_2
184
+ end
185
+
186
+ it "creates a mutator method for each enumeration value" do
187
+ [:value_1, :value_2, :value_3].each do |value|
188
+ TestClass.new(TestEnumeration::VALUE_1).should respond_to(:"foobar_#{value}!")
189
+ end
190
+ end
191
+
192
+ it "changes the attribute's value through mutator methods" do
193
+ target = TestClass.new(TestEnumeration::VALUE_2)
194
+ target.foobar_value_3!
195
+ target.foobar.should == TestEnumeration::VALUE_3
196
+ end
197
+ end
173
198
  end
174
199
 
175
200
  describe "using the :create_scopes option" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: enumerate_it
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.16
4
+ version: 0.7.17
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-07-27 00:00:00.000000000 Z
12
+ date: 2012-08-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -117,12 +117,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
117
117
  - - ! '>='
118
118
  - !ruby/object:Gem::Version
119
119
  version: '0'
120
+ segments:
121
+ - 0
122
+ hash: -392427821220719135
120
123
  required_rubygems_version: !ruby/object:Gem::Requirement
121
124
  none: false
122
125
  requirements:
123
126
  - - ! '>='
124
127
  - !ruby/object:Gem::Version
125
128
  version: '0'
129
+ segments:
130
+ - 0
131
+ hash: -392427821220719135
126
132
  requirements: []
127
133
  rubyforge_project:
128
134
  rubygems_version: 1.8.24
@@ -135,4 +141,3 @@ test_files:
135
141
  - spec/i18n/pt.yml
136
142
  - spec/spec.opts
137
143
  - spec/spec_helper.rb
138
- has_rdoc: