Skip to content
This repository
Browse code

Adding ClassyEnum::Base#last

Behaves more like an array now.
  • Loading branch information...
commit 70c48f76ddff1f8be14232506c96adf3e9794090 1 parent 367aa08
Peter Brown authored
16 lib/classy_enum/collection.rb
@@ -64,6 +64,22 @@ def each
64 64 enum_options.each {|e| yield e.new }
65 65 end
66 66
  67 + # Returns the last enum instance in the collection
  68 + #
  69 + # ==== Example
  70 + # # Create an Enum with some elements
  71 + # class Priority < ClassyEnum::Base
  72 + # end
  73 + #
  74 + # class Priority::Low < Priority; end
  75 + # class Priority::Medium < Priority; end
  76 + # class Priority::High < Priority; end
  77 + #
  78 + # Priority.last # => Priority::High
  79 + def last
  80 + to_a.last
  81 + end
  82 +
67 83 # Finds an enum instance by symbol, string, or block.
68 84 #
69 85 # If a block is given, it passes each entry in enum to block, and returns
6 spec/classy_enum/collection_spec.rb
@@ -26,6 +26,12 @@ class ClassyEnumCollection::Three < ClassyEnumCollection
26 26 end
27 27 end
28 28
  29 + context '.last' do
  30 + its(:last) { should == ClassyEnumCollection::Three.new }
  31 + its(:last) { should == :three }
  32 + its(:last) { should_not == :one }
  33 + end
  34 +
29 35 context '#<=> (equality)' do
30 36 its(:first) { should == ClassyEnumCollection::One.new }
31 37 its(:first) { should == :one }

0 comments on commit 70c48f7

Please sign in to comment.
Something went wrong with that request. Please try again.