redistat 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
data/lib/redistat.rb CHANGED
@@ -1,13 +1,19 @@
1
1
 
2
2
  require 'rubygems'
3
- require 'active_support'
4
- require 'active_support/hash_with_indifferent_access' if !{}.respond_to?(:with_indifferent_access) # Active Support 2.x and 3.x
5
- require 'redis'
6
3
  require 'date'
7
4
  require 'time'
5
+ require 'digest/sha1'
6
+
7
+ # Active Support 2.x or 3.x
8
+ require 'active_support'
9
+ if !{}.respond_to?(:with_indifferent_access)
10
+ require 'active_support/core_ext/hash/indifferent_access'
11
+ require 'active_support/core_ext/hash/reverse_merge'
12
+ end
13
+
8
14
  require 'time_ext'
15
+ require 'redis'
9
16
  require 'json'
10
- require 'digest/sha1'
11
17
 
12
18
  require 'redistat/options'
13
19
  require 'redistat/connection'
@@ -17,7 +23,6 @@ require 'redistat/date'
17
23
  require 'redistat/date_helper'
18
24
  require 'redistat/event'
19
25
  require 'redistat/finder'
20
- require 'redistat/finder/date_set'
21
26
  require 'redistat/key'
22
27
  require 'redistat/label'
23
28
  require 'redistat/model'
@@ -26,10 +31,7 @@ require 'redistat/scope'
26
31
  require 'redistat/summary'
27
32
  require 'redistat/version'
28
33
 
29
- require 'redistat/core_ext/date'
30
- require 'redistat/core_ext/time'
31
- require 'redistat/core_ext/fixnum'
32
- require 'redistat/core_ext/bignum'
34
+ require 'redistat/core_ext'
33
35
 
34
36
  module Redistat
35
37
 
@@ -0,0 +1,5 @@
1
+ require 'redistat/core_ext/bignum'
2
+ require 'redistat/core_ext/date'
3
+ require 'redistat/core_ext/fixnum'
4
+ require 'redistat/core_ext/hash'
5
+ require 'redistat/core_ext/time'
@@ -0,0 +1,23 @@
1
+ class Hash
2
+
3
+ def merge_and_incr(hash)
4
+ self.clone.merge_and_incr!(hash)
5
+ end
6
+
7
+ def merge_and_incr!(hash)
8
+ raise ArgumentError unless hash.is_a?(Hash)
9
+ hash.each do |key, value|
10
+ self[key] = value unless self.set_or_incr(key, value)
11
+ end
12
+ self
13
+ end
14
+
15
+ def set_or_incr(key, value)
16
+ return false unless value.is_a?(Numeric)
17
+ self[key] = 0 unless self.has_key?(key)
18
+ return false unless self[key].is_a?(Numeric)
19
+ self[key] += value
20
+ true
21
+ end
22
+
23
+ end
@@ -1,3 +1,5 @@
1
+ require 'redistat/finder/date_set'
2
+
1
3
  module Redistat
2
4
  class Finder
3
5
  include Database
@@ -5,8 +5,6 @@ module Redistat
5
5
  base.extend(ClassMethods)
6
6
  end
7
7
 
8
- class InvalidDefaultOptions < ArgumentError; end
9
-
10
8
  module ClassMethods
11
9
  def option_accessor(*opts)
12
10
  opts.each do |option|
@@ -12,12 +12,5 @@ module Redistat
12
12
  @till = options[:till] ||= nil
13
13
  end
14
14
 
15
-
16
- def set_or_incr(key, value)
17
- self[key] = 0 if !self.has_key?(key)
18
- self[key] += value
19
- self
20
- end
21
-
22
15
  end
23
- end
16
+ end
@@ -18,10 +18,10 @@ module Redistat
18
18
 
19
19
  if options[:enable_grouping]
20
20
  stats = inject_group_summaries(stats)
21
- key.groups.each { |k|
21
+ key.groups.each do |k|
22
22
  update_key(k, stats, depth_limit, options[:connection_ref])
23
23
  k.update_index if options[:label_indexing]
24
- }
24
+ end
25
25
  else
26
26
  update_key(key, stats, depth_limit, options[:connection_ref])
27
27
  end
@@ -43,6 +43,7 @@ module Redistat
43
43
  end
44
44
 
45
45
  def self.inject_group_summaries!(stats)
46
+ summaries = {}
46
47
  stats.each do |key, value|
47
48
  parts = key.to_s.split(GROUP_SEPARATOR)
48
49
  parts.pop
@@ -51,11 +52,11 @@ module Redistat
51
52
  parts.each do |part|
52
53
  sum_parts << part
53
54
  sum_key = sum_parts.join(GROUP_SEPARATOR)
54
- (stats.has_key?(sum_key)) ? stats[sum_key] += value : stats[sum_key] = value
55
+ (summaries.has_key?(sum_key)) ? summaries[sum_key] += value : summaries[sum_key] = value
55
56
  end
56
57
  end
57
58
  end
58
- stats
59
+ stats.merge_and_incr!(summaries)
59
60
  end
60
61
 
61
62
  def self.inject_group_summaries(stats)
@@ -1,3 +1,3 @@
1
1
  module Redistat
2
- VERSION = "0.2.1"
2
+ VERSION = "0.2.2"
3
3
  end
data/redistat.gemspec CHANGED
@@ -27,5 +27,4 @@ Gem::Specification.new do |s|
27
27
  s.add_development_dependency 'rspec', '>= 2.1.0'
28
28
  s.add_development_dependency 'rcov', '>= 0.9.9'
29
29
  s.add_development_dependency 'yard', '>= 0.6.3'
30
- s.add_development_dependency 'ruby-debug'
31
30
  end
@@ -0,0 +1,30 @@
1
+ require "spec_helper"
2
+
3
+ describe Hash do
4
+
5
+ it "should #set_or_incr values" do
6
+ hash = {:count => 1}
7
+ hash.set_or_incr(:sum, 3).should be_true
8
+ hash.should == {:count => 1, :sum => 3}
9
+ hash.set_or_incr(:count, 4).should be_true
10
+ hash.should == {:count => 5, :sum => 3}
11
+ hash.set_or_incr(:count, 'test').should be_false
12
+ hash.set_or_incr(:view, 'test').should be_false
13
+ hash.should == {:count => 5, :sum => 3}
14
+ hash[:view] = 'test'
15
+ hash.set_or_incr(:view, 3).should be_false
16
+ end
17
+
18
+ it "should #merge_and_incr hashes" do
19
+ hash = { :count => 1, :city => 'hell', :sum => 3, :name => 'john' }
20
+
21
+ new_hash = { :count => 3, :city => 'slum', :views => 2 }
22
+ hash.clone.merge_and_incr(new_hash).should == { :count => 4, :city => 'slum', :views => 2,
23
+ :sum => 3, :name => 'john' }
24
+
25
+ new_hash = { :count => 'six', :city => 'slum', :views => 2, :time => 'late' }
26
+ hash.clone.merge_and_incr(new_hash).should == { :count => 'six', :city => 'slum', :views => 2,
27
+ :sum => 3, :name => 'john', :time => 'late' }
28
+ end
29
+
30
+ end
data/spec/finder_spec.rb CHANGED
@@ -44,8 +44,11 @@ describe Redistat::Finder do
44
44
  finder = Redistat::Finder.depth(:hour)
45
45
  finder.options[:depth].should == :hour
46
46
 
47
- finder = Redistat::Finder.interval(:hour)
48
- finder.options[:interval].should == :hour
47
+ finder = Redistat::Finder.interval(true)
48
+ finder.options[:interval].should be_true
49
+
50
+ finder = Redistat::Finder.interval(false)
51
+ finder.options[:interval].should be_false
49
52
 
50
53
  end
51
54
 
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: redistat
3
3
  version: !ruby/object:Gem::Version
4
- hash: 21
4
+ hash: 19
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 1
10
- version: 0.2.1
9
+ - 2
10
+ version: 0.2.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Jim Myhrberg
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-03-10 00:00:00 +00:00
18
+ date: 2011-03-12 00:00:00 +00:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -130,20 +130,6 @@ dependencies:
130
130
  version: 0.6.3
131
131
  type: :development
132
132
  version_requirements: *id007
133
- - !ruby/object:Gem::Dependency
134
- name: ruby-debug
135
- prerelease: false
136
- requirement: &id008 !ruby/object:Gem::Requirement
137
- none: false
138
- requirements:
139
- - - ">="
140
- - !ruby/object:Gem::Version
141
- hash: 3
142
- segments:
143
- - 0
144
- version: "0"
145
- type: :development
146
- version_requirements: *id008
147
133
  description: A Redis-backed statistics storage and querying library written in Ruby.
148
134
  email:
149
135
  - contact@jimeh.me
@@ -165,9 +151,11 @@ files:
165
151
  - lib/redistat.rb
166
152
  - lib/redistat/collection.rb
167
153
  - lib/redistat/connection.rb
154
+ - lib/redistat/core_ext.rb
168
155
  - lib/redistat/core_ext/bignum.rb
169
156
  - lib/redistat/core_ext/date.rb
170
157
  - lib/redistat/core_ext/fixnum.rb
158
+ - lib/redistat/core_ext/hash.rb
171
159
  - lib/redistat/core_ext/time.rb
172
160
  - lib/redistat/database.rb
173
161
  - lib/redistat/date.rb
@@ -186,6 +174,7 @@ files:
186
174
  - redistat.gemspec
187
175
  - spec/collection_spec.rb
188
176
  - spec/connection_spec.rb
177
+ - spec/core_ext/hash_spec.rb
189
178
  - spec/database_spec.rb
190
179
  - spec/date_spec.rb
191
180
  - spec/db/.emptydir
@@ -233,13 +222,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
233
222
  requirements: []
234
223
 
235
224
  rubyforge_project: redistat
236
- rubygems_version: 1.5.0
225
+ rubygems_version: 1.6.1
237
226
  signing_key:
238
227
  specification_version: 3
239
228
  summary: A Redis-backed statistics storage and querying library written in Ruby.
240
229
  test_files:
241
230
  - spec/collection_spec.rb
242
231
  - spec/connection_spec.rb
232
+ - spec/core_ext/hash_spec.rb
243
233
  - spec/database_spec.rb
244
234
  - spec/date_spec.rb
245
235
  - spec/db/.emptydir