added #each to the storage
This commit is contained in:
parent
98e208c874
commit
e2d93d203b
2
Gemfile
2
Gemfile
|
@ -1,5 +1,7 @@
|
||||||
source "http://rubygems.org"
|
source "http://rubygems.org"
|
||||||
|
|
||||||
|
gem "veritas", :git => 'https://github.com/dkubb/veritas.git'
|
||||||
|
|
||||||
# comment out the gems, you don't want
|
# comment out the gems, you don't want
|
||||||
group :preferred do
|
group :preferred do
|
||||||
gem "virtus"
|
gem "virtus"
|
||||||
|
|
|
@ -118,6 +118,13 @@ module Polecat
|
||||||
end
|
end
|
||||||
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
|
def check_key key
|
||||||
unless key.respond_to?(:<=>) && key.respond_to?(:<=)
|
unless key.respond_to?(:<=>) && key.respond_to?(:<=)
|
||||||
raise ArgumentError, 'key does not support #<=>'
|
raise ArgumentError, 'key does not support #<=>'
|
||||||
|
|
|
@ -47,6 +47,11 @@ module Polecat
|
||||||
@storage.count
|
@storage.count
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# traverse all elements
|
||||||
|
def each &block
|
||||||
|
@storage.each &block
|
||||||
|
end
|
||||||
|
|
||||||
def check_key key
|
def check_key key
|
||||||
unless key.respond_to?(:<=>) && key.respond_to?(:<=)
|
unless key.respond_to?(:<=>) && key.respond_to?(:<=)
|
||||||
raise ArgumentError, 'key does not support #<=>'
|
raise ArgumentError, 'key does not support #<=>'
|
||||||
|
|
|
@ -64,6 +64,15 @@ module Polecat
|
||||||
def interval lower, upper
|
def interval lower, upper
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
end
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -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
|
|
@ -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
|
|
@ -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
|
Loading…
Reference in New Issue