acts_as_rrranking 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ NjdjYTdkNjRmZTU4NmFiYTEyZjMxZDEzMjZkNjY1YmM4Mzk2YWM4Yg==
5
+ data.tar.gz: !binary |-
6
+ ZWY1YzUxNDNkMjZmZGY5ZWIwMzFjMTg1NTUyZThkNDU3NDk2OWEwYw==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ NjNiYWZmOTcwOTZkN2M1OGQyYjNhM2I0NWE2ZmFjNzk1NmYwNzg1MmM4OWE4
10
+ YjllYjZhNTkzZjkyMmRmNGQxZmVmOGU5ODUwNDQyMzI1MmY4OWM5M2IwYzZi
11
+ MDZjOGFiYjU4MGFjN2U4YTNmODYyMjY0ZDZkNGE2ZWI3YThhZGM=
12
+ data.tar.gz: !binary |-
13
+ ZjM1NDUwZDVjNGNlNGUyN2QwOTY5YTg2MDY5NWMxYzcwMGU4NmY3ZTkxYmI4
14
+ ZWJjMmY0ZjQ0NTg3YTY3NmNmM2QzNTg3NzA5ZTgxNGMwOTY0NjM2YTM2Yjc1
15
+ M2ZhYTQ4ODQzMzQ4YTM1YWRiZWM3MGExYTZlNjZkZjIwOWFjODk=
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2013 Weihu Chen
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,23 @@
1
+ = ActsAsRrranking
2
+
3
+ A rails plugin use redis to sort active_records on real time.
4
+
5
+ == Example
6
+
7
+ class Player < ActiveRecord::Base
8
+ acts_as_rrranking :ranking, score: :game_point
9
+ end
10
+
11
+ == Arguments
12
+
13
+ name: the name of the ranking field
14
+ options:
15
+ score: the field which the models can be sorted by (default: :score)
16
+ id: the identification field of the models (default: :id)
17
+
18
+ Suppose the name is :ranking and the score field is :game_point then the following methods will be generated:
19
+
20
+ #current_ranking: return the current ranking
21
+ #update_ranking(score): update the score to redis, also will be invoked in the after_save hook
22
+ ::top_rankings(limit, offset): return the array of top ids
23
+ ::top_ranking_player(limit, offset): return the array of top models
data/Rakefile ADDED
@@ -0,0 +1,27 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+ RDoc::Task = Rake::RDocTask
13
+ end
14
+
15
+ RDoc::Task.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.title = 'ActsAsRrranking'
18
+ rdoc.options << '--line-numbers'
19
+ rdoc.rdoc_files.include('README.rdoc')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+
24
+
25
+
26
+ Bundler::GemHelper.install_tasks
27
+
@@ -0,0 +1,52 @@
1
+ require 'active_support'
2
+ require 'active_record'
3
+
4
+ module ActsAsRrranking
5
+ extend ActiveSupport::Concern
6
+
7
+ module ClassMethods
8
+ def acts_as_rrranking(ranking_key, options = {})
9
+ @ranking_key = ranking_key
10
+ @redis_ranking_key = "#{self.name.underscore}_#{@ranking_key}"
11
+ @score_key = options[:score] || :score
12
+ @ranking_id = options[:id] || :id
13
+
14
+ class_eval <<-STR
15
+ def update_#{@ranking_key}
16
+ self.class.redis.zadd('#{@redis_ranking_key}', self.#{@score_key}, self.#{@ranking_id})
17
+ end
18
+
19
+ def current_#{@ranking_key}
20
+ if self.#{@score_key} && self.#{@score_key} > 0
21
+ (self.class.redis.zrevrank('#{@redis_ranking_key}', self.#{@ranking_id}) || -1) + 1
22
+ else
23
+ nil
24
+ end
25
+ end
26
+
27
+ after_save :update_#{@ranking_key}
28
+ STR
29
+
30
+ instance_eval <<-STR
31
+ def top_#{@ranking_key.to_s.pluralize}(limit = 10, offset = 0)
32
+ self.redis.zrange('#{@redis_ranking_key}', offset, limit)
33
+ end
34
+
35
+ def top_#{@ranking_key}_#{self.name.underscore.pluralize}(limit = 10, offset = 0)
36
+ ids = self.top_#{@ranking_key.to_s.pluralize}(limit, offset)
37
+ self.where(#{@ranking_id}: ids)
38
+ end
39
+
40
+ def redis_ranking_key
41
+ '#{@redis_ranking_key}'
42
+ end
43
+ STR
44
+ end
45
+
46
+ def redis
47
+ @redis ||= Redis.new
48
+ end
49
+ end
50
+ end
51
+
52
+ ActiveRecord::Base.send :include, ActsAsRrranking
@@ -0,0 +1,3 @@
1
+ module ActsAsRrranking
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: acts_as_rrranking
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Weihu Chen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-05-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 3.2.13
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 3.2.13
27
+ - !ruby/object:Gem::Dependency
28
+ name: redis
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 3.0.1
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: 3.0.1
41
+ - !ruby/object:Gem::Dependency
42
+ name: sqlite3
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec-rails
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: factory_girl_rails
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: A rails plugin use redis to sort active_records on real time.
84
+ email:
85
+ - cctiger36@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - lib/acts_as_rrranking/version.rb
91
+ - lib/acts_as_rrranking.rb
92
+ - MIT-LICENSE
93
+ - Rakefile
94
+ - README.rdoc
95
+ homepage: https://github.com/cctiger36/acts_as_rrranking
96
+ licenses: []
97
+ metadata: {}
98
+ post_install_message:
99
+ rdoc_options: []
100
+ require_paths:
101
+ - lib
102
+ required_ruby_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ! '>='
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ! '>='
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ requirements: []
113
+ rubyforge_project:
114
+ rubygems_version: 2.0.3
115
+ signing_key:
116
+ specification_version: 4
117
+ summary: Redis Realtime Ranking
118
+ test_files: []