streak 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,10 +1,15 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 0.2.0 (2012-07-02)
4
+
5
+ * You can now pass a custom set of keys to be used in the `aggregate` call if you want to
6
+ use a different set of positive/negative things than what is setup in the configuration.
7
+
3
8
  ## 0.1.0
4
9
 
5
10
  * Key-space is now separated by a single `:` instead of `::`
6
11
 
7
- ## 0.0.2
12
+ ## 0.0.2
8
13
 
9
14
  * Fix +total_key+ configuration.
10
15
 
data/README.md CHANGED
@@ -46,14 +46,48 @@ Streak.aggregate('david', 5) # 5 wins
46
46
  Streak.aggregate('david', -1) # 1 loss
47
47
 
48
48
  Streak.statistics('david')
49
- => {:wins=>0, :wins_total=>8, :wins_streak=>5, :losses=>1, :losses_total=>3, :losses_streak=>2, :total=>11}
49
+ => {:wins=>0, :wins_total=>8, :wins_streak=>5, :losses=>1, :losses_total=>3, :losses_streak=>2, :total=>11}
50
50
 
51
51
  Streak.statistics('david', [Streak.positive_streak_key, Streak.negative_streak_key])
52
52
  => {:wins_streak=>5, :losses_streak=>2}
53
53
 
54
54
  Streak.reset_statistics('david')
55
55
  Streak.statistics('david')
56
- => {:wins=>0, :wins_total=>0, :wins_streak=>0, :losses=>0, :losses_total=>0, :losses_streak=>0, :total=>0}
56
+ => {:wins=>0, :wins_total=>0, :wins_streak=>0, :losses=>0, :losses_total=>0, :losses_streak=>0, :total=>0}
57
+ ```
58
+
59
+ You can also pass a custom set of keys to be used in the `aggregate` call if you want to
60
+ use a different set of positive/negative things than what is setup in the configuration.
61
+ Below is a complete example:
62
+
63
+ ```ruby
64
+ custom_keys = {
65
+ :positive_key => 'kills',
66
+ :positive_total_key => 'kills_total',
67
+ :positive_streak_key => 'kills_streak',
68
+ :negative_key => 'deaths',
69
+ :negative_total_key => 'deaths_total',
70
+ :negative_streak_key => 'deaths_streak',
71
+ :total_key => 'kills_deaths_total'
72
+ }
73
+
74
+ Streak.aggregate('david', 1, custom_keys)
75
+ Streak.aggregate('david', -7, custom_keys)
76
+ Streak.aggregate('david', 6, custom_keys)
77
+ Streak.aggregate('david', -3, custom_keys)
78
+
79
+ Streak.aggregate('david', 3)
80
+ Streak.aggregate('david', -2)
81
+ Streak.aggregate('david', 5)
82
+ Streak.aggregate('david', -1)
83
+
84
+ Streak.statistics('david')
85
+ => {:wins=>0, :wins_total=>8, :wins_streak=>5, :losses=>1, :losses_total=>3, :losses_streak=>2, :total=>11}
86
+ Streak.statistics('david', custom_keys.values)
87
+ => {:kills=>0, :kills_total=>7, :kills_streak=>6, :deaths=>3, :deaths_total=>10, :deaths_streak=>7, :kills_deaths_total=>17}
88
+
89
+ Streak.reset_statistics('david')
90
+ Streak.reset_statistics('david', custom_keys.values)
57
91
  ```
58
92
 
59
93
  ## Contributing
@@ -1,58 +1,59 @@
1
1
  module Streak
2
2
  module Collector
3
- # Aggregate streaks for a given +id+. If +count+ is greater than 0, it will increment +Streak.positive_key+ and
4
- # +Streak.positive_total_key+ by the absolute value of count. It will zero out +Streak.negative_key+. Finally, it
5
- # will add the absolute value of count to +Streak.total_key+. If the current positive streak is greater than
6
- # the value of +Streak.positive_streak_key+, its value will be replaced.
7
- # If +count+ is less than than 0, it will increment +Streak.negative_key+ and
8
- # +Streak.negative_total_key+ by the absolute value of count. It will zero out +Streak.positive_key+. Finally, it
9
- # will add the absolute value of count to +Streak.total_key+. If the current negative streak is greater than
3
+ # Aggregate streaks for a given +id+. If +count+ is greater than 0, it will increment +Streak.positive_key+ and
4
+ # +Streak.positive_total_key+ by the absolute value of count. It will zero out +Streak.negative_key+. Finally, it
5
+ # will add the absolute value of count to +Streak.total_key+. If the current positive streak is greater than
6
+ # the value of +Streak.positive_streak_key+, its value will be replaced.
7
+ # If +count+ is less than than 0, it will increment +Streak.negative_key+ and
8
+ # +Streak.negative_total_key+ by the absolute value of count. It will zero out +Streak.positive_key+. Finally, it
9
+ # will add the absolute value of count to +Streak.total_key+. If the current negative streak is greater than
10
10
  # the value of +Streak.negative_streak_key+, its value will be replaced.
11
11
  #
12
12
  # @param id [String] ID of the item being monitored for a streak.
13
13
  # @param count [Integer] Streak count, which can be positive or negative.
14
- def aggregate(id, count)
14
+ # @param keys [Hash] Keys to be used for aggregating streaks.
15
+ def aggregate(id, count, keys = keys_for_aggregate)
15
16
  if count >= 0
16
17
  previous_data = Streak.redis.multi do |transaction|
17
- transaction.get("#{Streak.namespace}:#{Streak.positive_key}:#{id}")
18
- transaction.get("#{Streak.namespace}:#{Streak.positive_streak_key}:#{id}")
18
+ transaction.get("#{Streak.namespace}:#{keys[:positive_key]}:#{id}")
19
+ transaction.get("#{Streak.namespace}:#{keys[:positive_streak_key]}:#{id}")
19
20
  end
20
21
 
21
22
  previous_wins = previous_data[0].to_i
22
23
  previous_streak = previous_data[1].to_i
23
24
 
24
25
  Streak.redis.multi do |transaction|
25
- transaction.set("#{Streak.namespace}:#{Streak.positive_streak_key}:#{id}", [previous_wins + count, previous_streak].max)
26
- transaction.incrby("#{Streak.namespace}:#{Streak.positive_key}:#{id}", count.abs)
27
- transaction.incrby("#{Streak.namespace}:#{Streak.positive_total_key}:#{id}", count.abs)
28
- transaction.set("#{Streak.namespace}:#{Streak.negative_key}:#{id}", 0)
29
- transaction.incrby("#{Streak.namespace}:#{Streak.total_key}:#{id}", count.abs)
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)
30
31
  end
31
32
  else
32
33
  previous_data = Streak.redis.multi do |transaction|
33
- transaction.get("#{Streak.namespace}:#{Streak.negative_key}:#{id}")
34
- transaction.get("#{Streak.namespace}:#{Streak.negative_streak_key}:#{id}")
34
+ transaction.get("#{Streak.namespace}:#{keys[:negative_key]}:#{id}")
35
+ transaction.get("#{Streak.namespace}:#{keys[:negative_streak_key]}:#{id}")
35
36
  end
36
37
 
37
38
  previous_losses = previous_data[0].to_i
38
39
  previous_streak = previous_data[1].to_i
39
40
 
40
41
  Streak.redis.multi do |transaction|
41
- transaction.set("#{Streak.namespace}:#{Streak.negative_streak_key}:#{id}", [previous_losses + count.abs, previous_streak].max)
42
- transaction.incrby("#{Streak.namespace}:#{Streak.negative_key}:#{id}", count.abs)
43
- transaction.incrby("#{Streak.namespace}:#{Streak.negative_total_key}:#{id}", count.abs)
44
- transaction.set("#{Streak.namespace}:#{Streak.positive_key}:#{id}", 0)
45
- transaction.incrby("#{Streak.namespace}:#{Streak.total_key}:#{id}", count.abs)
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)
46
47
  end
47
48
  end
48
49
  end
49
50
 
50
- # Retrieve all (or some) of the streak statistics collected. By default, without a second parameter, this
51
- # method will return a +Hash+ of: +Streak.positive_key+, +Streak.positive_total_key+, +Streak.positive_streak_key+,
51
+ # Retrieve all (or some) of the streak statistics collected. By default, without a second parameter, this
52
+ # method will return a +Hash+ of: +Streak.positive_key+, +Streak.positive_total_key+, +Streak.positive_streak_key+,
52
53
  # +Streak.negative_key+, +Streak.negative_total_key+, +Streak.negative_streak_key+, and +Streak.total_key+ with
53
54
  # their corresponding values. If you want a subset of that list, pass in an array with the keys you want
54
55
  # returned.
55
- #
56
+ #
56
57
  # @param id [String] ID.
57
58
  # @param keys [Array, optional]. Optional list of streak statistic keys to be retrieved.
58
59
  #
@@ -71,12 +72,27 @@ module Streak
71
72
  # Reset all the statistics for a given +id+ to 0.
72
73
  #
73
74
  # @param id [String] ID.
74
- def reset_statistics(id)
75
+ # @param keys [Array] List of keys to zero-out.
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])
75
77
  Streak.redis.multi do |transaction|
76
- [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].each do |key|
78
+ keys.each do |key|
77
79
  transaction.set("#{Streak.namespace}:#{key}:#{id}", 0)
78
80
  end
79
81
  end
80
82
  end
83
+
84
+ private
85
+
86
+ def keys_for_aggregate
87
+ {
88
+ :positive_key => Streak.positive_key,
89
+ :positive_total_key => Streak.positive_total_key,
90
+ :positive_streak_key => Streak.positive_streak_key,
91
+ :negative_key => Streak.negative_key,
92
+ :negative_total_key => Streak.negative_total_key,
93
+ :negative_streak_key => Streak.negative_streak_key,
94
+ :total_key => Streak.total_key
95
+ }
96
+ end
81
97
  end
82
98
  end
@@ -1,3 +1,3 @@
1
1
  module Streak
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -29,7 +29,7 @@ describe Streak::Collector do
29
29
  streak_value_for(Streak.negative_key, 'david').should == 0
30
30
  streak_value_for(Streak.negative_streak_key, 'david').should == 1
31
31
  streak_value_for(Streak.total_key, 'david').should == 6
32
- end
32
+ end
33
33
  end
34
34
 
35
35
  describe '#statistics' do
@@ -64,6 +64,50 @@ describe Streak::Collector do
64
64
  Streak.reset_statistics('david')
65
65
 
66
66
  Streak.statistics('david').should == {:wins => 0, :wins_total => 0, :wins_streak => 0, :losses => 0, :losses_total => 0, :losses_streak => 0, :total => 0}
67
+
68
+ Streak.aggregate('david', 3)
69
+ Streak.aggregate('david', -2)
70
+ Streak.aggregate('david', 5)
71
+ Streak.aggregate('david', -1)
72
+
73
+ Streak.statistics('david').should == {:wins => 0, :wins_total => 8, :wins_streak => 5, :losses => 1, :losses_total => 3, :losses_streak => 2, :total => 11}
74
+
75
+ Streak.reset_statistics('david', [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])
76
+
77
+ Streak.statistics('david').should == {:wins => 0, :wins_total => 0, :wins_streak => 0, :losses => 0, :losses_total => 0, :losses_streak => 0, :total => 0}
78
+ end
79
+ end
80
+
81
+ describe 'custom keys in #aggregate, #statistics and #reset_statistics' do
82
+ it 'should allow you to use custom keys different from the configured keys' do
83
+ custom_keys = {
84
+ :positive_key => 'kills',
85
+ :positive_total_key => 'kills_total',
86
+ :positive_streak_key => 'kills_streak',
87
+ :negative_key => 'deaths',
88
+ :negative_total_key => 'deaths_total',
89
+ :negative_streak_key => 'deaths_streak',
90
+ :total_key => 'kills_deaths_total'
91
+ }
92
+
93
+ Streak.aggregate('david', 1, custom_keys)
94
+ Streak.aggregate('david', -7, custom_keys)
95
+ Streak.aggregate('david', 6, custom_keys)
96
+ Streak.aggregate('david', -3, custom_keys)
97
+
98
+ Streak.aggregate('david', 3)
99
+ Streak.aggregate('david', -2)
100
+ Streak.aggregate('david', 5)
101
+ Streak.aggregate('david', -1)
102
+
103
+ Streak.statistics('david').should == {:wins => 0, :wins_total => 8, :wins_streak => 5, :losses => 1, :losses_total => 3, :losses_streak => 2, :total => 11}
104
+ Streak.statistics('david', custom_keys.values).should == {:kills => 0, :kills_total => 7, :kills_streak => 6, :deaths => 3, :deaths_total => 10, :deaths_streak => 7, :kills_deaths_total => 17}
105
+
106
+ Streak.reset_statistics('david')
107
+ Streak.statistics('david').should == {:wins => 0, :wins_total => 0, :wins_streak => 0, :losses => 0, :losses_total => 0, :losses_streak => 0, :total => 0}
108
+ Streak.statistics('david', custom_keys.values).should == {:kills => 0, :kills_total => 7, :kills_streak => 6, :deaths => 3, :deaths_total => 10, :deaths_streak => 7, :kills_deaths_total => 17}
109
+ Streak.reset_statistics('david', custom_keys.values)
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}
67
111
  end
68
112
  end
69
113
  end
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Streak::VERSION' do
4
+ it 'should be the correct version' do
5
+ Streak::VERSION.should == '0.2.0'
6
+ end
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.1.0
4
+ version: 0.2.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-05-18 00:00:00.000000000 Z
12
+ date: 2012-07-02 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: redis
16
- requirement: &70352190361800 !ruby/object:Gem::Requirement
16
+ requirement: &70182212219520 !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: *70352190361800
24
+ version_requirements: *70182212219520
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rspec
27
- requirement: &70352190361340 !ruby/object:Gem::Requirement
27
+ requirement: &70182212218380 !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: *70352190361340
35
+ version_requirements: *70182212218380
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rake
38
- requirement: &70352190360860 !ruby/object:Gem::Requirement
38
+ requirement: &70182212217580 !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: *70352190360860
46
+ version_requirements: *70182212217580
47
47
  description: Streak is a gem for calculating win/loss streaks
48
48
  email:
49
49
  - me@davidczarnecki.com
@@ -66,6 +66,7 @@ files:
66
66
  - spec/spec_helper.rb
67
67
  - spec/streak/collector_spec.rb
68
68
  - spec/streak/configuration_spec.rb
69
+ - spec/streak/version_spec.rb
69
70
  - streak.gemspec
70
71
  homepage: https://github.com/czarneckid/streak
71
72
  licenses: []
@@ -81,7 +82,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
81
82
  version: '0'
82
83
  segments:
83
84
  - 0
84
- hash: 4141444244431860570
85
+ hash: 3197324458808714618
85
86
  required_rubygems_version: !ruby/object:Gem::Requirement
86
87
  none: false
87
88
  requirements:
@@ -90,10 +91,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
90
91
  version: '0'
91
92
  segments:
92
93
  - 0
93
- hash: 4141444244431860570
94
+ hash: 3197324458808714618
94
95
  requirements: []
95
96
  rubyforge_project:
96
- rubygems_version: 1.8.10
97
+ rubygems_version: 1.8.15
97
98
  signing_key:
98
99
  specification_version: 3
99
100
  summary: Streak is a gem for calculating win/loss streaks
@@ -101,3 +102,4 @@ test_files:
101
102
  - spec/spec_helper.rb
102
103
  - spec/streak/collector_spec.rb
103
104
  - spec/streak/configuration_spec.rb
105
+ - spec/streak/version_spec.rb