Class: Mabmapper::Engine

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

Direct Known Subclasses

AlephMabXmlEngine

Defined Under Namespace

Classes: Field

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.document_class(value) ⇒ Object



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

def document_class(value)
  self.document_class_value = value
end

.field(name, &block) ⇒ Object



71
72
73
74
# File 'lib/mabmapper/engine.rb', line 71

def field(name, &block)
  self.fields ||= []
  self.fields << Field.new(name, &block) if block_given?
end

Instance Method Details

#fieldsObject



28
29
30
# File 'lib/mabmapper/engine.rb', line 28

def fields
  self.class.fields
end

#process(input_filename, contents, options = {archive: nil}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/mabmapper/engine.rb', line 5

def process(input_filename, contents, options={archive: nil})
  document_class = self.class.document_class_value
  raise "The engine does't provide a document class. This is required." unless document_class.present?
  document = document_class.new(contents)
  fields.each do |field|
    begin
      field.send(:process, document)
    rescue => e
      puts "----------------------------"
      puts "Error  : #{e.message}"
      puts "Field  : #{field.name}"
      puts "File   : #{input_filename}"
      puts "Archive: #{options[:archive]}" if options[:archive].present?
      puts "----------------------------"
      puts "Backtrace:"
      puts e.backtrace
      puts "----------------------------"
      puts
    end
  end
  self
end

#to_hashObject



55
56
57
58
59
# File 'lib/mabmapper/engine.rb', line 55

def to_hash
  hash = {}
  fields.each { |field| hash[field.name.downcase] = field.result }
  hash
end

#to_xml(only_fields = []) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/mabmapper/engine.rb', line 32

def to_xml(only_fields = [])
  builder = Nokogiri::XML::Builder.new do |xml|
    xml.document do
      fields.each do |field|
        if field.result.present? || field.result == false
          next if only_fields.present? and not only_fields.include?(field.name)
          if field.result.is_a?(Array)
            #xml.send("#{field.name.downcase.pluralize}_") do
              field.result.each do |result|
                xml.send("#{field.name.downcase}_", result)
              end
            #end
          else
            xml.send("#{field.name.downcase}_", field.result)
          end
        end
      end
    end
  end

  builder.to_xml
end