Class: Dnif::Document

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

Instance Method Summary collapse

Constructor Details

#initialize(object) ⇒ Document

Returns a new instance of Document.



4
5
6
# File 'lib/dnif/document.rb', line 4

def initialize(object)
  @object = object
end

Instance Method Details

#document_idObject



55
56
57
# File 'lib/dnif/document.rb', line 55

def document_id
  @object.send(@object.class.primary_key.to_sym) + encoded_class_name.split(',').sum { |c| c.to_i }
end

#generateObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/dnif/document.rb', line 8

def generate
  xml = Builder::XmlMarkup.new(:indent => 2)
  xml.sphinx(:document, :id => document_id) do
    fields = []
    @object.indexes.values.each do |index|
      (index.fields - fields).each do |name|
        if @object.index.fields.include?(name)
          xml.tag!(name) do
            xml.cdata!(@object.send(name).to_s)
          end
        else
          xml.tag!(name, "")
        end

        fields << name
      end
    end

    xml.class_id(@object.indexes.keys.index(@object.class.name))
    xml.class_name(encoded_class_name)

    attributes = []
    @object.indexes.values.each do |index|
      index.attributes.each do |name, type|
        next if attributes.include?(name)

        if @object.index.attributes.has_key?(name)
          value = @object.send(name)

          if [:date, :datetime].include?(type)
            if value.is_a?(Date)
              value = value.to_datetime
            end
            value = value.to_i
          end
        else
          value = ""
        end

        xml.tag!(name, value)
        attributes << name
      end
    end
  end
  xml.target!
end