active_enum 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -63,6 +63,12 @@ Define enum class implicitly in enumerate block
63
63
  end
64
64
  end
65
65
 
66
+ Multiple attributes with same enum
67
+
68
+ class Patient < ActiveRecord::Base
69
+ enumerate :to, :from, :with => Sex
70
+ end
71
+
66
72
  Access the enum values and the enum class using the attribute method with a symbol for the enum component you want
67
73
 
68
74
  user = User.new
@@ -21,25 +21,32 @@ module ActiveEnum
21
21
  # enumerate :sex do
22
22
  # value :id => 1, :name => 'Male'
23
23
  # end
24
+ #
25
+ # # Multiple attributes with same enum
26
+ # enumerate :to, :from, :with => Sex
24
27
  #
25
- def enumerate(attribute, options={}, &block)
26
- attribute = attribute.to_sym
27
- if block_given?
28
- enum_name = "#{self.to_s.underscore}_#{attribute}"
29
- ActiveEnum.define { enum(enum_name, &block) }
30
- enum = enum_name.classify.constantize
31
- else
32
- enum = options[:with] || attribute.to_s.classify.constantize
33
- end
34
-
35
- self.enumerated_attributes[attribute] = enum
36
-
37
- define_active_enum_read_method(attribute)
38
- define_active_enum_write_method(attribute)
39
- define_active_enum_question_method(attribute)
40
- rescue NameError => e
41
- raise e unless e.message =~ /uninitialized constant/
42
- raise ActiveEnum::EnumNotFound, "Enum class could not be found for attribute '#{attribute}' in class #{self}. Specify the enum class using the :with option."
28
+ def enumerate(*attributes, &block)
29
+ options = attributes.extract_options!
30
+ attributes.each do |attribute|
31
+ begin
32
+ if block_given?
33
+ enum_name = "#{self.to_s.underscore}_#{attribute}"
34
+ ActiveEnum.define { enum(enum_name, &block) }
35
+ enum = enum_name.classify.constantize
36
+ else
37
+ enum = options[:with] || attribute.to_s.classify.constantize
38
+ end
39
+ attribute = attribute.to_sym
40
+ self.enumerated_attributes[attribute] = enum
41
+
42
+ define_active_enum_read_method(attribute)
43
+ define_active_enum_write_method(attribute)
44
+ define_active_enum_question_method(attribute)
45
+ rescue NameError => e
46
+ raise e unless e.message =~ /uninitialized constant/
47
+ raise ActiveEnum::EnumNotFound, "Enum class could not be found for attribute '#{attribute}' in class #{self}. Specify the enum class using the :with option."
48
+ end
49
+ end
43
50
  end
44
51
 
45
52
  def enum_for(attribute)
@@ -1,3 +1,3 @@
1
1
  module ActiveEnum
2
- VERSION = '0.3.0'
2
+ VERSION = '0.4.0'
3
3
  end
@@ -5,6 +5,12 @@ class Sex < ActiveEnum::Base
5
5
  value :id => 2, :name => 'Female'
6
6
  end
7
7
 
8
+ class Accepted < ActiveEnum::Base
9
+ value :id => 0, :name => 'No'
10
+ value :id => 1, :name => 'Definitely'
11
+ value :id => 2, :name => 'Maybe'
12
+ end
13
+
8
14
  describe ActiveEnum::Extensions do
9
15
  before do
10
16
  Person.class_eval do
@@ -21,6 +27,14 @@ describe ActiveEnum::Extensions do
21
27
  ActiveRecord::Base.should respond_to(:enum_for)
22
28
  end
23
29
 
30
+ it 'should allow multiple attributes to be enumerated with same enum' do
31
+ Person.class_eval do
32
+ enumerate :attending, :staying, :with => Accepted
33
+ end
34
+ Person.enum_for(:attending).should == Accepted
35
+ Person.enum_for(:staying).should == Accepted
36
+ end
37
+
24
38
  it 'should allow implicit enumeration class from attribute name' do
25
39
  Person.class_eval do
26
40
  enumerate :sex
data/spec/schema.rb CHANGED
@@ -4,6 +4,8 @@ ActiveRecord::Schema.define(:version => 1) do
4
4
  t.string :first_name
5
5
  t.string :last_name
6
6
  t.integer :sex
7
+ t.integer :attending
8
+ t.integer :staying
7
9
  end
8
10
 
9
11
  create_table :sexes, :force => true do |t|
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_enum
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Meehan
@@ -9,7 +9,7 @@ autorequire: active_enum
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-11-05 00:00:00 +11:00
12
+ date: 2009-11-06 00:00:00 +11:00
13
13
  default_executable:
14
14
  dependencies: []
15
15