ranking-simpledb 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ gem "aws-sdk"
9
+ group :development do
10
+ gem "rspec", "~> 2.3.0"
11
+ gem "bundler", "~> 1.0.0"
12
+ gem "jeweler", "~> 1.6.4"
13
+ gem "rcov", ">= 0"
14
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,42 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ aws-sdk (1.2.3)
5
+ httparty (~> 0.7)
6
+ json (~> 1.4)
7
+ nokogiri (>= 1.4.4)
8
+ uuidtools (~> 2.1)
9
+ diff-lcs (1.1.3)
10
+ git (1.2.5)
11
+ httparty (0.8.1)
12
+ multi_json
13
+ multi_xml
14
+ jeweler (1.6.4)
15
+ bundler (~> 1.0)
16
+ git (>= 1.2.5)
17
+ rake
18
+ json (1.6.1)
19
+ multi_json (1.0.3)
20
+ multi_xml (0.4.1)
21
+ nokogiri (1.5.0)
22
+ rake (0.9.2.2)
23
+ rcov (0.9.11)
24
+ rspec (2.3.0)
25
+ rspec-core (~> 2.3.0)
26
+ rspec-expectations (~> 2.3.0)
27
+ rspec-mocks (~> 2.3.0)
28
+ rspec-core (2.3.1)
29
+ rspec-expectations (2.3.0)
30
+ diff-lcs (~> 1.1.2)
31
+ rspec-mocks (2.3.0)
32
+ uuidtools (2.1.2)
33
+
34
+ PLATFORMS
35
+ ruby
36
+
37
+ DEPENDENCIES
38
+ aws-sdk
39
+ bundler (~> 1.0.0)
40
+ jeweler (~> 1.6.4)
41
+ rcov
42
+ rspec (~> 2.3.0)
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Takeshi Morita
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,47 @@
1
+ = ranking-simpledb
2
+
3
+ create,update,delete,fetch(rank of user,rank of score,list of near score,toplist,pagenate) ranking data on amazon simple db.
4
+
5
+ See spec/ranking-simpledb_spec.rb
6
+
7
+ === sample
8
+ ranking = RankingSimpledb.new(ENV["AWS_ACCESS_KEY"],ENV["AWS_SECRET_ACCESS_KEY"],ranking_name)
9
+ num_win = 1
10
+ num_win2 = 2
11
+ u_id = 1000
12
+ u_id2 = 1001
13
+ u_id3 = 1003
14
+ ranking.upsert(@u_id,@num_win)
15
+ ranking.upsert(@u_id2,@num_win)
16
+ ranking.upsert(@u_id3,@num_win2)
17
+ ranking.upsert(@u_id2,@num_win2)
18
+ ranking.rank_by_u_id(@u_id) => 3
19
+ ranking.rank_by_u_id(@u_id2) => 1
20
+ ranking.rank_by_u_id(@u_id3) => 1
21
+ ranking.rank_by_score(@num_win2) => 1
22
+ ranking.rank_by_score(@num_win) => 3
23
+ rank.top() => {"score"=>2,"id_score" => "000000000002000000000003,"u_id" => 3,"rank" => 1}
24
+ ranking.top_list(2) =>
25
+ [ {"score"=>2,"id_score" => "000000000002000000000003,"u_id" => 3,"rank" => 1},{"score"=>2,"id_score" => "000000000002000000000002,"u_id" => 2,"rank" => 1}]
26
+ ranking.next_list(2,"000000000002000000000003") =>
27
+ [ {"score"=>2,"id_score" => "000000000002000000000002,"u_id" => 2,"rank" => 1},{"score"=>1,"id_score" => "000000000001000000000001,"u_id" => 1,"rank" => 3}]
28
+ ranking.next_list(2,"000000000002000000000002") =>
29
+ [ {"score"=>1,"id_score" => "000000000001000000000001,"u_id" => 1,"rank" => 3}]
30
+ ranking.prev_list(2,"000000000002000000000002") => [ {"score"=>2,"id_score" => "000000000002000000000003,"u_id" => 3,"rank" => 1}]
31
+ ranking.my_list(2,"000000000002000000000002") =>
32
+ [ {"score"=>2,"id_score" => "000000000002000000000002,"u_id" => 2,"rank" => 1},{"score"=>1,"id_score" => "000000000001000000000001,"u_id" => 1,"rank" => 3}]
33
+
34
+ == Contributing to ranking-simpledb
35
+
36
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
37
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
38
+ * Fork the project
39
+ * Start a feature/bugfix branch
40
+ * Commit and push until you are happy with your contribution
41
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
42
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
43
+
44
+ == Copyright
45
+
46
+ Copyright (c) 2011 Takeshi Morita. See LICENSE.txt for
47
+ further details.
data/Rakefile ADDED
@@ -0,0 +1,49 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "ranking-simpledb"
18
+ gem.homepage = "http://github.com/takeshy/ranking-simpledb"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{library for ranking using ranking-simpledb}
21
+ gem.description = %Q{you can create and update and delete and fetch(score of user,rank of user,pagenate) ranking_data on amazon simpledb.}
22
+ gem.email = "laten@nifty.com"
23
+ gem.authors = ["Takeshi Morita"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec) do |spec|
31
+ spec.pattern = FileList['spec/**/*_spec.rb']
32
+ end
33
+
34
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
35
+ spec.pattern = 'spec/**/*_spec.rb'
36
+ spec.rcov = true
37
+ end
38
+
39
+ task :default => :spec
40
+
41
+ require 'rake/rdoctask'
42
+ Rake::RDocTask.new do |rdoc|
43
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
44
+
45
+ rdoc.rdoc_dir = 'rdoc'
46
+ rdoc.title = "ranking-simpledb #{version}"
47
+ rdoc.rdoc_files.include('README*')
48
+ rdoc.rdoc_files.include('lib/**/*.rb')
49
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,189 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'aws-sdk'
3
+ class RankingSimpledb
4
+ attr :domain
5
+
6
+ #
7
+ # change integer score to string score
8
+ # スコアの文字列化(文字列比較の場合に整数順になるようゼロパディング)
9
+ #
10
+ def self.score_str(score)
11
+ if score.is_a? Integer
12
+ sprintf("%012d",score)
13
+ else
14
+ score
15
+ end
16
+ end
17
+
18
+ #
19
+ # score + user_id for sort
20
+ # スコア+ユーザIDの文字列化(同じスコアの場合はユーザIDが大きい順に並べるため)
21
+ #
22
+ def self.to_id_score(score,u_id)
23
+ sprintf("%s%012d",score_str(score),u_id)
24
+ end
25
+
26
+ #
27
+ # formatize for items
28
+ # itemsにセットするレコード形式を作成
29
+ #
30
+ def self.to_rec(u_id,score)
31
+ str_score = score_str(score)
32
+ {"score" => score_str(score),"id_score" => to_id_score(score,u_id)}
33
+ end
34
+
35
+ #
36
+ # create domain
37
+ # domain(tableみたいなもの)を作成
38
+ #
39
+ def initialize(access_key,secret_access_key,ranking_name)
40
+ AWS.config({
41
+ :access_key_id => access_key,
42
+ :secret_access_key => secret_access_key,
43
+ })
44
+ db = AWS::SimpleDB.new()
45
+ @domain = db.domains.create(ranking_name)
46
+ end
47
+
48
+ #
49
+ # update ranking data. if u_id does not exist, create new data. if u_id exists in ranking, override new score
50
+ # 同じ主キーがあった場合は、自動的に書き換えられる
51
+ #
52
+ def upsert(u_id,score)
53
+ @domain.items.create(u_id.to_s,self.class.to_rec(u_id,score))
54
+ end
55
+
56
+ #
57
+ # delete ranking data specified by u_id
58
+ # 指定したレコードの削除
59
+ #
60
+ def delete(u_id)
61
+ @domain.items[u_id.to_s].delete
62
+ end
63
+
64
+ # rank of u_id
65
+ # 指定したユーザIDの順位
66
+ #
67
+ def rank_of_u_id(u_id)
68
+ score = score_of_u_id(u_id)
69
+ rank_by_score(score)
70
+ end
71
+
72
+ # score of u_id
73
+ # 指定したユーザIDのスコア
74
+ #
75
+ def score_of_u_id(u_id)
76
+ @domain.items[u_id.to_s].attributes["score"].values[0]
77
+ end
78
+
79
+ #
80
+ # rank by score
81
+ # 指定したスコアの順位
82
+ #
83
+ def rank_by_score(score)
84
+ return @domain.items.count + 1 unless score
85
+ @domain.items.where("score > \"#{self.class.score_str(score)}\"").count + 1
86
+ end
87
+
88
+ #
89
+ # number of user in the score. same score user.
90
+ # 指定したスコアにいる人数(同位タイ)
91
+ #
92
+ def user_num_of_score(score)
93
+ @domain.items.where(:score => self.class.score_str(score)).count
94
+ end
95
+
96
+ #
97
+ # return ranking top recored
98
+ # 一番高いスコアのランキング情報
99
+ #
100
+ def top
101
+ to_list(@domain.items.where("id_score is not null").order(:id_score,:desc).limit(1))[0]
102
+ end
103
+
104
+ #
105
+ # top (list_num) list.
106
+ # スコアが高い順にlist_num数分表示
107
+ #
108
+ def top_list(list_num)
109
+ to_list(@domain.items.where("id_score is not null").order(:id_score,:desc).limit(list_num))
110
+ end
111
+
112
+ #
113
+ # return list_num record above id_score. if inc is true, include specified id_score_record.
114
+ # use for paginate.
115
+ # list_num数分、指定したスコア+ユーザIDよりも上にいるランキングを取得。incがtrueの場合は、指定したid_scoreのレコードも含める
116
+ #
117
+ def prev_list(list_num,id_score,inc=false)
118
+ if inc
119
+ clause = "id_score >= \"#{id_score}\""
120
+ else
121
+ clause = "id_score > \"#{id_score}\""
122
+ end
123
+ items = @domain.items.where(clause).order(:id_score,:asc).limit(list_num).to_a
124
+ if items.count < list_num
125
+ return top_list(list_num)
126
+ end
127
+ to_list(items.reverse)
128
+ end
129
+
130
+ #
131
+ # return list_num record above id_score. if inc is true, include specified id_score_record.
132
+ # use for paginate.
133
+ # list_num数分、指定したスコア+ユーザIDよりも下にいるランキングを取得。incがtrueの場合は、指定したid_scoreのレコードも含める
134
+ #
135
+ def next_list(list_num,id_score,inc=false)
136
+ if inc
137
+ clause = "id_score <= \"#{id_score}\""
138
+ else
139
+ clause = "id_score < \"#{id_score}\""
140
+ end
141
+ to_list(@domain.items.where(clause).order(:id_score,:desc).limit(list_num))
142
+ end
143
+
144
+ #
145
+ # return list_num recored near u_id's score. include u_id score.
146
+ # list_num数分、指定したユーザとそれ以下のユーザのランキングを取得。
147
+ #
148
+ def my_list(list_num,u_id)
149
+ score = score_of_u_id(u_id)
150
+ next_list(list_num,self.class.to_id_score(score,u_id),true)
151
+ end
152
+
153
+ #
154
+ # change format of item_record [{"u_id"=>user id,"rank"=>rank,score"=>score,"id_score"=>score+u_id(zero padding)}, ...]
155
+ # SimpleDBに登録したレコードを{"u_id"=>ユーザID,"rank"=>順位,"score"=>スコア,"id_score"=>スコア+ユーザID}
156
+ # のフォーマットの配列に変換。
157
+ #
158
+ def to_list(list)
159
+ records = []
160
+ list.each{|elm|
161
+ attrs = {}
162
+ elm.attributes.each_value{|k,v| attrs[k] = v}
163
+ attrs["u_id"] = elm.name.to_i
164
+ records.push(attrs)
165
+ }
166
+ return [] if records.count == 0
167
+ prev_score = records[0]["score"].to_i
168
+ rank = rank_by_score(prev_score)
169
+ count = user_num_of_score(prev_score)
170
+ first_flg = true
171
+ records.each do|entry|
172
+ score = entry["score"].to_i
173
+ id_score = entry["id_score"]
174
+ if score != prev_score
175
+ rank = count + rank
176
+ count = 1
177
+ prev_score = score
178
+ first_flg = false
179
+ else
180
+ unless first_flg
181
+ count += 1
182
+ end
183
+ end
184
+ entry["score"] = score
185
+ entry["rank"] = rank
186
+ end
187
+ records
188
+ end
189
+ end
@@ -0,0 +1,63 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{ranking-simpledb}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Takeshi Morita"]
12
+ s.date = %q{2011-11-12}
13
+ s.description = %q{you can create and update and delete and fetch(score of user,rank of user,pagenate) ranking_data on amazon simpledb.}
14
+ s.email = %q{laten@nifty.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".rspec",
22
+ "Gemfile",
23
+ "Gemfile.lock",
24
+ "LICENSE.txt",
25
+ "README.rdoc",
26
+ "Rakefile",
27
+ "VERSION",
28
+ "lib/ranking-simpledb.rb",
29
+ "ranking-simpledb.gemspec",
30
+ "spec/ranking-simpledb_spec.rb",
31
+ "spec/spec_helper.rb"
32
+ ]
33
+ s.homepage = %q{http://github.com/takeshy/ranking-simpledb}
34
+ s.licenses = ["MIT"]
35
+ s.require_paths = ["lib"]
36
+ s.rubygems_version = %q{1.6.2}
37
+ s.summary = %q{library for ranking using ranking-simpledb}
38
+
39
+ if s.respond_to? :specification_version then
40
+ s.specification_version = 3
41
+
42
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
43
+ s.add_runtime_dependency(%q<aws-sdk>, [">= 0"])
44
+ s.add_development_dependency(%q<rspec>, ["~> 2.3.0"])
45
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
46
+ s.add_development_dependency(%q<jeweler>, ["~> 1.6.4"])
47
+ s.add_development_dependency(%q<rcov>, [">= 0"])
48
+ else
49
+ s.add_dependency(%q<aws-sdk>, [">= 0"])
50
+ s.add_dependency(%q<rspec>, ["~> 2.3.0"])
51
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
52
+ s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
53
+ s.add_dependency(%q<rcov>, [">= 0"])
54
+ end
55
+ else
56
+ s.add_dependency(%q<aws-sdk>, [">= 0"])
57
+ s.add_dependency(%q<rspec>, ["~> 2.3.0"])
58
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
59
+ s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
60
+ s.add_dependency(%q<rcov>, [">= 0"])
61
+ end
62
+ end
63
+
@@ -0,0 +1,124 @@
1
+ # -*- coding: utf-8 -*-
2
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
3
+
4
+ describe RankingSimpledb do
5
+ before(:all) do
6
+ @ranking_name = "num_of_win"
7
+ @rank = RankingSimpledb.new(ENV["AWS_ACCESS_KEY"],ENV["AWS_SECRET_ACCESS_KEY"],@ranking_name)
8
+ @list_num = 10
9
+ @num_win = 1
10
+ @num_win2 = 2
11
+ @num_win3 = 3
12
+ @num_win4 = 4
13
+ @num_win5 = 5
14
+ @num_win6 = 6
15
+ @num_win7 = 7
16
+ @num_win8 = 8
17
+ @num_win9 = 9
18
+ @num_win10 = 10
19
+ @num_win11 = 11
20
+ @u_id = 1001
21
+ @u_id2 = 1002
22
+ @u_id3 = 1003
23
+ @u_id4 = 1004
24
+ @u_id5 = 1005
25
+ @u_id6 = 1006
26
+ @u_id7 = 1007
27
+ @u_id8 = 1008
28
+ @u_id9 = 1009
29
+ @u_id10 = 1010
30
+ @u_id11 = 1011
31
+ @rank.upsert(@u_id,@num_win)
32
+ @rank.upsert(@u_id2,@num_win2)
33
+ @rank.upsert(@u_id3,@num_win2)
34
+ @rank.upsert(@u_id4,@num_win4)
35
+ @rank.upsert(@u_id5,@num_win5)
36
+ @rank.upsert(@u_id6,@num_win6)
37
+ @rank.upsert(@u_id7,@num_win7)
38
+ @rank.upsert(@u_id8,@num_win8)
39
+ @rank.upsert(@u_id9,@num_win9)
40
+ @rank.upsert(@u_id10,@num_win10)
41
+ @rank.upsert(@u_id11,@num_win11)
42
+ @rank.upsert(@u_id4,@num_win11)
43
+ @rank.upsert(@u_id5,@num_win11)
44
+ @rank.upsert(@u_id6,@num_win11)
45
+ @rank.upsert(@u_id7,@num_win11)
46
+ @rank.upsert(@u_id8,@num_win11)
47
+ @rank.upsert(@u_id9,@num_win11)
48
+ @rank.upsert(@u_id10,@num_win11)
49
+ end
50
+ describe "initialize" do
51
+ context "順位が取れること" do
52
+ it { @rank.rank_by_score(@num_win).should == 11}
53
+ it { @rank.rank_by_score(@num_win2).should == 9}
54
+ end
55
+ end
56
+ describe "順位リスト" do
57
+ context "Top10" do
58
+ it { @rank.top_list(@list_num).should == [
59
+ {"score"=>11,"id_score" => RankingSimpledb.to_id_score(11,@u_id11),"u_id" => @u_id11,"rank" => 1},
60
+ {"score"=>11,"id_score" => RankingSimpledb.to_id_score(11,@u_id10),"u_id" => @u_id10,"rank" => 1},
61
+ {"score"=>11,"id_score" => RankingSimpledb.to_id_score(11,@u_id9), "u_id" => @u_id9,"rank" => 1},
62
+ {"score"=>11,"id_score" => RankingSimpledb.to_id_score(11,@u_id8), "u_id" => @u_id8,"rank" => 1},
63
+ {"score"=>11,"id_score" => RankingSimpledb.to_id_score(11,@u_id7), "u_id" => @u_id7,"rank" => 1},
64
+ {"score"=>11,"id_score" => RankingSimpledb.to_id_score(11,@u_id6), "u_id" => @u_id6,"rank" => 1},
65
+ {"score"=>11,"id_score" => RankingSimpledb.to_id_score(11,@u_id5), "u_id" => @u_id5,"rank" => 1},
66
+ {"score"=>11,"id_score" => RankingSimpledb.to_id_score(11,@u_id4), "u_id" => @u_id4,"rank" => 1},
67
+ {"score"=>2, "id_score" => RankingSimpledb.to_id_score(2,@u_id3), "u_id" => @u_id3,"rank" => 9},
68
+ {"score"=>2, "id_score" => RankingSimpledb.to_id_score(2,@u_id2), "u_id" => @u_id2,"rank" => 9},
69
+ ]
70
+ }
71
+ end
72
+ end
73
+ describe "ページング" do
74
+ context "next_list" do
75
+ it{@rank.next_list(@list_num,RankingSimpledb.to_id_score(2,@u_id2)).should == [
76
+ {"score"=>1, "id_score" => RankingSimpledb.to_id_score(1,@u_id),"u_id" => @u_id,"rank" => 11}
77
+ ]}
78
+ end
79
+ context "prev_list" do
80
+ it{@rank.prev_list(@list_num,RankingSimpledb.to_id_score(1,@u_id)).should == [
81
+ {"score"=>11,"id_score" => RankingSimpledb.to_id_score(11,@u_id11),"u_id" => @u_id11,"rank" => 1},
82
+ {"score"=>11,"id_score" => RankingSimpledb.to_id_score(11,@u_id10),"u_id" => @u_id10,"rank" => 1},
83
+ {"score"=>11,"id_score" => RankingSimpledb.to_id_score(11,@u_id9), "u_id" => @u_id9,"rank" => 1},
84
+ {"score"=>11,"id_score" => RankingSimpledb.to_id_score(11,@u_id8), "u_id" => @u_id8,"rank" => 1},
85
+ {"score"=>11,"id_score" => RankingSimpledb.to_id_score(11,@u_id7), "u_id" => @u_id7,"rank" => 1},
86
+ {"score"=>11,"id_score" => RankingSimpledb.to_id_score(11,@u_id6), "u_id" => @u_id6,"rank" => 1},
87
+ {"score"=>11,"id_score" => RankingSimpledb.to_id_score(11,@u_id5), "u_id" => @u_id5,"rank" => 1},
88
+ {"score"=>11,"id_score" => RankingSimpledb.to_id_score(11,@u_id4), "u_id" => @u_id4,"rank" => 1},
89
+ {"score"=>2, "id_score" => RankingSimpledb.to_id_score(2,@u_id3), "u_id" => @u_id3,"rank" => 9},
90
+ {"score"=>2, "id_score" => RankingSimpledb.to_id_score(2,@u_id2), "u_id" => @u_id2,"rank" => 9},
91
+ ]}
92
+ end
93
+ context "my_list" do
94
+ context "上位に3名以上下位に2名以上" do
95
+ it{@rank.my_list(3,@u_id4).should == [
96
+ {"score"=>11,"id_score" => RankingSimpledb.to_id_score(11,@u_id4),"u_id" => @u_id4,"rank" => 1},
97
+ {"score"=>2,"id_score" => RankingSimpledb.to_id_score(2,@u_id3),"u_id" => @u_id3,"rank" => 9},
98
+ {"score"=>2,"id_score" => RankingSimpledb.to_id_score(2,@u_id2), "u_id" => @u_id2,"rank" => 9},
99
+ ]}
100
+ end
101
+ context "下位がいない" do
102
+ it{@rank.my_list(3,@u_id).should == [
103
+ {"score"=>1,"id_score" => RankingSimpledb.to_id_score(1,@u_id),"u_id" => @u_id,"rank" => 11},
104
+ ]}
105
+ end
106
+ end
107
+ end
108
+ describe "更新" do
109
+ before do
110
+ @num_win12 = 12
111
+ @rank.upsert(@u_id3,@num_win12)
112
+ end
113
+ context "単独1位になる" do
114
+ it {@rank.rank_by_score(@num_win12).should == 1}
115
+ it {@rank.rank_of_u_id(@u_id3).should == 1}
116
+ it {@rank.top().should == {"score"=>12, "id_score" => RankingSimpledb.to_id_score(12,@u_id3), "u_id" => @u_id3,"rank" => 1}}
117
+ end
118
+ end
119
+ after(:all) do
120
+ @ranking_name = "num_of_win"
121
+ @rank = RankingSimpledb.new(@ranking_name)
122
+ @rank.domain.delete!
123
+ end
124
+ end
@@ -0,0 +1,12 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'ranking-simpledb'
5
+
6
+ # Requires supporting files with custom matchers and macros, etc,
7
+ # in ./support/ and its subdirectories.
8
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
+
10
+ RSpec.configure do |config|
11
+
12
+ end
metadata ADDED
@@ -0,0 +1,120 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ranking-simpledb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Takeshi Morita
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-11-12 00:00:00.000000000 +09:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: aws-sdk
17
+ requirement: &27498640 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: '0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: *27498640
26
+ - !ruby/object:Gem::Dependency
27
+ name: rspec
28
+ requirement: &27498100 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 2.3.0
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: *27498100
37
+ - !ruby/object:Gem::Dependency
38
+ name: bundler
39
+ requirement: &27497480 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ~>
43
+ - !ruby/object:Gem::Version
44
+ version: 1.0.0
45
+ type: :development
46
+ prerelease: false
47
+ version_requirements: *27497480
48
+ - !ruby/object:Gem::Dependency
49
+ name: jeweler
50
+ requirement: &27496880 !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ~>
54
+ - !ruby/object:Gem::Version
55
+ version: 1.6.4
56
+ type: :development
57
+ prerelease: false
58
+ version_requirements: *27496880
59
+ - !ruby/object:Gem::Dependency
60
+ name: rcov
61
+ requirement: &27496280 !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ! '>='
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ type: :development
68
+ prerelease: false
69
+ version_requirements: *27496280
70
+ description: you can create and update and delete and fetch(score of user,rank of
71
+ user,pagenate) ranking_data on amazon simpledb.
72
+ email: laten@nifty.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files:
76
+ - LICENSE.txt
77
+ - README.rdoc
78
+ files:
79
+ - .document
80
+ - .rspec
81
+ - Gemfile
82
+ - Gemfile.lock
83
+ - LICENSE.txt
84
+ - README.rdoc
85
+ - Rakefile
86
+ - VERSION
87
+ - lib/ranking-simpledb.rb
88
+ - ranking-simpledb.gemspec
89
+ - spec/ranking-simpledb_spec.rb
90
+ - spec/spec_helper.rb
91
+ has_rdoc: true
92
+ homepage: http://github.com/takeshy/ranking-simpledb
93
+ licenses:
94
+ - MIT
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ none: false
101
+ requirements:
102
+ - - ! '>='
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ segments:
106
+ - 0
107
+ hash: -3481954310947593773
108
+ required_rubygems_version: !ruby/object:Gem::Requirement
109
+ none: false
110
+ requirements:
111
+ - - ! '>='
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ requirements: []
115
+ rubyforge_project:
116
+ rubygems_version: 1.6.2
117
+ signing_key:
118
+ specification_version: 3
119
+ summary: library for ranking using ranking-simpledb
120
+ test_files: []