class Mongoid::Giza::XMLPipe2
Represents the xmlpipe2 data source
Public Class Methods
Creates a new XMLPipe2
object based on the specified index and that will
write to the specified buffer.
Note that the actual XML will be generated only when {#generate!} is
called
@param index [Mongoid::Giza::Index] the index which will be used to
generate the data
@param buffer any object that supports the method <<
# File lib/mongoid/giza/xml_pipe2.rb, line 15 def initialize(index, buffer) @index = index @xml = Builder::XmlMarkup.new(target: buffer) end
Public Instance Methods
Returns a Hash of the attribute's attributes
@return [Hash] The attribute's attributes
# File lib/mongoid/giza/xml_pipe2.rb, line 58 def attribute_attrs(attribute) attrs = {name: attribute.name, type: attribute.type} attrs[:default] = attribute.default if attribute.default attrs[:bits] = attribute.bits if attribute.bits attrs end
Returns a Hash of the field's attributes
@return [Hash] The field's attributes
# File lib/mongoid/giza/xml_pipe2.rb, line 49 def field_attrs(field) attrs = {name: field.name} attrs[:attr] = :string if field.attribute attrs end
Generates a XML document with the
{http://sphinxsearch.com/docs/current.html#xmlpipe2 xmlpipe2 specification}.
The buffer passed on object creation will contain the XML
# File lib/mongoid/giza/xml_pipe2.rb, line 24 def generate! @xml.instruct! :xml, version: "1.0", encoding: "utf-8" @xml.sphinx :docset do generate_schema generate_docset end end
Generates the content part of the XML document. Used internally by {#generate!} so you should never need to call it
directly
# File lib/mongoid/giza/xml_pipe2.rb, line 68 def generate_docset @index.criteria.each do |object| @xml.sphinx :document, id: object._giza_id do generate_doc_tags(@index.fields, object) generate_doc_tags(@index.attributes, object) end end end
Generates the schema part of the XML document. Used internally by {#generate!} so you should never need to call it
directly
# File lib/mongoid/giza/xml_pipe2.rb, line 35 def generate_schema @xml.sphinx :schema do |schema| @index.fields.each do |field| schema.sphinx :field, field_attrs(field) end @index.attributes.each do |attribute| schema.sphinx :attr, attribute_attrs(attribute) end end end
Process values
-
Converts Date, Time and DateTime objects to unix time
@param content
[Mongoid::Giza::Index::Field, Mongoid::Giza::Index::Attribute] field or attribute to process content
@param object [Object] object being indexed
@return the processed object attribute value
# File lib/mongoid/giza/xml_pipe2.rb, line 104 def process_value(content, object) if content.is_a?(Index::Attribute) && content.type == :timestamp return object[content.name].to_time.to_i end object[content.name] end