ohm-tallyable 0.1.4 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +6 -0
- data/README.md +4 -2
- data/Rakefile +7 -7
- data/lib/ohm/tallyable.rb +46 -36
- data/ohm-tallyable.gemspec +1 -1
- data/test/test-tallyable.rb +21 -0
- metadata +7 -5
- checksums.yaml +0 -15
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -10,8 +10,9 @@ A tally plugin for Ohm
|
|
10
10
|
Setup
|
11
11
|
-----
|
12
12
|
|
13
|
-
1. Include the `Tallyable`
|
13
|
+
1. Include the `Callbacks` and `Tallyable` modules in your model:
|
14
14
|
|
15
|
+
include Ohm::Callbacks
|
15
16
|
include Ohm::Tallyable
|
16
17
|
|
17
18
|
2. Add a tally to your model with the following line:
|
@@ -25,7 +26,8 @@ Usage
|
|
25
26
|
|
26
27
|
To query the tallies, use the `leaderboard` class method.
|
27
28
|
|
28
|
-
Post.leaderboard(:category)
|
29
|
+
>> Post.leaderboard(:category)
|
30
|
+
=> [["Personal", 2], ["Work", 1]]
|
29
31
|
|
30
32
|
|
31
33
|
Advanced Usage
|
data/Rakefile
CHANGED
@@ -11,17 +11,17 @@ task :run => [:start, :test, :stop]
|
|
11
11
|
|
12
12
|
desc "Start the Redis server"
|
13
13
|
task :start do
|
14
|
-
|
15
|
-
|
16
|
-
|
14
|
+
unless File.exists?(REDIS_PID)
|
15
|
+
system "redis-server #{REDIS_CNF}"
|
16
|
+
end
|
17
17
|
end
|
18
18
|
|
19
19
|
desc "Stop the Redis server"
|
20
20
|
task :stop do
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
21
|
+
if File.exists?(REDIS_PID)
|
22
|
+
system "kill #{File.read(REDIS_PID)}"
|
23
|
+
File.delete(REDIS_PID)
|
24
|
+
end
|
25
25
|
end
|
26
26
|
|
27
27
|
Rake::TestTask.new do |t|
|
data/lib/ohm/tallyable.rb
CHANGED
@@ -12,8 +12,14 @@ module Ohm
|
|
12
12
|
@tallies ||= {}
|
13
13
|
end
|
14
14
|
|
15
|
+
def retally(attribute)
|
16
|
+
raise ArgumentError unless tallies.include?(attribute)
|
17
|
+
db.del(*_tally_keys(attribute))
|
18
|
+
all.each { |e| e.send(:_increment_tallies) }
|
19
|
+
end
|
20
|
+
|
15
21
|
def leaderboard(attribute, by=nil)
|
16
|
-
raise ArgumentError
|
22
|
+
raise ArgumentError unless _has_tally(attribute, by)
|
17
23
|
|
18
24
|
_load_zset(_tally_key(attribute, by))
|
19
25
|
.map { |k, v| [k, v.to_i] }
|
@@ -33,42 +39,58 @@ module Ohm
|
|
33
39
|
key
|
34
40
|
end
|
35
41
|
|
36
|
-
|
42
|
+
def _tally_keys(attribute)
|
43
|
+
keys = db.keys(_tally_key(attribute))
|
44
|
+
keys.concat(db.keys(_tally_key(attribute)["*"]))
|
45
|
+
end
|
46
|
+
|
47
|
+
if Redis::VERSION.to_i >= 3
|
37
48
|
def _load_zset(key)
|
38
|
-
key.zrevrange(0, -1, with_scores: true)
|
49
|
+
key.zrevrange(0, -1, with_scores: true)
|
39
50
|
end
|
40
51
|
else
|
41
52
|
def _load_zset(key)
|
42
|
-
key.zrevrange(0, -1, with_scores: true)
|
53
|
+
key.zrevrange(0, -1, with_scores: true).each_slice(2)
|
43
54
|
end
|
44
55
|
end
|
45
56
|
end
|
46
57
|
|
47
|
-
|
48
|
-
|
58
|
+
if Ohm::Contrib::VERSION.to_i >= 1
|
59
|
+
def self.included(model)
|
60
|
+
model.extend(Macros)
|
61
|
+
end
|
62
|
+
|
63
|
+
def before_delete
|
64
|
+
_decrement_tallies
|
65
|
+
super
|
66
|
+
end
|
67
|
+
protected :before_delete
|
68
|
+
|
69
|
+
def before_update
|
70
|
+
_decrement_tallies
|
71
|
+
super
|
72
|
+
end
|
73
|
+
protected :before_update
|
74
|
+
|
75
|
+
def after_save
|
76
|
+
_increment_tallies
|
77
|
+
super
|
78
|
+
end
|
79
|
+
protected :after_save
|
80
|
+
|
81
|
+
else
|
82
|
+
def self.included(model)
|
49
83
|
model.before(:delete, :_decrement_tallies)
|
50
84
|
model.before(:save, :_decrement_tallies)
|
51
85
|
model.after(:save, :_increment_tallies)
|
52
|
-
end
|
53
|
-
model.extend(Macros)
|
54
|
-
end
|
55
86
|
|
56
|
-
|
57
|
-
|
58
|
-
Ohm::Callbacks.protected_instance_methods.include?(:before_save)
|
87
|
+
model.extend(Macros)
|
88
|
+
end
|
59
89
|
end
|
60
90
|
|
61
|
-
|
91
|
+
protected
|
62
92
|
def _decrement_tallies
|
63
|
-
_update_tallies(-1)
|
64
|
-
if respond_to? :read_remote
|
65
|
-
read_remote(attribute)
|
66
|
-
else
|
67
|
-
# ugly, but better than using get and having
|
68
|
-
# to save and restore the old value
|
69
|
-
db.hget(key, attribute)
|
70
|
-
end
|
71
|
-
end
|
93
|
+
_update_tallies(-1) { |attribute| db.hget(key, attribute) }
|
72
94
|
end
|
73
95
|
|
74
96
|
def _increment_tallies
|
@@ -76,6 +98,8 @@ module Ohm
|
|
76
98
|
end
|
77
99
|
|
78
100
|
def _update_tallies(amount, &block)
|
101
|
+
return if new?
|
102
|
+
|
79
103
|
self.class.tallies.each do |attribute, options|
|
80
104
|
by = options[:by] ? {options[:by] => yield(options[:by])} : nil
|
81
105
|
key = self.class._tally_key(attribute, by)
|
@@ -88,19 +112,5 @@ module Ohm
|
|
88
112
|
end
|
89
113
|
end
|
90
114
|
end
|
91
|
-
|
92
|
-
if new_callbacks?
|
93
|
-
def before_delete
|
94
|
-
_decrement_tallies
|
95
|
-
end
|
96
|
-
|
97
|
-
def before_update
|
98
|
-
_decrement_tallies
|
99
|
-
end
|
100
|
-
|
101
|
-
def after_save
|
102
|
-
_increment_tallies
|
103
|
-
end
|
104
|
-
end
|
105
115
|
end
|
106
116
|
end
|
data/ohm-tallyable.gemspec
CHANGED
data/test/test-tallyable.rb
CHANGED
@@ -45,6 +45,15 @@ class TallyableTest < Test::Unit::TestCase
|
|
45
45
|
l = Event.leaderboard(:location)
|
46
46
|
assert_equal [], l
|
47
47
|
end
|
48
|
+
|
49
|
+
def test_retally
|
50
|
+
Event.create(location: "Buenos Aires")
|
51
|
+
Event.create(location: "Rosario")
|
52
|
+
Event.retally(:location)
|
53
|
+
|
54
|
+
l = Event.leaderboard(:location)
|
55
|
+
assert_equal [["Buenos Aires", 2], ["Rosario", 1]], l
|
56
|
+
end
|
48
57
|
end
|
49
58
|
|
50
59
|
class Post < Ohm::Model
|
@@ -97,4 +106,16 @@ class TallyableByTest < Test::Unit::TestCase
|
|
97
106
|
Post.leaderboard(:category, foo: "bar")
|
98
107
|
end
|
99
108
|
end
|
109
|
+
|
110
|
+
def test_retally
|
111
|
+
Post.retally(:category)
|
112
|
+
l = Post.leaderboard(:category, site: "ar")
|
113
|
+
assert_equal [["Personal", 2], ["Work", 1]], l
|
114
|
+
end
|
115
|
+
|
116
|
+
def post_retally_all
|
117
|
+
Post.retally(:category)
|
118
|
+
l = Post.leaderboard(:category, site: "ar")
|
119
|
+
assert_equal [["Personal", 2], ["Work", 1]], l
|
120
|
+
end
|
100
121
|
end
|
metadata
CHANGED
@@ -1,14 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ohm-tallyable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.5
|
5
|
+
prerelease:
|
5
6
|
platform: ruby
|
6
7
|
authors:
|
7
8
|
- Federico Bond
|
8
9
|
autorequire:
|
9
10
|
bindir: bin
|
10
11
|
cert_chain: []
|
11
|
-
date: 2013-08-
|
12
|
+
date: 2013-08-23 00:00:00.000000000 Z
|
12
13
|
dependencies: []
|
13
14
|
description: A tally plugin for Ohm that keeps counts of records for every value of
|
14
15
|
an attribute
|
@@ -30,25 +31,26 @@ files:
|
|
30
31
|
homepage: https://github.com/educabilia/ohm-tallyable
|
31
32
|
licenses:
|
32
33
|
- UNLICENSE
|
33
|
-
metadata: {}
|
34
34
|
post_install_message:
|
35
35
|
rdoc_options: []
|
36
36
|
require_paths:
|
37
37
|
- lib
|
38
38
|
required_ruby_version: !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
39
40
|
requirements:
|
40
41
|
- - ! '>='
|
41
42
|
- !ruby/object:Gem::Version
|
42
43
|
version: '0'
|
43
44
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
45
|
+
none: false
|
44
46
|
requirements:
|
45
47
|
- - ! '>='
|
46
48
|
- !ruby/object:Gem::Version
|
47
49
|
version: '0'
|
48
50
|
requirements: []
|
49
51
|
rubyforge_project:
|
50
|
-
rubygems_version:
|
52
|
+
rubygems_version: 1.8.23
|
51
53
|
signing_key:
|
52
|
-
specification_version:
|
54
|
+
specification_version: 3
|
53
55
|
summary: Ohm Tally Plugin
|
54
56
|
test_files: []
|
checksums.yaml
DELETED
@@ -1,15 +0,0 @@
|
|
1
|
-
---
|
2
|
-
!binary "U0hBMQ==":
|
3
|
-
metadata.gz: !binary |-
|
4
|
-
Yzk4Mzk5MWIwNzE4NDQ4YWU0MTVkNGJiMWE5NzA3ZjlkMDk2ZTkyYw==
|
5
|
-
data.tar.gz: !binary |-
|
6
|
-
MDc1MTIxYmNlNmI4ZTIzZmI5NGIxM2U4OTQyYTliM2ZlYzQ0YjFiZA==
|
7
|
-
!binary "U0hBNTEy":
|
8
|
-
metadata.gz: !binary |-
|
9
|
-
YWNhNWU3NzdkMDFiMDNiMzgxNGMxYjc3NTI4MzA5YWFmMTY0YTgzYTU3ZTVj
|
10
|
-
YmNiMzMzN2ZkMjA1NDRjMzhmNzc5M2Y5N2E3YjYxZWM4ODZiMWQxYjI3NDFk
|
11
|
-
ODk3YTQwMWE3YTk4MGQwN2JjZDg0M2UwMzMzMzhlODliNDMwYjI=
|
12
|
-
data.tar.gz: !binary |-
|
13
|
-
ZTE1YTlmMWY4YmUwMGE3MThiM2M5ZWYzNmNkZGY4ZGZiNDI4OGM4Mzk2NzUz
|
14
|
-
NjlmMzNhYTQ2Y2YzMzVkYzcwZjJkYjJjYmRhYTU4NWRmYzFjZjdhN2ZiM2Vj
|
15
|
-
ZTZkYTVhYmEwOTQxNjI4YjU1ZTA1NTY3MGZiYjhkN2I0YjI4NDI=
|