From e2d93d203baf8dda2586a00381c21d46be8546b1 Mon Sep 17 00:00:00 2001 From: Gibheer Date: Fri, 22 Jul 2011 16:31:22 +0200 Subject: [PATCH] added #each to the storage --- Gemfile | 2 ++ lib/polecat/storage/binary_storage.rb | 7 +++++++ lib/polecat/storage/hash_storage.rb | 5 +++++ lib/polecat/storage/storage.rb | 9 +++++++++ spec/storage/binary_storage/each_spec.rb | 24 ++++++++++++++++++++++++ spec/storage/hash_storage/each_spec.rb | 16 ++++++++++++++++ spec/storage/storage/each_spec.rb | 8 ++++++++ 7 files changed, 71 insertions(+) create mode 100644 spec/storage/binary_storage/each_spec.rb create mode 100644 spec/storage/hash_storage/each_spec.rb create mode 100644 spec/storage/storage/each_spec.rb diff --git a/Gemfile b/Gemfile index 1dc36aa..88273b3 100644 --- a/Gemfile +++ b/Gemfile @@ -1,5 +1,7 @@ source "http://rubygems.org" +gem "veritas", :git => 'https://github.com/dkubb/veritas.git' + # comment out the gems, you don't want group :preferred do gem "virtus" diff --git a/lib/polecat/storage/binary_storage.rb b/lib/polecat/storage/binary_storage.rb index 5109328..8345793 100644 --- a/lib/polecat/storage/binary_storage.rb +++ b/lib/polecat/storage/binary_storage.rb @@ -118,6 +118,13 @@ module Polecat end end + def each node = @root, &block + return if node.nil? + each node.lower, &block unless node.lower.nil? + yield node.key, node.value + each node.upper, &block unless node.upper.nil? + end + def check_key key unless key.respond_to?(:<=>) && key.respond_to?(:<=) raise ArgumentError, 'key does not support #<=>' diff --git a/lib/polecat/storage/hash_storage.rb b/lib/polecat/storage/hash_storage.rb index 305de72..590b4b9 100644 --- a/lib/polecat/storage/hash_storage.rb +++ b/lib/polecat/storage/hash_storage.rb @@ -47,6 +47,11 @@ module Polecat @storage.count end + # traverse all elements + def each &block + @storage.each &block + end + def check_key key unless key.respond_to?(:<=>) && key.respond_to?(:<=) raise ArgumentError, 'key does not support #<=>' diff --git a/lib/polecat/storage/storage.rb b/lib/polecat/storage/storage.rb index 429e6ec..9efbdfa 100644 --- a/lib/polecat/storage/storage.rb +++ b/lib/polecat/storage/storage.rb @@ -64,6 +64,15 @@ module Polecat def interval lower, upper raise NotImplementedError end + + # traverse all keys + # + # Traverse the list of all documents and get a list of tuples. + # @param [Proc] the block to traverse all documents + # @yield [key, value] get a list of tuples + def each &block + raise NotImplementedError + end end end end diff --git a/spec/storage/binary_storage/each_spec.rb b/spec/storage/binary_storage/each_spec.rb new file mode 100644 index 0000000..d2ee5a1 --- /dev/null +++ b/spec/storage/binary_storage/each_spec.rb @@ -0,0 +1,24 @@ +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') +require 'polecat/storage/binary_storage' + +describe "BinaryStorage#each" do + let (:s) { Polecat::Storage::BinaryStorage.new } + before do + s.add(1,1) + s.add(2,2) + end + + it "yields the key and the value" do + s.each do |key, value| + value.should == 1 if key == 1 + end + end + + it "yields all elements" do + count = 0 + s.each do + count += 1 + end + count.should == s.count + end +end diff --git a/spec/storage/hash_storage/each_spec.rb b/spec/storage/hash_storage/each_spec.rb new file mode 100644 index 0000000..15f9cbd --- /dev/null +++ b/spec/storage/hash_storage/each_spec.rb @@ -0,0 +1,16 @@ +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') +require 'polecat/storage/hash_storage' + +describe "HashStorage#each" do + let (:s) { Polecat::Storage::HashStorage.new } + before do + s.add(1, 1) + s.add(2, 2) + end + + it "yields the key and value" do + s.each do |key, value| + value.should == 1 if key == 1 + end + end +end diff --git a/spec/storage/storage/each_spec.rb b/spec/storage/storage/each_spec.rb new file mode 100644 index 0000000..fe8a32d --- /dev/null +++ b/spec/storage/storage/each_spec.rb @@ -0,0 +1,8 @@ +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') + +describe "Storage#each" do + it "raises an error, as it has to be implemented" do + lambda { Polecat::Storage::Storage.new.each {|k,v| } }.should( + raise_error(NotImplementedError)) + end +end