From 5087f4ef45c75087f9cdb28177ab125e13cc8cdf Mon Sep 17 00:00:00 2001 From: Jim Myhrberg Date: Tue, 17 Apr 2012 16:31:31 +0100 Subject: [PATCH] Improve Buffer's unique key identifier --- lib/redistat/buffer.rb | 11 +++++++---- spec/buffer_spec.rb | 6 ++++-- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/lib/redistat/buffer.rb b/lib/redistat/buffer.rb index dc13bd6..ac645ad 100644 --- a/lib/redistat/buffer.rb +++ b/lib/redistat/buffer.rb @@ -95,12 +95,15 @@ module Redistat end end + # depth_limit is not needed as it's evident in key.to_s def buffer_key(key, opts) - # depth_limit is not needed as it's evident in key.to_s - opts_index = Summary.default_options.keys.sort { |a,b| a.to_s <=> b.to_s }.map do |k| - opts[k] if opts.has_key?(k) + # covert keys to strings, as sorting a Hash with Symbol keys fails on + # Ruby 1.8.x. + opts = opts.inject({}) do |result, (k, v)| + result[k.to_s] = v + result end - "#{key.to_s}:#{opts_index.join(':')}" + "#{key.to_s}:#{opts.sort.flatten.join(':')}" end end diff --git a/spec/buffer_spec.rb b/spec/buffer_spec.rb index 5d5d8f5..c2b0bd5 100644 --- a/spec/buffer_spec.rb +++ b/spec/buffer_spec.rb @@ -78,9 +78,11 @@ describe Redistat::Buffer do it "should build #buffer_key correctly" do opts = {:enable_grouping => true, :label_indexing => false, :connection_ref => nil} - @buffer.send(:buffer_key, @key, opts).should == "#{@key.to_s}::true:false" + @buffer.send(:buffer_key, @key, opts).should == + "#{@key.to_s}:connection_ref::enable_grouping:true:label_indexing:false" opts = {:enable_grouping => false, :label_indexing => true, :connection_ref => :omg} - @buffer.send(:buffer_key, @key, opts).should == "#{@key.to_s}:omg:false:true" + @buffer.send(:buffer_key, @key, opts).should == + "#{@key.to_s}:connection_ref:omg:enable_grouping:false:label_indexing:true" end describe "Buffering" do