class Mongoid::Giza::XMLPipe2

Represents the xmlpipe2 data source

Public Class Methods

new(index, buffer) click to toggle source

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

attribute_attrs(attribute) click to toggle source

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
field_attrs(field) click to toggle source

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
generate!() click to toggle source

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
generate_doc_tags(contents, object) click to toggle source

Generates the tags with the content to be indexed of every field and

attribute.

Used internally by {#generate_docset} so you should never need to call

it directly

@param contents [Array] list of fields or attributes to generate the

tags for

@param object [Object] the object being indexed

# File lib/mongoid/giza/xml_pipe2.rb, line 85
def generate_doc_tags(contents, object)
  contents.each do |content|
    if content.block
      @xml.tag! content.name, content.block.call(object)
    else
      @xml.tag! content.name, process_value(content, object)
    end
  end
end
generate_docset() click to toggle source

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
generate_schema() click to toggle source

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_value(content, object) click to toggle source

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