test with all rubies
This commit is contained in:
parent
24ba8c1761
commit
f0ea1944da
7
Rakefile
7
Rakefile
|
@ -40,5 +40,12 @@ end
|
||||||
|
|
||||||
task :default => :spec
|
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'
|
require 'yard'
|
||||||
YARD::Rake::YardocTask.new
|
YARD::Rake::YardocTask.new
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
class Polecat
|
class Polecat
|
||||||
require 'polecat/index'
|
require 'polecat/index'
|
||||||
|
require 'polecat/document'
|
||||||
end
|
end
|
||||||
|
|
|
@ -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
|
|
@ -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
|
|
@ -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
|
|
@ -24,3 +24,14 @@ def prepare_index_dir
|
||||||
Dir.mkdir path
|
Dir.mkdir path
|
||||||
return path
|
return path
|
||||||
end
|
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
|
||||||
|
|
Loading…
Reference in New Issue