Permalink
Browse files

Merge pull request #25 from marcandre/fix_enum_definitions

Fix enum definitions
  • Loading branch information...
lwe committed Jun 29, 2012
2 parents 82e6c15 + 4117cd0 commit b310e103bd6a55c5274eb30b164299fc3be7494f
Showing with 17 additions and 7 deletions.
  1. +6 −4 lib/simple_enum.rb
  2. +2 −3 lib/simple_enum/mongoid.rb
  3. +9 −0 test/simple_enum_test.rb
View
@@ -45,8 +45,7 @@ def default_options
end
def included(base) #:nodoc:
- base.send :class_attribute, :enum_definitions, :instance_write => 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
@@ -214,7 +212,7 @@ def as_enum(enum_cd, values, options = {})
end
class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
- class_attribute #{enum_attr.inspect}, :instance_write => false, :instance_reader => false
+ class_attribute #{enum_attr.inspect}, :instance_writer => false, :instance_reader => false
def self.#{attr_name}(*args)
return #{enum_attr} if args.first.nil?
@@ -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
@@ -27,8 +27,7 @@ module Mongoid
included do
# create class level methods
- class_attribute :enum_definitions, :instance_write => false, :instance_reader => false
- enum_definitions = {}
+ class_attribute :simple_enum_definitions, :instance_writer => false, :instance_reader => false
end
module ClassMethods
@@ -49,4 +48,4 @@ def as_enum_with_mongoid(enum_cd, values, options = {})
alias_method_chain :as_enum, :mongoid
end
end
-end
+end
View
@@ -9,6 +9,15 @@ def test_reading_public_enum_definitions
assert_equal "gender_cd", Dummy.enum_definitions[:gender][:column]
end
+ def test_enum_definitions_only_available_from_class
+ assert_raises(NoMethodError) { Dummy.new.enum_definitions }
+ 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 b310e10

Please sign in to comment.