streak 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,10 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 0.3.0 (2012-07-06)
4
+
5
+ * Use of Redis hashes to store data.
6
+ * Added `remove_all_statistics(id)` method to remove all streak data for a given ID.
7
+
3
8
  ## 0.2.0 (2012-07-02)
4
9
 
5
10
  * You can now pass a custom set of keys to be used in the `aggregate` call if you want to
data/README.md CHANGED
@@ -90,6 +90,8 @@ Streak.reset_statistics('david')
90
90
  Streak.reset_statistics('david', custom_keys.values)
91
91
  ```
92
92
 
93
+ You can remove all data for a given ID using the `remove_all_statistics(id)` method.
94
+
93
95
  ## Contributing
94
96
 
95
97
  1. Fork it
@@ -15,35 +15,35 @@ module Streak
15
15
  def aggregate(id, count, keys = keys_for_aggregate)
16
16
  if count >= 0
17
17
  previous_data = Streak.redis.multi do |transaction|
18
- transaction.get("#{Streak.namespace}:#{keys[:positive_key]}:#{id}")
19
- transaction.get("#{Streak.namespace}:#{keys[:positive_streak_key]}:#{id}")
18
+ transaction.hget("#{Streak.namespace}:#{id}", keys[:positive_key])
19
+ transaction.hget("#{Streak.namespace}:#{id}", keys[:positive_streak_key])
20
20
  end
21
21
 
22
22
  previous_wins = previous_data[0].to_i
23
23
  previous_streak = previous_data[1].to_i
24
24
 
25
25
  Streak.redis.multi do |transaction|
26
- transaction.set("#{Streak.namespace}:#{keys[:positive_streak_key]}:#{id}", [previous_wins + count, previous_streak].max)
27
- transaction.incrby("#{Streak.namespace}:#{keys[:positive_key]}:#{id}", count.abs)
28
- transaction.incrby("#{Streak.namespace}:#{keys[:positive_total_key]}:#{id}", count.abs)
29
- transaction.set("#{Streak.namespace}:#{keys[:negative_key]}:#{id}", 0)
30
- transaction.incrby("#{Streak.namespace}:#{keys[:total_key]}:#{id}", count.abs)
26
+ transaction.hset("#{Streak.namespace}:#{id}", keys[:positive_streak_key], [previous_wins + count, previous_streak].max)
27
+ transaction.hincrby("#{Streak.namespace}:#{id}", keys[:positive_key], count.abs)
28
+ transaction.hincrby("#{Streak.namespace}:#{id}", keys[:positive_total_key], count.abs)
29
+ transaction.hset("#{Streak.namespace}:#{id}", keys[:negative_key], 0)
30
+ transaction.hincrby("#{Streak.namespace}:#{id}", keys[:total_key], count.abs)
31
31
  end
32
32
  else
33
33
  previous_data = Streak.redis.multi do |transaction|
34
- transaction.get("#{Streak.namespace}:#{keys[:negative_key]}:#{id}")
35
- transaction.get("#{Streak.namespace}:#{keys[:negative_streak_key]}:#{id}")
34
+ transaction.hget("#{Streak.namespace}:#{id}", keys[:negative_key])
35
+ transaction.hget("#{Streak.namespace}:#{id}", keys[:negative_streak_key])
36
36
  end
37
37
 
38
38
  previous_losses = previous_data[0].to_i
39
39
  previous_streak = previous_data[1].to_i
40
40
 
41
41
  Streak.redis.multi do |transaction|
42
- transaction.set("#{Streak.namespace}:#{keys[:negative_streak_key]}:#{id}", [previous_losses + count.abs, previous_streak].max)
43
- transaction.incrby("#{Streak.namespace}:#{keys[:negative_key]}:#{id}", count.abs)
44
- transaction.incrby("#{Streak.namespace}:#{keys[:negative_total_key]}:#{id}", count.abs)
45
- transaction.set("#{Streak.namespace}:#{keys[:positive_key]}:#{id}", 0)
46
- transaction.incrby("#{Streak.namespace}:#{keys[:total_key]}:#{id}", count.abs)
42
+ transaction.hset("#{Streak.namespace}:#{id}", keys[:negative_streak_key], [previous_losses + count.abs, previous_streak].max)
43
+ transaction.hincrby("#{Streak.namespace}:#{id}", keys[:negative_key], count.abs)
44
+ transaction.hincrby("#{Streak.namespace}:#{id}", keys[:negative_total_key], count.abs)
45
+ transaction.hset("#{Streak.namespace}:#{id}", keys[:positive_key], 0)
46
+ transaction.hincrby("#{Streak.namespace}:#{id}", keys[:total_key], count.abs)
47
47
  end
48
48
  end
49
49
  end
@@ -62,7 +62,7 @@ module Streak
62
62
  def statistics(id, keys = [Streak.positive_key, Streak.positive_total_key, Streak.positive_streak_key, Streak.negative_key, Streak.negative_total_key, Streak.negative_streak_key, Streak.total_key])
63
63
  values = Streak.redis.multi do |transaction|
64
64
  keys.each do |key|
65
- transaction.get("#{Streak.namespace}:#{key}:#{id}")
65
+ transaction.hget("#{Streak.namespace}:#{id}", key)
66
66
  end
67
67
  end.map(&:to_i)
68
68
 
@@ -76,11 +76,18 @@ module Streak
76
76
  def reset_statistics(id, keys = [Streak.positive_key, Streak.positive_total_key, Streak.positive_streak_key, Streak.negative_key, Streak.negative_total_key, Streak.negative_streak_key, Streak.total_key])
77
77
  Streak.redis.multi do |transaction|
78
78
  keys.each do |key|
79
- transaction.set("#{Streak.namespace}:#{key}:#{id}", 0)
79
+ transaction.hset("#{Streak.namespace}:#{id}", key, 0)
80
80
  end
81
81
  end
82
82
  end
83
83
 
84
+ # Remove all the statistics for a given +id+.
85
+ #
86
+ # @param id [String] ID.
87
+ def remove_all_statistics(id)
88
+ Streak.redis.del("#{Streak.namespace}:#{id}")
89
+ end
90
+
84
91
  private
85
92
 
86
93
  def keys_for_aggregate
@@ -1,3 +1,3 @@
1
1
  module Streak
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -21,6 +21,6 @@ RSpec.configure do |config|
21
21
  end
22
22
 
23
23
  def streak_value_for(key, id)
24
- Streak.redis.get("#{Streak.namespace}:#{key}:#{id}").to_i
24
+ Streak.redis.hget("#{Streak.namespace}:#{id}", key).to_i
25
25
  end
26
26
  end
@@ -110,4 +110,20 @@ describe Streak::Collector do
110
110
  Streak.statistics('david', custom_keys.values).should == {:kills => 0, :kills_total => 0, :kills_streak => 0, :deaths => 0, :deaths_total => 0, :deaths_streak => 0, :kills_deaths_total => 0}
111
111
  end
112
112
  end
113
+
114
+ describe '#remove_all_statistics' do
115
+ it 'should remove all the statistics for a given ID' do
116
+ Streak.aggregate('david', 3)
117
+ Streak.aggregate('david', -2)
118
+ Streak.aggregate('david', 5)
119
+ Streak.aggregate('david', -1)
120
+
121
+ Streak.statistics('david').should == {:wins => 0, :wins_total => 8, :wins_streak => 5, :losses => 1, :losses_total => 3, :losses_streak => 2, :total => 11}
122
+
123
+ Streak.remove_all_statistics('david')
124
+ Streak.statistics('david').should == {:wins => 0, :wins_total => 0, :wins_streak => 0, :losses => 0, :losses_total => 0, :losses_streak => 0, :total => 0}
125
+
126
+ Streak.redis.exists("#{Streak.namespace}:david").should be_false
127
+ end
128
+ end
113
129
  end
@@ -2,6 +2,6 @@ require 'spec_helper'
2
2
 
3
3
  describe 'Streak::VERSION' do
4
4
  it 'should be the correct version' do
5
- Streak::VERSION.should == '0.2.0'
5
+ Streak::VERSION.should == '0.3.0'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: streak
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
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-07-02 00:00:00.000000000 Z
12
+ date: 2012-07-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: redis
16
- requirement: &70182212219520 !ruby/object:Gem::Requirement
16
+ requirement: &2152469960 !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: *70182212219520
24
+ version_requirements: *2152469960
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rspec
27
- requirement: &70182212218380 !ruby/object:Gem::Requirement
27
+ requirement: &2152469080 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70182212218380
35
+ version_requirements: *2152469080
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rake
38
- requirement: &70182212217580 !ruby/object:Gem::Requirement
38
+ requirement: &2152468280 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70182212217580
46
+ version_requirements: *2152468280
47
47
  description: Streak is a gem for calculating win/loss streaks
48
48
  email:
49
49
  - me@davidczarnecki.com
@@ -80,18 +80,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
80
80
  - - ! '>='
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
- segments:
84
- - 0
85
- hash: 3197324458808714618
86
83
  required_rubygems_version: !ruby/object:Gem::Requirement
87
84
  none: false
88
85
  requirements:
89
86
  - - ! '>='
90
87
  - !ruby/object:Gem::Version
91
88
  version: '0'
92
- segments:
93
- - 0
94
- hash: 3197324458808714618
95
89
  requirements: []
96
90
  rubyforge_project:
97
91
  rubygems_version: 1.8.15