Class: MongoSphinx::Indexer::XMLDocset
- Inherits:
-
Object
- Object
- MongoSphinx::Indexer::XMLDocset
- 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
-
#xml_docs ⇒ Object
readonly
Objects contained in document set.
-
#xml_footer ⇒ Object
readonly
XML generated for closing the document.
-
#xml_header ⇒ Object
readonly
XML generated for opening the document.
Instance Method Summary collapse
-
#initialize(rows = []) ⇒ XMLDocset
constructor
Creates a XMLDocset object from the provided data.
-
#to_s ⇒ Object
Returns the encoded data as XML.
-
#to_xml ⇒ Object
Returns the encoded data as XML.
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
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 = '</sphinx:docset>' end |
Instance Attribute Details
#xml_docs ⇒ Object (readonly)
Objects contained in document set.
67 68 69 |
# File 'lib/indexer.rb', line 67 def xml_docs @xml_docs end |
#xml_footer ⇒ Object (readonly)
XML generated for closing the document.
75 76 77 |
# File 'lib/indexer.rb', line 75 def end |
#xml_header ⇒ Object (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_s ⇒ Object
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. end |
#to_xml ⇒ Object
Returns the encoded data as XML.
132 133 134 |
# File 'lib/indexer.rb', line 132 def to_xml return to_s end |