diff --git a/lib/redistat.rb b/lib/redistat.rb index 00b8e75..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' @@ -26,10 +31,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..398e6e2 --- /dev/null +++ b/lib/redistat/core_ext/hash.rb @@ -0,0 +1,23 @@ +class Hash + + def merge_and_incr(hash) + self.clone.merge_and_incr!(hash) + end + + 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/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 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/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/lib/redistat/summary.rb b/lib/redistat/summary.rb index c32bb6c..e089b5a 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 @@ -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) 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 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 diff --git a/spec/core_ext/hash_spec.rb b/spec/core_ext/hash_spec.rb new file mode 100644 index 0000000..c838dc2 --- /dev/null +++ b/spec/core_ext/hash_spec.rb @@ -0,0 +1,30 @@ +require "spec_helper" + +describe Hash do + + it "should #set_or_incr values" do + hash = {:count => 1} + hash.set_or_incr(:sum, 3).should be_true + hash.should == {:count => 1, :sum => 3} + 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.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 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