leaderboard 2.0.3 → 2.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.markdown CHANGED
@@ -1,5 +1,9 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## leaderboard 2.0.4 (2012-02-29)
4
+
5
+ * Added `page_for(member, page_size = DEFAULT_PAGE_SIZE)` and `page_for_in(leaderboard_name, member, page_size = DEFAULT_PAGE_SIZE)` calls to allow you to determine the page where a member falls in the leaderboard
6
+
3
7
  ## leaderboard 2.0.3 (2012-02-22)
4
8
 
5
9
  * Added `:reverse => false` to `Leaderboard::DEFAULT_OPTIONS` to support leaderboards sorted from lowest to highest score instead of highest to lowest score. (Thanks @siuying)
data/README.markdown CHANGED
@@ -163,6 +163,7 @@ Get rank and score for an arbitrary list of members (e.g. friends):
163
163
  score_and_rank_for(member): Retrieve the score and rank for a member in a single call
164
164
  remove_members_in_score_range(min_score, max_score): Remove members from the leaderboard within a score range
165
165
  percentile_for(member): Calculate the percentile for a given member
166
+ page_for(member, page_size): Determine the page where a member falls in the leaderboard
166
167
  merge_leaderboards(destination, keys, options = {:aggregate => :min}): Merge leaderboards given by keys with this leaderboard into destination
167
168
  intersect_leaderboards(destination, keys, options = {:aggregate => :min}): Intersect leaderboards given by keys with this leaderboard into destination
168
169
  ```
data/Rakefile CHANGED
@@ -1,8 +1,7 @@
1
- require 'bundler'
2
- Bundler::GemHelper.install_tasks
3
-
1
+ require 'bundler/gem_tasks'
4
2
  require 'rake'
5
3
  require 'rake/testtask'
4
+
6
5
  Rake::TestTask.new(:test) do |test|
7
6
  test.libs << 'lib' << 'test'
8
7
  test.pattern = 'test/**/test_*.rb'
@@ -20,9 +19,6 @@ task :default => :run
20
19
  desc "Run tests and manage server start/stop"
21
20
  task :run => [:start, :test, :stop]
22
21
 
23
- desc "Run rcov and manage server start/stop"
24
- task :rcoverage => [:start, :rcov, :stop]
25
-
26
22
  desc "Start the Redis server"
27
23
  task :start do
28
24
  redis_running = \
data/lib/leaderboard.rb CHANGED
@@ -343,6 +343,37 @@ class Leaderboard
343
343
  percentile
344
344
  end
345
345
  end
346
+
347
+ # Determine the page where a member falls in the leaderboard.
348
+ #
349
+ # @param member [String] Member name.
350
+ # @param page_size [int] Page size to be used in determining page location.
351
+ #
352
+ # @return the page where a member falls in the leaderboard.
353
+ def page_for(member, page_size = DEFAULT_PAGE_SIZE)
354
+ page_for_in(@leaderboard_name, member, page_size)
355
+ end
356
+
357
+ # Determine the page where a member falls in the named leaderboard.
358
+ #
359
+ # @param leaderboard [String] Name of the leaderboard.
360
+ # @param member [String] Member name.
361
+ # @param page_size [int] Page size to be used in determining page location.
362
+ #
363
+ # @return the page where a member falls in the leaderboard.
364
+ def page_for_in(leaderboard_name, member, page_size = DEFAULT_PAGE_SIZE)
365
+ rank_for_member = @reverse ?
366
+ @redis_connection.zrank(leaderboard_name, member) :
367
+ @redis_connection.zrevrank(leaderboard_name, member)
368
+
369
+ if rank_for_member.nil?
370
+ rank_for_member = 0
371
+ else
372
+ rank_for_member += 1
373
+ end
374
+
375
+ (rank_for_member.to_f / page_size.to_f).ceil
376
+ end
346
377
 
347
378
  # Retrieve a page of leaders from the leaderboard.
348
379
  #
@@ -1,3 +1,3 @@
1
1
  class Leaderboard
2
- VERSION = '2.0.3'.freeze
2
+ VERSION = '2.0.4'.freeze
3
3
  end
data/test/test_helper.rb CHANGED
@@ -2,3 +2,18 @@ require 'rubygems'
2
2
  require 'test/unit'
3
3
 
4
4
  require 'leaderboard'
5
+
6
+ class LeaderboardTest < Test::Unit::TestCase
7
+
8
+ def test_version
9
+ assert_equal '2.0.4', Leaderboard::VERSION
10
+ end
11
+
12
+ private
13
+
14
+ def rank_members_in_leaderboard(members_to_add = 5)
15
+ 1.upto(members_to_add) do |index|
16
+ @leaderboard.rank_member("member_#{index}", index)
17
+ end
18
+ end
19
+ end
@@ -1,6 +1,6 @@
1
1
  require 'test_helper'
2
2
 
3
- class TestLeaderboard < Test::Unit::TestCase
3
+ class TestLeaderboard < LeaderboardTest
4
4
  def setup
5
5
  @redis_connection = Redis.new(:host => "127.0.0.1")
6
6
  @leaderboard = Leaderboard.new('name', Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS, :host => "127.0.0.1")
@@ -11,11 +11,7 @@ class TestLeaderboard < Test::Unit::TestCase
11
11
  @leaderboard.disconnect
12
12
  @redis_connection.client.disconnect
13
13
  end
14
-
15
- def test_version
16
- assert_equal '2.0.3', Leaderboard::VERSION
17
- end
18
-
14
+
19
15
  def test_initialize_with_defaults
20
16
  assert_equal 'name', @leaderboard.leaderboard_name
21
17
  assert_equal Leaderboard::DEFAULT_PAGE_SIZE, @leaderboard.page_size
@@ -456,11 +452,19 @@ class TestLeaderboard < Test::Unit::TestCase
456
452
  assert_equal 5.0, @leaderboard.score_for('jones')
457
453
  end
458
454
 
459
- private
460
-
461
- def rank_members_in_leaderboard(members_to_add = 5)
462
- 1.upto(members_to_add) do |index|
463
- @leaderboard.rank_member("member_#{index}", index)
464
- end
455
+ def test_page_for
456
+ assert_equal 0, @leaderboard.page_for('jones')
457
+
458
+ rank_members_in_leaderboard(20)
459
+
460
+ assert_equal 1, @leaderboard.page_for('member_17')
461
+ assert_equal 1, @leaderboard.page_for('member_11')
462
+ assert_equal 1, @leaderboard.page_for('member_10')
463
+ assert_equal 1, @leaderboard.page_for('member_1')
464
+
465
+ assert_equal 1, @leaderboard.page_for('member_17', 10)
466
+ assert_equal 1, @leaderboard.page_for('member_11', 10)
467
+ assert_equal 2, @leaderboard.page_for('member_10', 10)
468
+ assert_equal 2, @leaderboard.page_for('member_1', 10)
465
469
  end
466
470
  end
@@ -1,6 +1,6 @@
1
1
  require 'test_helper'
2
2
 
3
- class TestRevLeaderboard < Test::Unit::TestCase
3
+ class TestRevLeaderboard < LeaderboardTest
4
4
  def setup
5
5
  @redis_connection = Redis.new(:host => "127.0.0.1")
6
6
  @leaderboard = Leaderboard.new('name', Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS.merge({:reverse => true}), :host => "127.0.0.1")
@@ -11,11 +11,7 @@ class TestRevLeaderboard < Test::Unit::TestCase
11
11
  @leaderboard.disconnect
12
12
  @redis_connection.client.disconnect
13
13
  end
14
-
15
- def test_version
16
- assert_equal '2.0.3', Leaderboard::VERSION
17
- end
18
-
14
+
19
15
  def test_initialize_with_defaults
20
16
  assert_equal 'name', @leaderboard.leaderboard_name
21
17
  assert_equal Leaderboard::DEFAULT_PAGE_SIZE, @leaderboard.page_size
@@ -456,11 +452,19 @@ class TestRevLeaderboard < Test::Unit::TestCase
456
452
  assert_equal 5.0, @leaderboard.score_for('jones')
457
453
  end
458
454
 
459
- private
460
-
461
- def rank_members_in_leaderboard(members_to_add = 5)
462
- 1.upto(members_to_add) do |index|
463
- @leaderboard.rank_member("member_#{index}", index)
464
- end
455
+ def test_page_for
456
+ assert_equal 0, @leaderboard.page_for('jones')
457
+
458
+ rank_members_in_leaderboard(20)
459
+
460
+ assert_equal 1, @leaderboard.page_for('member_17')
461
+ assert_equal 1, @leaderboard.page_for('member_11')
462
+ assert_equal 1, @leaderboard.page_for('member_10')
463
+ assert_equal 1, @leaderboard.page_for('member_1')
464
+
465
+ assert_equal 1, @leaderboard.page_for('member_10', 10)
466
+ assert_equal 1, @leaderboard.page_for('member_1', 10)
467
+ assert_equal 2, @leaderboard.page_for('member_17', 10)
468
+ assert_equal 2, @leaderboard.page_for('member_11', 10)
465
469
  end
466
470
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: leaderboard
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.3
4
+ version: 2.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-02-22 00:00:00.000000000 Z
12
+ date: 2012-02-29 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: redis
16
- requirement: &2157419300 !ruby/object:Gem::Requirement
16
+ requirement: &70133210061060 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *2157419300
24
+ version_requirements: *70133210061060
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rake
27
- requirement: &2157418880 !ruby/object:Gem::Requirement
27
+ requirement: &70133210060620 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *2157418880
35
+ version_requirements: *70133210060620
36
36
  description: Leaderboards backed by Redis in Ruby
37
37
  email:
38
38
  - dczarnecki@agoragames.com
@@ -40,7 +40,6 @@ executables: []
40
40
  extensions: []
41
41
  extra_rdoc_files: []
42
42
  files:
43
- - .document
44
43
  - .gitignore
45
44
  - .rvmrc
46
45
  - CHANGELOG.markdown
@@ -70,7 +69,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
70
69
  version: '0'
71
70
  segments:
72
71
  - 0
73
- hash: 2153712714897513648
72
+ hash: 182894351603387394
74
73
  required_rubygems_version: !ruby/object:Gem::Requirement
75
74
  none: false
76
75
  requirements:
@@ -79,7 +78,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
79
78
  version: '0'
80
79
  segments:
81
80
  - 0
82
- hash: 2153712714897513648
81
+ hash: 182894351603387394
83
82
  requirements: []
84
83
  rubyforge_project: leaderboard
85
84
  rubygems_version: 1.8.10
data/.document DELETED
@@ -1,5 +0,0 @@
1
- lib/**/*.rb
2
- bin/*
3
- -
4
- features/**/*.feature
5
- LICENSE.txt