Skip to content

Commit

Permalink
Allow strings and symbols when testing for equality
Browse files Browse the repository at this point in the history
Priority::Low.new == :low  # => true
Priority::Low.new == 'low' # => true
  • Loading branch information
beerlington committed Aug 12, 2012
1 parent 04d3227 commit 63dfc47
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 7 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/classy_enum/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions lib/classy_enum/collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions lib/classy_enum/predicate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
10 changes: 6 additions & 4 deletions spec/classy_enum/active_record_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions spec/classy_enum/collection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down

0 comments on commit 63dfc47

Please sign in to comment.