Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions lib/xapian_fu/xapian_doc_value_accessor.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'zlib'
require 'yaml'

class Integer #:nodoc:
def self.to_xapian_fu_storage_value(value)
Expand Down Expand Up @@ -63,6 +64,26 @@ def self.from_xapian_fu_storage_value(value)
end
end

class Array #:nodoc:
def self.to_xapian_fu_storage_value(value)
YAML::dump(value)
end

def self.from_xapian_fu_storage_value(value)
YAML::load(value) rescue nil
end
end

class Hash #:nodoc:
def self.to_xapian_fu_storage_value(value)
YAML::dump(value)
end

def self.from_xapian_fu_storage_value(value)
YAML::load(value) rescue nil
end
end

module XapianFu #:nodoc:

class ValueOutOfBounds < XapianFuError
Expand Down
16 changes: 16 additions & 0 deletions spec/xapian_doc_value_accessor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,22 @@
doc.to_xapian_document.values.first.value.should == date.to_s
end

it "should store fields defined as Array" do
xdb = XapianDb.new(:fields => { :list => { :type => Array, :store => true }})
list = [1,2,3]
doc = xdb.documents.new(:list => list)
doc.values.store(:list, list).should == list
doc.values.fetch(:list).should == list
end

it "should store fields defined as Hash" do
xdb = XapianDb.new(:fields => { :h => { :type => Hash, :store => true }})
h = {:a => 1, :b => 2}
doc = xdb.documents.new(:h => h)
doc.values.store(:h, h).should == h
doc.values.fetch(:h).should == h
end

it "should count the stored values when size is called" do
doc = XapianDoc.new(nil)
lambda { doc.values[:city] = "London" }.should change(doc.values, :size).by(1)
Expand Down