Permalink
Browse files

Add #top_member and #bottom_member convenience methods.

  • Loading branch information...
1 parent 9374a01 commit 26481fd0ad864685b3b47e0ff342cb53597f445d @hypomodern committed Aug 6, 2012
Showing with 29 additions and 1 deletion.
  1. +15 −1 lib/leaderboard.rb
  2. +14 −0 spec/leaderboard_spec.rb
View
@@ -675,7 +675,21 @@ def member_at_in(leaderboard_name, position, options = {})
leaders[offset] if leaders
end
end
-
+
+ # Retrieve the first member from the leaderboard, with member data
+ #
+ # @return the top item in the leaderboard
+ def top_member(options = {})
+ member_at(1, { :with_member_data => true }.merge(options))
+ end
+
+ # Retrieve the last member from the leaderboard, with member data
+ #
+ # @return the bottom item in the leaderboard
+ def bottom_member(options = {})
+ member_at(total_members, { :with_member_data => true }.merge(options))
+ end
+
# Retrieve a page of leaders from the leaderboard around a given member.
#
# @param member [String] Member name.
@@ -272,6 +272,20 @@
@leaderboard.member_at(1, :use_zero_index_for_rank => true)[:rank].should == 0
end
+ it 'should return the first member when calling #top_member' do
+ rank_members_in_leaderboard(10)
+ top = @leaderboard.top_member
+ top[:rank].should == 1
+ top[:member_data].should == { 'member_name' => 'Leaderboard member 10' }
+ end
+
+ it 'should return the last member when calling #bottom_member' do
+ rank_members_in_leaderboard(10)
+ bot = @leaderboard.bottom_member
+ bot[:rank].should == 10
+ bot[:member_data].should == { 'member_name' => 'Leaderboard member 1' }
+ end
+
it 'should return the correct information when calling around_me' do
rank_members_in_leaderboard(Leaderboard::DEFAULT_PAGE_SIZE * 3 + 1)

0 comments on commit 26481fd

Please sign in to comment.