Browse files

Fix enum_definitions to act like a class attribute

  • Loading branch information...
1 parent ac1c57b commit 4117cd06f074fdad944dbd234b138d9731792669 @marcandre marcandre committed Jun 28, 2012
Showing with 10 additions and 5 deletions.
  1. +5 −3 lib/simple_enum.rb
  2. +1 −2 lib/simple_enum/mongoid.rb
  3. +4 −0 test/simple_enum_test.rb
View
8 lib/simple_enum.rb
@@ -45,8 +45,7 @@ def default_options
end
def included(base) #:nodoc:
- base.send :class_attribute, :enum_definitions, :instance_writer => false, :instance_reader => false
- base.enum_definitions = {}
+ base.send :class_attribute, :simple_enum_definitions, :instance_writer => false, :instance_reader => false
base.send :extend, ClassMethods
end
end
@@ -166,7 +165,6 @@ def as_enum(enum_cd, values, options = {})
values_inverted = values.invert
# store info away
- self.enum_definitions = {} if self.enum_definitions.nil?
self.enum_definitions[enum_cd] = self.enum_definitions[options[:column]] = { :name => enum_cd, :column => options[:column], :options => options }
# raise error if enum_cd == column
@@ -273,6 +271,10 @@ def human_enum_name(enum, key, options = {})
options.reverse_merge! :count => 1, :default => defaults
I18n.translate(defaults.shift, options)
end
+
+ def enum_definitions
+ self.simple_enum_definitions ||= {}
+ end
end
end
View
3 lib/simple_enum/mongoid.rb
@@ -27,8 +27,7 @@ module Mongoid
included do
# create class level methods
- class_attribute :enum_definitions, :instance_writer => false, :instance_reader => false
- enum_definitions = {}
+ class_attribute :simple_enum_definitions, :instance_writer => false, :instance_reader => false
end
module ClassMethods
View
4 test/simple_enum_test.rb
@@ -14,6 +14,10 @@ def test_enum_definitions_only_available_from_class
assert_raises(NoMethodError) { Dummy.new.enum_definitions= {} }
end
+ def test_enum_definitions_local_to_model
+ assert_equal nil, Computer.enum_definitions[:gender]
+ end
+
def test_getting_the_correct_integer_values_when_setting_to_symbol
d = Dummy.new
d.gender = :male

0 comments on commit 4117cd0

Please sign in to comment.