diff --git a/Rakefile b/Rakefile index bf42520..af2c9ee 100644 --- a/Rakefile +++ b/Rakefile @@ -40,5 +40,12 @@ end task :default => :spec +namespace :spec do + desc "with all ruby versions" + task :all do + puts `rvm 1.8.7@polecat,1.9.2@polecat,rbx@polecat,rbx-hydra@polecat rake spec` + end +end + require 'yard' YARD::Rake::YardocTask.new diff --git a/lib/polecat.rb b/lib/polecat.rb index 903419f..d746e86 100644 --- a/lib/polecat.rb +++ b/lib/polecat.rb @@ -1,3 +1,4 @@ class Polecat require 'polecat/index' + require 'polecat/document' end diff --git a/lib/polecat/document.rb b/lib/polecat/document.rb new file mode 100644 index 0000000..438ff28 --- /dev/null +++ b/lib/polecat/document.rb @@ -0,0 +1,38 @@ +require 'polecat/document_resource' + +module Document + OPTIONS = { + :index => true, + :lazy => false, + :value => nil + } + + def self.included o + o.extend(DocumentResource) + o.instance_variable_set :@attributes, {} + end + + def initialize fields = {} + fields.each do |key, value| + attribute_set key, value + end + end + + def attribute_get name + attributes[name.to_sym][:value] + end + + def attribute_set name, value + if attributes.has_key? name.to_sym + attributes[name.to_sym][:value] = value + else + raise ArgumentError, "attribute #{name} does not exist" + end + end + + def attributes + return @attributes if @attributes + @attributes = Marshal.load(Marshal.dump( + self.class.instance_variable_get :@attributes)) + end +end diff --git a/lib/polecat/document_resource.rb b/lib/polecat/document_resource.rb new file mode 100644 index 0000000..f61c710 --- /dev/null +++ b/lib/polecat/document_resource.rb @@ -0,0 +1,35 @@ +module DocumentResource + def field name, options = {} + attributes = self.instance_variable_get :@attributes + attributes[name.to_sym] = Document::OPTIONS.merge(options) + + create_reader_for name + create_writer_for name + end + + def mod + if !@mod + @mod = Module.new + self.class_eval do + include @mod + end + end + @mod + end + + def create_reader_for name + mod.module_eval <<-RUBYCODE + def #{name.to_s} + attribute_get :#{name} + end + RUBYCODE + end + + def create_writer_for name + mod.module_eval <<-RUBYCODE + def #{name.to_s}= o + attribute_set :#{name}, o + end + RUBYCODE + end +end diff --git a/spec/document/document_spec.rb b/spec/document/document_spec.rb new file mode 100644 index 0000000..7bba3c3 --- /dev/null +++ b/spec/document/document_spec.rb @@ -0,0 +1,63 @@ +require File.expand_path(File.dirname(__FILE__) + '/spec_helper') + +describe "Document" do + describe "#initialize" do + it "creates a new Document object" do + d = Spec::FooDocument.new + d.is_a?(Polecat::Document).should == true + end + + it "takes a hash as argument" do + d = Spec::FooDocument.new( + :id => 1, + :name => 'foo', + :lastname => 'bar', + :description => 'this is a test' + ) + d.id.should == 1 + d.name.should == 'foo' + d.lastname.should == 'bar' + d.description.should == 'this is a test' + end + + it "sets the attributes to nil as default" do + d = Spec::FooDocument.new + d.id.should == nil + end + + it "creates a method to get an attribute value" do + class FooDoc + include Polecat::Document + field :doc + end + FooDoc.new.respond_to?(:doc).should == true + end + + it "creates a method to set an attribute" do + class FooDoc + include Polecat::Document + field :doc + end + d = FooDoc.new + d.doc = 'foo' + d.doc.should == 'foo' + end + + it "raises an error, when the key is not found" do + lambda { Spec::FooDocument.new(:foo => :bar) }.should( + raise_error(ArgumentError)) + end + end + + describe "#attributes" do + it "returns a hash with all attributes" do + class FooDoc + include Polecat::Document + + field :bar + end + d = FooDoc.new + d.attributes.should == {:bar => Polecat::Document::OPTIONS} + end + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index e796177..5759ba8 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -24,3 +24,14 @@ def prepare_index_dir Dir.mkdir path return path end + +module Spec + class FooDocument + include Polecat::Document + + field :id + field :name, :lazy => true + field :lastname, :lazy => true + field :description, :analyze => true, :lazy => true + end +end