0
0
Fork 0

added #each to the storage

This commit is contained in:
Gibheer 2011-07-22 16:31:22 +02:00
parent 98e208c874
commit e2d93d203b
7 changed files with 71 additions and 0 deletions

View File

@ -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"

View File

@ -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 #<=>'

View File

@ -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 #<=>'

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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