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
59
# File 'backend/app/converters/converter.rb', line 55

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

Class Method Details

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

Raises:



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'backend/app/converters/converter.rb', line 132

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

    if converter
      if converter.respond_to?(:set_import_options)
        import_events = opts[:import_events]
        import_subjects = opts[:import_subjects]
        import_repository = opts[:import_repository]

        unless [import_events, import_subjects, import_repository].all?(&:nil?)
          converter.set_import_options({:import_events   => import_events,
                                        :import_subjects => import_subjects,
                                        :import_repository => import_repository})
        end
      end

      return converter
    end
  end

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

.import_optionsObject



89
90
91
# File 'backend/app/converters/converter.rb', line 89

def self.import_options
  @import_options
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



103
104
105
106
107
# File 'backend/app/converters/converter.rb', line 103

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)



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'backend/app/converters/converter.rb', line 114

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



94
95
96
97
98
99
100
# File 'backend/app/converters/converter.rb', line 94

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



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

def get_output_path
  @batch.get_output_path
end

#import_optionsObject



84
85
86
# File 'backend/app/converters/converter.rb', line 84

def import_options
  self.class.import_options
end

#remove_filesObject

forcibly remove files in the event of an interruption



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

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