Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Allow strings and symbols when testing for equality

Priority::Low.new == :low  # => true
Priority::Low.new == 'low' # => true
  • Loading branch information...
commit 63dfc4761bfd78b2e66446f7a65b67b8e0e85e89 1 parent 04d3227
@beerlington authored
View
5 CHANGELOG.md
@@ -10,6 +10,11 @@
* Introducing I18n support and providing a ClassyEnum::Base#text method
that will automatically translate text values.
* Translation support was added to ClassyEnum::Base.select_options.
+* Equality can now be determined using strings and symbols. The
+ following will return true:
+
+ Priority::Low.new == :low # => true
+ Priority::Low.new == 'low' # => true
## 3.0.0
View
2  lib/classy_enum/active_record.rb
@@ -29,7 +29,7 @@ def classy_enum_attr(attribute, options={})
# Add ActiveRecord validation to ensure it won't be saved unless it's an option
validates_inclusion_of attribute,
- :in => enum.all,
+ :in => enum,
:allow_blank => allow_blank,
:allow_nil => allow_nil
View
4 lib/classy_enum/collection.rb
@@ -19,6 +19,10 @@ module Collection
# priorities.max # => @high
# priorities.min # => @low
def <=> other
+ if other.is_a?(Symbol) || other.is_a?(String)
+ other = self.class.find(other)
+ end
+
index <=> other.index
end
View
4 lib/classy_enum/predicate.rb
@@ -4,7 +4,7 @@ module Predicate
# Define attribute methods like two?
def self.define_predicate_method(klass, enum)
klass.base_class.class_eval do
- define_method "#{enum}?", lambda { attribute?(enum.to_s) }
+ define_method "#{enum}?", lambda { attribute?(enum) }
end
end
@@ -29,7 +29,7 @@ def self.define_predicate_method(klass, enum)
# @dog.breed.snoop? # => true
# @dog.breed.golden_retriever? # => false
def attribute?(attribute)
- to_s == attribute
+ self == attribute
end
end
end
View
10 spec/classy_enum/active_record_spec.rb
@@ -43,10 +43,12 @@ class OtherDog < ActiveRecord::Base
specify { Dog.new(:breed => '').should_not be_valid }
context "with valid breed options" do
- subject { Dog.new(:breed => :golden_retriever) }
- it { should be_valid }
- its(:breed) { should be_a(Breed::GoldenRetriever) }
- its('breed.allow_blank') { should be_false }
+ [:golden_retriever, 'golden_retriever', Breed::GoldenRetriever.new].each do |option|
+ subject { Dog.new(:breed => option) }
+ it { should be_valid }
+ its(:breed) { should be_a(Breed::GoldenRetriever) }
+ its('breed.allow_blank') { should be_false }
+ end
end
context "with invalid breed options" do
View
10 spec/classy_enum/collection_spec.rb
@@ -26,6 +26,16 @@ class ClassyEnumCollection::Three < ClassyEnumCollection
end
end
+ context '#<=> (equality)' do
+ its(:first) { should == ClassyEnumCollection::One.new }
+ its(:first) { should == :one }
+ its(:first) { should == 'one' }
+ its(:first) { should_not == :two }
+ its(:first) { should_not == :not_found }
+
+ its(:max) { should == :three }
+ end
+
context '.find, .detect, []' do
let(:expected_enum) { ClassyEnumCollection::Two.new }
Please sign in to comment.
Something went wrong with that request. Please try again.