xapian_db 0.4.1 → 0.4.2

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md CHANGED
@@ -1,3 +1,23 @@
1
+ ##0.4.2 (December 17th, 2010)
2
+
3
+ Features:
4
+
5
+ - added a sample rails application to the repo
6
+ - added the id attribute for documents based on ActiveRecord and Datamapper objects
7
+
8
+ Changes:
9
+
10
+ - removed the language_method option from the blueprint configuration since it gives
11
+ unpredictable results
12
+
13
+ Bugfixes:
14
+
15
+ - fixed the initialization error in a Rails app ig there is no xapian_db.yml config file
16
+ - fixed the fallback to the global language when a model has an unsupported language and a
17
+ language method is configured in the blueprint
18
+ - fixed an issue with yaml deserialization of ActiveRecord objects (only the attributes hash
19
+ should be serialized)
20
+
1
21
  ##0.4.1 (December 16th, 2010)
2
22
 
3
23
  Bugfixes:
data/README.rdoc CHANGED
@@ -108,12 +108,6 @@ If you config a class that has a language property, e.g.
108
108
  attr_reader :language
109
109
  end
110
110
 
111
- you can configure the blueprint to use the language of the object when indexing:
112
-
113
- XapianDb::DocumentBlueprint.setup(Person) do |blueprint|
114
- blueprint.language_method :language
115
- end
116
-
117
111
  The method must return the iso code for the language (:en, :de, ...) as a symbol or a string. Don't worry if you have languages in your database that are not supported by Xapian. If the language is not supported, XapianDb will fall back to the global language configuration or none, if you haven't configured one.
118
112
 
119
113
  If you want to declare multiple attributes or indexes with default options, you can do this in one statement:
@@ -59,6 +59,15 @@ module XapianDb
59
59
  # @param [Module] a_module The module to add the helper methods to
60
60
  def add_doc_helper_methods_to(a_module)
61
61
  a_module.instance_eval do
62
+
63
+ # Implement access to the model id
64
+ define_method :id do
65
+ return @id unless @d.nil?
66
+ # retrieve the class and id from data
67
+ klass_name, id = data.split("-")
68
+ @id = id.to_i
69
+ end
70
+
62
71
  # Implement access to the indexed object
63
72
  define_method :indexed_object do
64
73
  return @indexed_object unless @indexed_object.nil?
@@ -67,6 +76,7 @@ module XapianDb
67
76
  klass = Kernel.const_get(klass_name)
68
77
  @indexed_object = klass.find(id.to_i)
69
78
  end
79
+
70
80
  end
71
81
 
72
82
  end
@@ -58,6 +58,15 @@ module XapianDb
58
58
  # @param [Module] a_module The module to add the helper methods to
59
59
  def add_doc_helper_methods_to(a_module)
60
60
  a_module.instance_eval do
61
+
62
+ # Implement access to the model id
63
+ define_method :id do
64
+ return @id unless @d.nil?
65
+ # retrieve the class and id from data
66
+ klass_name, id = data.split("-")
67
+ @id = id.to_i
68
+ end
69
+
61
70
  # Implement access to the indexed object
62
71
  define_method :indexed_object do
63
72
  return @indexed_object unless @indexed_object.nil?
@@ -129,13 +129,16 @@ module XapianDb
129
129
  end
130
130
  end
131
131
 
132
- @attributes_hash.keys.sort.each_with_index do |field, index|
132
+ # Add an accessor for each attribute
133
+ attribute_names.each do |attribute|
134
+ index = value_index_for(attribute)
133
135
  @accessors_module.instance_eval do
134
- define_method field do
135
- YAML::load(self.values[index+1].value)
136
+ define_method attribute do
137
+ YAML::load(self.values[index].value)
136
138
  end
137
139
  end
138
140
  end
141
+
139
142
  # Let the adapter add its document helper methods (if any)
140
143
  adapter = @adapter || XapianDb::Config.adapter || XapianDb::Adapters::GenericAdapter
141
144
  adapter.add_doc_helper_methods_to(@accessors_module)
@@ -161,13 +164,6 @@ module XapianDb
161
164
  @indexed_methods_hash = {}
162
165
  end
163
166
 
164
- # Set the name of the method to get the language for an indexed object
165
- # @param [Symbol] lang The method name. The method must return an iso language code (:en, :de, ...)
166
- # see LANGUAGE_MAP for the supported lanugaes
167
- def language_method(lang)
168
- @lang_method = lang
169
- end
170
-
171
167
  # Add an attribute to the blueprint. Attributes will be stored in the xapian documents an can be
172
168
  # accessed from a search result.
173
169
  # @param [String] name The name of the method that delivers the value for the attribute
@@ -42,19 +42,22 @@ module XapianDb
42
42
  else
43
43
  value = @obj.send(attribute)
44
44
  end
45
- @xapian_doc.add_value(@blueprint.value_index_for(attribute), value.to_yaml)
45
+
46
+ # If we have an object that responds to attributes (e.g. an Active Record
47
+ # or a Datamapper model), we serialize only the attributes
48
+ yaml = value.respond_to?(:attributes) ? value.attributes.to_yaml : value.to_yaml
49
+ @xapian_doc.add_value(@blueprint.value_index_for(attribute), yaml)
46
50
  end
47
51
  end
48
52
 
49
53
  # Index all configured text methods
50
54
  def index_text
51
- setup_language_helpers
52
55
  term_generator = Xapian::TermGenerator.new
53
56
  term_generator.database = @database.writer
54
57
  term_generator.document = @xapian_doc
55
- if @stemmer
56
- term_generator.stemmer = @stemmer
57
- term_generator.stopper = @stopper unless @stopper.nil?
58
+ if XapianDb::Config.stemmer
59
+ term_generator.stemmer = XapianDb::Config.stemmer
60
+ term_generator.stopper = XapianDb::Config.stopper if XapianDb::Config.stopper
58
61
  # Enable the creation of a spelling dictionary if the database is not in memory
59
62
  term_generator.set_flags Xapian::TermGenerator::FLAG_SPELLING if @database.is_a? XapianDb::PersistentDatabase
60
63
  end
@@ -66,25 +69,6 @@ module XapianDb
66
69
  term_generator.index_text("#{@obj.class}".downcase, 1, "XINDEXED_CLASS")
67
70
  @xapian_doc.add_term("C#{@obj.class}")
68
71
 
69
-
70
- # @blueprint.indexed_methods_hash.keys.sort.each do |method|
71
- # options = @blueprint.indexed_methods_hash[method]
72
- # if options.block
73
- # obj = @obj.instance_eval(&options.block)
74
- # else
75
- # obj = @obj.send(method)
76
- # end
77
- # unless obj.nil?
78
- # values = get_values_to_index_from obj
79
- # values.each do |value|
80
- # # Add value with field name
81
- # term_generator.index_text(value.to_s.downcase, options.weight, "X#{method.upcase}")
82
- # # Add value without field name
83
- # term_generator.index_text(value.to_s.downcase)
84
- # end
85
- # end
86
- # end
87
-
88
72
  @blueprint.indexed_method_names.each do |method|
89
73
  options = @blueprint.options_for_indexed_method method
90
74
  if options.block
@@ -107,24 +91,6 @@ module XapianDb
107
91
 
108
92
  private
109
93
 
110
- # Configure the stemmer and stopper to use
111
- def setup_language_helpers
112
- # Do we have a language config on the blueprint?
113
- if @blueprint.lang_method
114
- lang = @obj.send(@blueprint.lang_method)
115
- if lang && LANGUAGE_MAP.has_key?(lang.to_sym)
116
- @stemmer = XapianDb::Repositories::Stemmer.stemmer_for lang.to_sym
117
- @stopper = XapianDb::Repositories::Stopper.stopper_for lang.to_sym
118
- return
119
- end
120
- end
121
-
122
- # Use the global config
123
- @stemmer = XapianDb::Config.stemmer
124
- @stopper = XapianDb::Config.stopper
125
-
126
- end
127
-
128
94
  # Get the values to index from an object
129
95
  def get_values_to_index_from(obj)
130
96
 
@@ -35,7 +35,7 @@ module XapianDb
35
35
  end
36
36
  config.adapter adapter.to_sym
37
37
  config.writer writer.to_sym
38
- config.language(env_config["language"]) if env_config["language"]
38
+ config.language(env_config["language"]) if env_config && env_config["language"]
39
39
  end
40
40
 
41
41
  end
@@ -21,14 +21,12 @@ module XapianDb
21
21
  # Do we already have a stopper for this language?
22
22
  return @stoppers[key] unless @stoppers[key].nil?
23
23
 
24
- # Do we support this language?
25
- unless (LANGUAGE_MAP.keys + [:none]).include?(key)
26
- raise ArgumentError.new "Language #{iso_cd} is not supported by XapianDb (remember to use the language iso codes)"
27
- end
28
-
29
24
  # build the stopper
30
25
  stopper = Xapian::SimpleStopper.new
31
26
  stopwords_file = File.join(File.dirname(__FILE__), '../stopwords', "#{iso_cd}.txt")
27
+
28
+ return nil unless File.exist? stopwords_file
29
+
32
30
  open(stopwords_file, "r") do |file|
33
31
  file.each do |word|
34
32
  stopper.add word.chomp
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 4
8
- - 1
9
- version: 0.4.1
8
+ - 2
9
+ version: 0.4.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Gernot Kogler
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-12-16 00:00:00 +01:00
17
+ date: 2010-12-17 00:00:00 +01:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency