Class: MongoSphinx::Indexer::XMLDoc

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

Overview

Class XMLDoc wraps the XML representation of a single document to index and contains a complete “sphinx:document” tag.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, class_name, properties) ⇒ XMLDoc

Creates a XMLDoc object from the provided ID, class name and data.

Parameters:

id

ID of the object to index

class_name

Name of the class

data

Hash with the properties to index

Raises:

  • (ArgumentError)


181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/indexer.rb', line 181

def initialize(id, class_name, properties)
  raise ArgumentError, 'Missing id' if id.nil?
  raise ArgumentError, 'Missing class_name' if class_name.nil?

  xml = "<sphinx:document id=\"#{id}\">"

  xml << '<csphinx-class>'
  xml << MongoSphinx::MultiAttribute.encode(class_name)
  xml << '</csphinx-class>'
  xml << "<classname>#{class_name}</classname>"

  properties.each do |key, value|
    xml << "<#{key}><![CDATA[[#{value}]]></#{key}>"
  end

  xml << '</sphinx:document>'

  @xml = xml

  @id = id
  @class_name = class_name
end

Instance Attribute Details

#class_nameObject (readonly)

Returns the class name of the encoded data.



154
155
156
# File 'lib/indexer.rb', line 154

def class_name
  @class_name
end

#idObject (readonly)

Returns the ID of the encoded data.



150
151
152
# File 'lib/indexer.rb', line 150

def id
  @id
end

#xmlObject (readonly)

Returns the encoded data.



158
159
160
# File 'lib/indexer.rb', line 158

def xml
  @xml
end

Class Method Details

.from_object(object) ⇒ Object

Creates a XMLDoc object from the provided CouchRest object.

Parameters:

object

Object to index

Raises:

  • (ArgumentError)


166
167
168
169
170
171
# File 'lib/indexer.rb', line 166

def self.from_object(object)
  raise ArgumentError, 'Missing object' if object.nil?
  raise ArgumentError, 'No compatible ID' if (id = object.sphinx_id).nil?

  return new(id, object.class.to_s, object.fulltext_attributes)
end

Instance Method Details

#to_sObject

Returns the encoded data as XML.



212
213
214
# File 'lib/indexer.rb', line 212

def to_s
  return self.xml
end

#to_xmlObject

Returns the encoded data as XML.



206
207
208
# File 'lib/indexer.rb', line 206

def to_xml
  return to_s
end