From 82119fcf692bf416e00f726bc0fab7c315a4f4d4 Mon Sep 17 00:00:00 2001 From: Jim Myhrberg Date: Fri, 11 Mar 2011 15:38:10 +0000 Subject: [PATCH 1/8] wrong usage of interval method in Finder spec --- spec/finder_spec.rb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/spec/finder_spec.rb b/spec/finder_spec.rb index bf60a29..14e9f5c 100644 --- a/spec/finder_spec.rb +++ b/spec/finder_spec.rb @@ -44,8 +44,11 @@ describe Redistat::Finder do finder = Redistat::Finder.depth(:hour) finder.options[:depth].should == :hour - finder = Redistat::Finder.interval(:hour) - finder.options[:interval].should == :hour + finder = Redistat::Finder.interval(true) + finder.options[:interval].should be_true + + finder = Redistat::Finder.interval(false) + finder.options[:interval].should be_false end From 0938781cd158137fe05926cafd05d92c79d072d1 Mon Sep 17 00:00:00 2001 From: Jim Myhrberg Date: Sat, 12 Mar 2011 21:47:54 +0000 Subject: [PATCH 2/8] extend ::Hash with #set_or_incr and #merge_and_incr methods --- lib/redistat.rb | 5 +---- lib/redistat/core_ext.rb | 5 +++++ lib/redistat/core_ext/hash.rb | 21 +++++++++++++++++++++ lib/redistat/result.rb | 9 +-------- spec/core_ext/hash_spec.rb | 20 ++++++++++++++++++++ 5 files changed, 48 insertions(+), 12 deletions(-) create mode 100644 lib/redistat/core_ext.rb create mode 100644 lib/redistat/core_ext/hash.rb create mode 100644 spec/core_ext/hash_spec.rb diff --git a/lib/redistat.rb b/lib/redistat.rb index 00b8e75..f89bd06 100644 --- a/lib/redistat.rb +++ b/lib/redistat.rb @@ -26,10 +26,7 @@ require 'redistat/scope' require 'redistat/summary' require 'redistat/version' -require 'redistat/core_ext/date' -require 'redistat/core_ext/time' -require 'redistat/core_ext/fixnum' -require 'redistat/core_ext/bignum' +require 'redistat/core_ext' module Redistat diff --git a/lib/redistat/core_ext.rb b/lib/redistat/core_ext.rb new file mode 100644 index 0000000..f794627 --- /dev/null +++ b/lib/redistat/core_ext.rb @@ -0,0 +1,5 @@ +require 'redistat/core_ext/bignum' +require 'redistat/core_ext/date' +require 'redistat/core_ext/fixnum' +require 'redistat/core_ext/hash' +require 'redistat/core_ext/time' \ No newline at end of file diff --git a/lib/redistat/core_ext/hash.rb b/lib/redistat/core_ext/hash.rb new file mode 100644 index 0000000..0cc5390 --- /dev/null +++ b/lib/redistat/core_ext/hash.rb @@ -0,0 +1,21 @@ +class Hash + + def merge_and_incr(hash) + raise ArgumentError unless hash.is_a?(Hash) + hash.each do |key, value| + if value.is_a?(Numeric) + self.set_or_incr(key, value) + else + self[key] = value + end + end + end + + def set_or_incr(key, value) + return self unless value.is_a?(Numeric) + self[key] = 0 unless self.has_key?(key) + self[key] += value if self[key].is_a?(Numeric) + self + end + +end diff --git a/lib/redistat/result.rb b/lib/redistat/result.rb index 777221b..7a91dff 100644 --- a/lib/redistat/result.rb +++ b/lib/redistat/result.rb @@ -12,12 +12,5 @@ module Redistat @till = options[:till] ||= nil end - - def set_or_incr(key, value) - self[key] = 0 if !self.has_key?(key) - self[key] += value - self - end - end -end \ No newline at end of file +end diff --git a/spec/core_ext/hash_spec.rb b/spec/core_ext/hash_spec.rb new file mode 100644 index 0000000..fad64cc --- /dev/null +++ b/spec/core_ext/hash_spec.rb @@ -0,0 +1,20 @@ +require "spec_helper" + +describe Hash do + + it "should #set_or_incr values" do + hash = {:count => 1} + hash.set_or_incr(:sum, 3) + hash.should == {:count => 1, :sum => 3} + hash.set_or_incr(:count, 4) + hash.should == {:count => 5, :sum => 3} + end + + it "should #merge_and_incr hashes" do + hash = {:count => 1, :city => 'hell', :sum => 3, :name => 'john'} + new_hash = {:count => 3, :city => 'slum', :views => 2} + hash.merge_and_incr(new_hash) + hash.should == {:count => 4, :sum => 3, :views => 2, :city => 'slum', :name => 'john'} + end + +end From d5f79b82a9fc31b8c2555cfe3abe8e3ae8c756a8 Mon Sep 17 00:00:00 2001 From: Jim Myhrberg Date: Sat, 12 Mar 2011 21:59:52 +0000 Subject: [PATCH 3/8] somewhat better loading procedure --- lib/redistat.rb | 17 +++++++++++------ lib/redistat/finder.rb | 2 ++ 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/lib/redistat.rb b/lib/redistat.rb index f89bd06..11d63db 100644 --- a/lib/redistat.rb +++ b/lib/redistat.rb @@ -1,14 +1,20 @@ require 'rubygems' -require 'active_support' -require 'active_support/hash_with_indifferent_access' if !{}.respond_to?(:with_indifferent_access) # Active Support 2.x and 3.x -require 'redis' require 'date' require 'time' -require 'time_ext' -require 'json' require 'digest/sha1' +# Active Support 2.x or 3.x +require 'active_support' +if !{}.respond_to?(:with_indifferent_access) + require 'active_support/core_ext/hash/indifferent_access' + require 'active_support/core_ext/hash/reverse_merge' +end + +require 'time_ext' +require 'redis' +require 'json' + require 'redistat/options' require 'redistat/connection' require 'redistat/database' @@ -17,7 +23,6 @@ require 'redistat/date' require 'redistat/date_helper' require 'redistat/event' require 'redistat/finder' -require 'redistat/finder/date_set' require 'redistat/key' require 'redistat/label' require 'redistat/model' diff --git a/lib/redistat/finder.rb b/lib/redistat/finder.rb index 289f0ce..08fe381 100644 --- a/lib/redistat/finder.rb +++ b/lib/redistat/finder.rb @@ -1,3 +1,5 @@ +require 'redistat/finder/date_set' + module Redistat class Finder include Database From dcca3556eaac665a08fcd85a7f94cdb23104cbab Mon Sep 17 00:00:00 2001 From: Jim Myhrberg Date: Sat, 12 Mar 2011 22:00:21 +0000 Subject: [PATCH 4/8] some cleanup --- lib/redistat/options.rb | 2 -- redistat.gemspec | 1 - 2 files changed, 3 deletions(-) diff --git a/lib/redistat/options.rb b/lib/redistat/options.rb index 252b508..16632d5 100644 --- a/lib/redistat/options.rb +++ b/lib/redistat/options.rb @@ -5,8 +5,6 @@ module Redistat base.extend(ClassMethods) end - class InvalidDefaultOptions < ArgumentError; end - module ClassMethods def option_accessor(*opts) opts.each do |option| diff --git a/redistat.gemspec b/redistat.gemspec index dcc62a5..fb0ee41 100644 --- a/redistat.gemspec +++ b/redistat.gemspec @@ -27,5 +27,4 @@ Gem::Specification.new do |s| s.add_development_dependency 'rspec', '>= 2.1.0' s.add_development_dependency 'rcov', '>= 0.9.9' s.add_development_dependency 'yard', '>= 0.6.3' - s.add_development_dependency 'ruby-debug' end From 43fc8bc2dda8932b76546b8dd75d4b9485593870 Mon Sep 17 00:00:00 2001 From: Jim Myhrberg Date: Sat, 12 Mar 2011 22:21:15 +0000 Subject: [PATCH 5/8] updated Hash extensions and specs --- lib/redistat/core_ext/hash.rb | 26 ++++++++++++++------------ spec/core_ext/hash_spec.rb | 22 ++++++++++++++++------ 2 files changed, 30 insertions(+), 18 deletions(-) diff --git a/lib/redistat/core_ext/hash.rb b/lib/redistat/core_ext/hash.rb index 0cc5390..398e6e2 100644 --- a/lib/redistat/core_ext/hash.rb +++ b/lib/redistat/core_ext/hash.rb @@ -1,21 +1,23 @@ class Hash def merge_and_incr(hash) - raise ArgumentError unless hash.is_a?(Hash) - hash.each do |key, value| - if value.is_a?(Numeric) - self.set_or_incr(key, value) - else - self[key] = value - end - end + self.clone.merge_and_incr!(hash) end - def set_or_incr(key, value) - return self unless value.is_a?(Numeric) - self[key] = 0 unless self.has_key?(key) - self[key] += value if self[key].is_a?(Numeric) + def merge_and_incr!(hash) + raise ArgumentError unless hash.is_a?(Hash) + hash.each do |key, value| + self[key] = value unless self.set_or_incr(key, value) + end self end + def set_or_incr(key, value) + return false unless value.is_a?(Numeric) + self[key] = 0 unless self.has_key?(key) + return false unless self[key].is_a?(Numeric) + self[key] += value + true + end + end diff --git a/spec/core_ext/hash_spec.rb b/spec/core_ext/hash_spec.rb index fad64cc..c838dc2 100644 --- a/spec/core_ext/hash_spec.rb +++ b/spec/core_ext/hash_spec.rb @@ -4,17 +4,27 @@ describe Hash do it "should #set_or_incr values" do hash = {:count => 1} - hash.set_or_incr(:sum, 3) + hash.set_or_incr(:sum, 3).should be_true hash.should == {:count => 1, :sum => 3} - hash.set_or_incr(:count, 4) + hash.set_or_incr(:count, 4).should be_true hash.should == {:count => 5, :sum => 3} + hash.set_or_incr(:count, 'test').should be_false + hash.set_or_incr(:view, 'test').should be_false + hash.should == {:count => 5, :sum => 3} + hash[:view] = 'test' + hash.set_or_incr(:view, 3).should be_false end it "should #merge_and_incr hashes" do - hash = {:count => 1, :city => 'hell', :sum => 3, :name => 'john'} - new_hash = {:count => 3, :city => 'slum', :views => 2} - hash.merge_and_incr(new_hash) - hash.should == {:count => 4, :sum => 3, :views => 2, :city => 'slum', :name => 'john'} + hash = { :count => 1, :city => 'hell', :sum => 3, :name => 'john' } + + new_hash = { :count => 3, :city => 'slum', :views => 2 } + hash.clone.merge_and_incr(new_hash).should == { :count => 4, :city => 'slum', :views => 2, + :sum => 3, :name => 'john' } + + new_hash = { :count => 'six', :city => 'slum', :views => 2, :time => 'late' } + hash.clone.merge_and_incr(new_hash).should == { :count => 'six', :city => 'slum', :views => 2, + :sum => 3, :name => 'john', :time => 'late' } end end From e2a551d01c891a834268707daa9ef66aa4c40860 Mon Sep 17 00:00:00 2001 From: Jim Myhrberg Date: Sat, 12 Mar 2011 22:23:06 +0000 Subject: [PATCH 6/8] syntax beautification --- lib/redistat/summary.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/redistat/summary.rb b/lib/redistat/summary.rb index c32bb6c..b9fbb5c 100644 --- a/lib/redistat/summary.rb +++ b/lib/redistat/summary.rb @@ -18,10 +18,10 @@ module Redistat if options[:enable_grouping] stats = inject_group_summaries(stats) - key.groups.each { |k| + key.groups.each do |k| update_key(k, stats, depth_limit, options[:connection_ref]) k.update_index if options[:label_indexing] - } + end else update_key(key, stats, depth_limit, options[:connection_ref]) end From 94fcd5b4ae1b0699f9645e7b209334e7fe150db4 Mon Sep 17 00:00:00 2001 From: Jim Myhrberg Date: Sat, 12 Mar 2011 22:23:40 +0000 Subject: [PATCH 7/8] fixed a ruby 1.9.x issue --- lib/redistat/summary.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/redistat/summary.rb b/lib/redistat/summary.rb index b9fbb5c..e089b5a 100644 --- a/lib/redistat/summary.rb +++ b/lib/redistat/summary.rb @@ -43,6 +43,7 @@ module Redistat end def self.inject_group_summaries!(stats) + summaries = {} stats.each do |key, value| parts = key.to_s.split(GROUP_SEPARATOR) parts.pop @@ -51,11 +52,11 @@ module Redistat parts.each do |part| sum_parts << part sum_key = sum_parts.join(GROUP_SEPARATOR) - (stats.has_key?(sum_key)) ? stats[sum_key] += value : stats[sum_key] = value + (summaries.has_key?(sum_key)) ? summaries[sum_key] += value : summaries[sum_key] = value end end end - stats + stats.merge_and_incr!(summaries) end def self.inject_group_summaries(stats) From 008228660ed62d04a3d5e1a6a6954799aec3b228 Mon Sep 17 00:00:00 2001 From: Jim Myhrberg Date: Sat, 12 Mar 2011 22:26:12 +0000 Subject: [PATCH 8/8] started release v0.2.2 --- lib/redistat/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/redistat/version.rb b/lib/redistat/version.rb index b2350f8..06e8660 100644 --- a/lib/redistat/version.rb +++ b/lib/redistat/version.rb @@ -1,3 +1,3 @@ module Redistat - VERSION = "0.2.1" + VERSION = "0.2.2" end