Class: MongoSphinx::Indexer::XMLDocset

Inherits:
Object
  • Object
show all
Defined in:
lib/indexer.rb

Overview

Class XMLDocset wraps the XML representation of a document to index. It contains a complete “sphinx:docset” including its schema definition.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rows = []) ⇒ XMLDocset

Creates a XMLDocset object from the provided data. It defines a superset of all fields of the classes to index objects for. The class names are collected from the provided objects as well.

Parameters:

data

Array with objects of type CouchRest::Document or Hash to create XML for

Raises:

  • (ArgumentError)


85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/indexer.rb', line 85

def initialize(rows = [])
  raise ArgumentError, 'Missing class names' if rows.nil?

  xml = '<?xml version="1.0" encoding="utf-8"?>'

  xml << '<sphinx:docset><sphinx:schema>'

  @xml_docs = []
  classes = []

  rows.each do |row|
    object = nil

    if row.kind_of? MongoMapper::Document
      object = row
    elsif row.kind_of? Hash
      row = row['value'] if row['classname'].nil?

      if row and (class_name = row['classname'])
        object = eval(class_name.to_s).new(row) rescue nil
      end
    end

    if object and object.sphinx_id
      classes << object.class if not classes.include? object.class
      @xml_docs << XMLDoc.from_object(object)
    end
  end

  field_names = classes.collect { |clas| clas.fulltext_keys rescue []
                  }.flatten.uniq

  field_names.each do |key, value|
    xml << "<sphinx:field name=\"#{key}\"/>"
  end

  xml << '<sphinx:field name="classname"/>'
  xml << '<sphinx:attr name="csphinx-class" type="multi"/>'

  xml << '</sphinx:schema>'

  @xml_header = xml
  @xml_footer = '</sphinx:docset>'
end

Instance Attribute Details

#xml_docsObject (readonly)

Objects contained in document set.



67
68
69
# File 'lib/indexer.rb', line 67

def xml_docs
  @xml_docs
end

XML generated for closing the document.



75
76
77
# File 'lib/indexer.rb', line 75

def xml_footer
  @xml_footer
end

#xml_headerObject (readonly)

XML generated for opening the document.



71
72
73
# File 'lib/indexer.rb', line 71

def xml_header
  @xml_header
end

Instance Method Details

#to_sObject

Returns the encoded data as XML.



138
139
140
# File 'lib/indexer.rb', line 138

def to_s
  return self.xml_header + self.xml_docs.join + self.xml_footer
end

#to_xmlObject

Returns the encoded data as XML.



132
133
134
# File 'lib/indexer.rb', line 132

def to_xml
  return to_s
end