Module: ActsAsXapian::InstanceMethods
- Defined in:
- lib/acts_as_xapian/base.rb
Overview
Instance methods that get injected into your model.
Instance Method Summary collapse
-
#xapian_destroy ⇒ Object
Delete record from the Xapian database.
-
#xapian_document_term ⇒ Object
Used internally.
-
#xapian_index ⇒ Object
Store record in the Xapian database.
- #xapian_mark_needs_destroy ⇒ Object
-
#xapian_mark_needs_index ⇒ Object
Used to mark changes needed by batch indexer.
-
#xapian_value(field, type = nil) ⇒ Object
Extract value of a field from the model.
Instance Method Details
#xapian_destroy ⇒ Object
Delete record from the Xapian database
170 171 172 |
# File 'lib/acts_as_xapian/base.rb', line 170 def xapian_destroy WriteableIndex.delete_document("I#{self.xapian_document_term}") end |
#xapian_document_term ⇒ Object
Used internally
119 120 121 |
# File 'lib/acts_as_xapian/base.rb', line 119 def xapian_document_term "#{self.class}-#{self.id}" end |
#xapian_index ⇒ Object
Store record in the Xapian database
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/acts_as_xapian/base.rb', line 141 def xapian_index # if we have a conditional function for indexing, call it and destory object if failed if self.class..key?(:if) && !xapian_value(self.class.[:if], :boolean) self.xapian_destroy return end # otherwise (re)write the Xapian record for the object doc = Xapian::Document.new WriteableIndex.term_generator.document = doc doc.data = self.xapian_document_term doc.add_term("M#{self.class}") doc.add_term("I#{doc.data}") (self.[:terms] || []).each do |term| WriteableIndex.term_generator.increase_termpos # stop phrases spanning different text fields WriteableIndex.term_generator.index_text(xapian_value(term[0]), self.xapian_boost(:term, term[0]), term[1]) end (self.[:values] || []).each {|value| doc.add_value(value[1], xapian_value(value[0], value[3])) } (self.[:texts] || []).each do |text| WriteableIndex.term_generator.increase_termpos # stop phrases spanning different text fields WriteableIndex.term_generator.index_text(xapian_value(text), self.xapian_boost(:text, text)) end WriteableIndex.replace_document("I#{doc.data}", doc) end |
#xapian_mark_needs_destroy ⇒ Object
189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/acts_as_xapian/base.rb', line 189 def xapian_mark_needs_destroy model = self.class.base_class.to_s model_id = self.id ActiveRecord::Base.transaction do found = ActsAsXapianJob.delete_all(["model = ? and model_id = ?", model, model_id]) job = ActsAsXapianJob.new job.model = model job.model_id = model_id job.action = 'destroy' job.save! end end |
#xapian_mark_needs_index ⇒ Object
Used to mark changes needed by batch indexer
175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/acts_as_xapian/base.rb', line 175 def xapian_mark_needs_index model = self.class.base_class.to_s model_id = self.id return false unless model_id # After save gets called even if save fails ActiveRecord::Base.transaction do found = ActsAsXapianJob.delete_all(["model = ? and model_id = ?", model, model_id]) job = ActsAsXapianJob.new job.model = model job.model_id = model_id job.action = 'update' job.save! end end |
#xapian_value(field, type = nil) ⇒ Object
Extract value of a field from the model
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/acts_as_xapian/base.rb', line 124 def xapian_value(field, type = nil) value = self.respond_to?(field) ? self.send(field) : self[field] # Give preference to method if it exists case type when :date value = value.to_time if value.kind_of?(Date) raise "Only Time or Date types supported by acts_as_xapian for :date fields, got #{value.class}" unless value.kind_of?(Time) value.utc.strftime("%Y%m%d") when :boolean value ? true : false when :number value.nil? ? "" : Xapian::sortable_serialise(value.to_f) else value.to_s end end |