Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Store Array and Hash, fixes issue #10

  • Loading branch information...
commit df4444afabaca7dbd8b65bfd4edafa515c0a9324 1 parent 89341fa
@singpolyma authored
View
21 lib/xapian_fu/xapian_doc_value_accessor.rb
@@ -1,4 +1,5 @@
require 'zlib'
+require 'yaml'
class Integer #:nodoc:
def self.to_xapian_fu_storage_value(value)
@@ -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

What kind of exceptions can be raised and we want to ignore?

In general, rescue nil is a very bad idea.

@singpolyma Owner

I don't recall why the rescue nil is there in this case. It may have been a temporary part of testing. You could take it off.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ 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
View
16 spec/xapian_doc_value_accessor_spec.rb
@@ -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)
@djanowski

What kind of exceptions can be raised and we want to ignore?

In general, rescue nil is a very bad idea.

@singpolyma

I don't recall why the rescue nil is there in this case. It may have been a temporary part of testing. You could take it off.

Please sign in to comment.
Something went wrong with that request. Please try again.