Permalink
Browse files

Adding ClassyEnum::Base#last

Behaves more like an array now.
  • Loading branch information...
1 parent 367aa08 commit 70c48f76ddff1f8be14232506c96adf3e9794090 @beerlington beerlington committed Feb 8, 2013
Showing with 22 additions and 0 deletions.
  1. +16 −0 lib/classy_enum/collection.rb
  2. +6 −0 spec/classy_enum/collection_spec.rb
View
16 lib/classy_enum/collection.rb
@@ -64,6 +64,22 @@ def each
enum_options.each {|e| yield e.new }
end
+ # Returns the last enum instance in the collection
+ #
+ # ==== Example
+ # # Create an Enum with some elements
+ # class Priority < ClassyEnum::Base
+ # end
+ #
+ # class Priority::Low < Priority; end
+ # class Priority::Medium < Priority; end
+ # class Priority::High < Priority; end
+ #
+ # Priority.last # => Priority::High
+ def last
+ to_a.last
+ end
+
# Finds an enum instance by symbol, string, or block.
#
# If a block is given, it passes each entry in enum to block, and returns
View
6 spec/classy_enum/collection_spec.rb
@@ -26,6 +26,12 @@ class ClassyEnumCollection::Three < ClassyEnumCollection
end
end
+ context '.last' do
+ its(:last) { should == ClassyEnumCollection::Three.new }
+ its(:last) { should == :three }
+ its(:last) { should_not == :one }
+ end
+
context '#<=> (equality)' do
its(:first) { should == ClassyEnumCollection::One.new }
its(:first) { should == :one }

0 comments on commit 70c48f7

Please sign in to comment.