Browse files

Allow enums to be assigned using class

  • Loading branch information...
1 parent 9698dd6 commit fc1a89bdfe47d3c6924f0e5157558acfc14508d8 @beerlington committed Jan 11, 2013
Showing with 18 additions and 5 deletions.
  1. +8 −0 CHANGELOG.md
  2. +1 −1 LICENSE
  3. +7 −2 lib/classy_enum/active_record.rb
  4. +1 −1 lib/classy_enum/version.rb
  5. +1 −1 spec/classy_enum/active_record_spec.rb
View
8 CHANGELOG.md
@@ -1,5 +1,13 @@
# ClassyEnum Changelog
+## 3.1.2
+
+* Allow enum property to be assigned using enum class. Previously it
+ could only be assigned with an instance, string or symbol.
+
+ @alarm.priority = Priority::Medium
+ @alarm.priority.medium? # => true
+
## 3.1.1
* Fixes a regression with Formtastic support. ClassyEnumm::Base.build now
View
2 LICENSE
@@ -1,4 +1,4 @@
-Copyright (c) 2010 Peter Brown
+Copyright (c) 2010-2013 Peter Brown
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
View
9 lib/classy_enum/active_record.rb
@@ -42,9 +42,14 @@ def classy_enum_attr(attribute, options={})
)
end
- # Define setter method that accepts either string or symbol for member
+ # Define setter method that accepts string, symbol, instance or class for member
define_method "#{attribute}=" do |value|
- value = value.to_s unless value.nil?
+ if value.class == Class && value < ClassyEnum::Base
+ value = value.new
+ elsif value.present?
+ value = value.to_s
+ end
+
super(value)
end
end
View
2 lib/classy_enum/version.rb
@@ -1,3 +1,3 @@
module ClassyEnum
- VERSION = "3.1.1"
+ VERSION = "3.1.2"
end
View
2 spec/classy_enum/active_record_spec.rb
@@ -43,7 +43,7 @@ class OtherDog < ActiveRecord::Base
specify { Dog.new(:breed => '').should_not be_valid }
context "with valid breed options" do
- [:golden_retriever, 'golden_retriever', Breed::GoldenRetriever.new].each do |option|
+ [:golden_retriever, 'golden_retriever', Breed::GoldenRetriever.new, Breed::GoldenRetriever].each do |option|
subject { Dog.new(:breed => option) }
it { should be_valid }
its(:breed) { should be_a(Breed::GoldenRetriever) }

0 comments on commit fc1a89b

Please sign in to comment.