From b771e23219887f47df010bd88050e2141ff105c6 Mon Sep 17 00:00:00 2001 From: Gibheer Date: Sat, 7 May 2011 22:02:28 +0200 Subject: [PATCH] initial commit with an already somewhat working index --- .gitignore | 4 +-- Gemfile | 1 - Rakefile | 9 ++---- lib/polecat.rb | 3 ++ lib/polecat/index.rb | 40 ++++++++++++++++++++++++ spec/index_dir/index.txt | 1 + spec/index_spec.rb | 67 ++++++++++++++++++++++++++++++++++++++++ spec/polecat_spec.rb | 6 ++-- spec/spec_helper.rb | 14 +++++++++ 9 files changed, 133 insertions(+), 12 deletions(-) create mode 100644 lib/polecat/index.rb create mode 100644 spec/index_dir/index.txt create mode 100644 spec/index_spec.rb diff --git a/.gitignore b/.gitignore index eb8e985..e74bf5c 100644 --- a/.gitignore +++ b/.gitignore @@ -39,10 +39,10 @@ pkg #.\#* # For vim: -#*.swp +*.swp # For redcar: #.redcar # For rubinius: -#*.rbc +*.rbc diff --git a/Gemfile b/Gemfile index dde2623..4184e85 100644 --- a/Gemfile +++ b/Gemfile @@ -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 diff --git a/Rakefile b/Rakefile index e84284b..bf42520 100644 --- a/Rakefile +++ b/Rakefile @@ -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 diff --git a/lib/polecat.rb b/lib/polecat.rb index e69de29..903419f 100644 --- a/lib/polecat.rb +++ b/lib/polecat.rb @@ -0,0 +1,3 @@ +class Polecat + require 'polecat/index' +end diff --git a/lib/polecat/index.rb b/lib/polecat/index.rb new file mode 100644 index 0000000..9311b4a --- /dev/null +++ b/lib/polecat/index.rb @@ -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 diff --git a/spec/index_dir/index.txt b/spec/index_dir/index.txt new file mode 100644 index 0000000..257cc56 --- /dev/null +++ b/spec/index_dir/index.txt @@ -0,0 +1 @@ +foo diff --git a/spec/index_spec.rb b/spec/index_spec.rb new file mode 100644 index 0000000..a05139a --- /dev/null +++ b/spec/index_spec.rb @@ -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 diff --git a/spec/polecat_spec.rb b/spec/polecat_spec.rb index 4500de6..0771e0e 100644 --- a/spec/polecat_spec.rb +++ b/spec/polecat_spec.rb @@ -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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 8a0f6f2..e796177 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -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