0
0
Fork 0

initial commit with an already somewhat working index

This commit is contained in:
Gibheer 2011-05-07 22:02:28 +02:00
parent 93cf6efa0d
commit b771e23219
9 changed files with 133 additions and 12 deletions

4
.gitignore vendored
View File

@ -39,10 +39,10 @@ pkg
#.\#*
# For vim:
#*.swp
*.swp
# For redcar:
#.redcar
# For rubinius:
#*.rbc
*.rbc

View File

@ -10,6 +10,5 @@ group :development do
gem "yard", "~> 0.6.0"
gem "bundler", "~> 1.0.0"
gem "jeweler", "~> 1.6.0"
gem "rcov", ">= 0"
gem "reek", "~> 1.2.8"
end

View File

@ -17,8 +17,8 @@ Jeweler::Tasks.new do |gem|
gem.name = "polecat"
gem.homepage = "http://github.com/Gibheer/polecat"
gem.license = "MIT"
gem.summary = %Q{TODO: one-line summary of your gem}
gem.description = %Q{TODO: longer description of your gem}
gem.summary = %Q{library for searching through documents}
gem.description = %Q{This is a search library for searching terms in documents}
gem.email = "gibheer@gmail.com"
gem.authors = ["Gibheer"]
# dependencies defined in Gemfile
@ -31,11 +31,6 @@ RSpec::Core::RakeTask.new(:spec) do |spec|
spec.pattern = FileList['spec/**/*_spec.rb']
end
RSpec::Core::RakeTask.new(:rcov) do |spec|
spec.pattern = 'spec/**/*_spec.rb'
spec.rcov = true
end
require 'reek/rake/task'
Reek::Rake::Task.new do |t|
t.fail_on_error = true

View File

@ -0,0 +1,3 @@
class Polecat
require 'polecat/index'
end

40
lib/polecat/index.rb Normal file
View File

@ -0,0 +1,40 @@
class Polecat
class Index
attr_reader :path
# initialises an index object on the given path
#
# This method initialises an index on the path. The Path has to be a
# directory.
def initialize path
if File.directory? path
@path = path
else
raise ArgumentError, "Argument no valid directory"
end
end
# returns true, if it has an index
def index_dir?
false
end
def write term
File.open @path + '/index.txt', 'a' do |f|
f.write "#{term}\n"
f.flush
end
end
def search term
File.open @path + '/index.txt' do |f|
linenr = 0
while (line = f.gets) do
return linenr if line =~ /#{term}/
linenr += 1
end
nil
end
end
end
end

1
spec/index_dir/index.txt Normal file
View File

@ -0,0 +1 @@
foo

67
spec/index_spec.rb Normal file
View File

@ -0,0 +1,67 @@
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
describe "Index" do
before :all do
@path = prepare_index_dir
end
describe "#initialize" do
it "takes a path as an argument" do
i = Polecat::Index.new @path
i.path.should == @path
end
it "raises an ArgumentError, when no path is given" do
lambda { Polecat::Index.new }.should raise_error(ArgumentError)
end
it "raises an ArgumentError, when the path is no directory" do
lambda {
Polecat::Index.new "/dev/null"
}.should raise_error(ArgumentError)
end
end
describe "#index_dir?" do
it "returns false, if the directory does not contain an index" do
i = Polecat::Index.new @path
i.index_dir?.should == false
end
it "returns true, if the directory contains an index"
end
describe "#write" do
before do
@path = prepare_index_dir
@file = @path + '/index.txt'
end
it "writes a string to a file" do
i = Polecat::Index.new @path
i.write "foobar"
File.read(@file).should == "foobar\n"
end
it "appends new entries to the end of the file" do
i = Polecat::Index.new @path
i.write "foo"
i.write "bar"
File.read(@file).should == "foo\nbar\n"
end
end
describe "#search" do
before do
@path = prepare_index_dir
@file = @path + '/index.txt'
end
it "returns the line of the first found occurence" do
i = Polecat::Index.new @path
i.write "foo"
i.search("foo").should == 0
end
end
end

View File

@ -1,7 +1,9 @@
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
describe "Polecat" do
it "fails" do
fail "hey buddy, you should probably rename this file and start specing for real"
describe "#new" do
it "can be initialized" do
Polecat.new
end
end
end

View File

@ -1,6 +1,7 @@
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
$LOAD_PATH.unshift(File.dirname(__FILE__))
require 'rspec'
require 'fileutils'
require 'polecat'
# Requires supporting files with custom matchers and macros, etc,
@ -10,3 +11,16 @@ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
RSpec.configure do |config|
end
def prepare_index_dir
path = File.expand_path(File.dirname(__FILE__) + '/index_dir')
begin
FileUtils.rm_r path
rescue SystemCallError
# the directory structure is not there, so just
# ignore it and print a hint
puts "error occured, but was ignored: $!"
end
Dir.mkdir path
return path
end