Class: XMLPipe2::XMLStream

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

Constant Summary collapse

TEMPLATE =
File.read(File.expand_path(File.dirname(__FILE__) + '/../../erb/xml_stream.xml.erb'))

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(instance) ⇒ XMLStream

Returns a new instance of XMLStream.



7
8
9
# File 'lib/xml_pipe2/xml_stream.rb', line 7

def initialize(instance)
  @instance = instance
end

Class Attribute Details

.sphinx_id_methodObject

Returns the value of attribute sphinx_id_method.



12
13
14
# File 'lib/xml_pipe2/xml_stream.rb', line 12

def sphinx_id_method
  @sphinx_id_method
end

Instance Attribute Details

#document_idObject

Returns the value of attribute document_id.



16
17
18
# File 'lib/xml_pipe2/xml_stream.rb', line 16

def document_id
  @document_id
end

#instanceObject (readonly)

Returns the value of attribute instance.



15
16
17
# File 'lib/xml_pipe2/xml_stream.rb', line 15

def instance
  @instance
end

Instance Method Details

#generateObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/xml_pipe2/xml_stream.rb', line 18

def generate
  self.document_id = self.class.sphinx_id_method ?  instance.send(self.class.sphinx_id_method) : instance.id

  fields = instance.class.sphinx_indexes.inject({ }) do |hash, index|
    if index[:options].has_key?(:as)
      hash.merge!(index[:options][:as] => instance.send(index[:method]))
    else
      hash.merge!(index[:method] => instance.send(index[:method]))
    end
    hash
  end
  attrs = instance.class.sphinx_attributes.inject({ }) do |hash, attr|
    if attr[:options].has_key?(:as)
      hash.merge!(attr[:options][:as] => instance.send(attr[:method]))
    else
      hash.merge!(attr[:method] => instance.send(attr[:method]))
    end
    hash
  end
  ERB.new(TEMPLATE).result(binding)
end