Class: Converter

Inherits:
Object
  • Object
show all
Defined in:
backend/app/converters/converter.rb

Overview

Converter is an interface used to implement new importer types. To implement your own converter, create a subclass of this class and implement the “IMPLEMENT ME” methods marked below.

Defined Under Namespace

Classes: ConverterMappingError, ConverterNotFound

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input_file) ⇒ Converter

Returns a new instance of Converter.



55
56
57
58
# File 'backend/app/converters/converter.rb', line 55

def initialize(input_file)
  @input_file = input_file
  @batch = ASpaceImport::RecordBatch.new
end

Class Method Details

.for(type, input_file, opts = {}) ⇒ Object

Raises:



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'backend/app/converters/converter.rb', line 121

def self.for(type, input_file, opts = {})
  Array(@converters).each do |converter|
    converter = converter.instance_for(type, input_file)

    if converter && converter.respond_to?(:set_import_events)
      if opts[:import_events]
        converter.set_import_events
      else
        converter.unset_import_events
      end
    end

    return converter if converter
  end

  raise ConverterNotFound.new("No suitable converter found for #{type}")
end

.import_types(show_hidden = false) ⇒ Object

Implement this in your Converter class!

Returns descriptive metadata for the import type(s) implemented by this Converter.

Raises:

  • (NotImplementedError)


14
15
16
17
18
19
20
21
22
23
24
# File 'backend/app/converters/converter.rb', line 14

def self.import_types(show_hidden = false)
  raise NotImplementedError.new

  # Example:
  [
   {
     :name => "my_import_type",
     :description => "Description of new importer"
   }
  ]
end

.inherited(subclass) ⇒ Object



92
93
94
95
96
# File 'backend/app/converters/converter.rb', line 92

def self.inherited(subclass)
  # We name Converter explicitly so that subclasses of subclasses still get
  # registered at the top-most level.
  Converter.register_converter(subclass)
end

.instance_for(type, input_file) ⇒ Object

Implement this in your Converter class!

If this Converter will handle type and input_file, return an instance.

Raises:

  • (NotImplementedError)


29
30
31
32
33
34
35
36
37
38
# File 'backend/app/converters/converter.rb', line 29

def self.instance_for(type, input_file)
  raise NotImplementedError.new

  # Example:
  if type == "my_import_type"
    self.new(input_file)
  else
    nil
  end
end

.list_import_types(show_hidden = false) ⇒ Object

List all available import types. Subclasses have the option of hiding certain import types that they actually support (for example, for suppressing imports that exist to support a plugin or user script, but shouldn’t be shown to end users)



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'backend/app/converters/converter.rb', line 103

def self.list_import_types(show_hidden = false)
  seen_types = {}

  Array(@converters).map {|converter|
    converter.import_types(show_hidden).map {|import|
      # Plugins might define their own converters that replace the standard
      # ones.  Only show one instance of each importer.
      if seen_types[import[:name]]
        nil
      else
        seen_types[import[:name]] = true
        import
      end
    }
  }.flatten(1).compact
end

.register_converter(subclass) ⇒ Object



83
84
85
86
87
88
89
# File 'backend/app/converters/converter.rb', line 83

def self.register_converter(subclass)
  @converters ||= []

  # Add the most recently created subclass to the beginning of the list so we
  # give it preference when searching.
  @converters.unshift(subclass)
end

Instance Method Details

#get_output_pathObject



61
62
63
# File 'backend/app/converters/converter.rb', line 61

def get_output_path
  @batch.get_output_path
end

#remove_filesObject

forcibly remove files in the event of an interruption



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'backend/app/converters/converter.rb', line 67

def remove_files
  @batch.each_open_file_path do |path|
    3.times do |i|
      begin
        File.unlink(path)
        break
      rescue Errno::EACCES # sometimes windows does not like this. let's wait and retry.
        sleep(1) # just in case it's open by something else..
        next unless i == 2
        $stderr.puts "Cannot remove #{path}...giving up."
      end
    end
  end
end

#runObject

Implement this in your Converter class!

Process @input_file and load records into @batch.

Raises:

  • (NotImplementedError)


43
44
45
# File 'backend/app/converters/converter.rb', line 43

def run
  raise NotImplementedError.new
end