class Libis::Format::Converter::Base

Attributes

flags[R]
options[R]

Public Class Methods

category() click to toggle source
# File lib/libis/format/converter/base.rb, line 26
def self.category
  :converter
end
conversion?(input_type, output_type) click to toggle source
# File lib/libis/format/converter/base.rb, line 99
def conversion?(input_type, output_type)
  conversions[input_type] and conversions[input_type].any? { |t| t == output_type }
end
conversions() click to toggle source
# File lib/libis/format/converter/base.rb, line 74
def conversions
  input_types.inject({}) do |hash, input_type|
    hash[input_type] = output_types
    hash
  end
end
extension?(extension) click to toggle source
# File lib/libis/format/converter/base.rb, line 107
def extension?(extension)
  !Libis::Format::Library.get_field_by(:extension, extension, :format).nil?
end
inherited(klass) click to toggle source
# File lib/libis/format/converter/base.rb, line 68
def Base.inherited(klass)

  Repository.register klass

  class << self

    def conversions
      input_types.inject({}) do |hash, input_type|
        hash[input_type] = output_types
        hash
      end
    end

    def input_type?(type_id)
      input_types.include? type_id
    end

    def output_type?(type_id)
      output_types.include? type_id
    end

    def input_mimetype?(mimetype)
      type_id = Libis::Format::Library.get_field_by(:mimetype, mimetype, :format)
      input_type? type_id
    end

    def output_mimetype?(mimetype)
      type_id = Libis::Format::Library.get_field_by(:mimetype, mimetype, :format)
      output_type? type_id
    end

    def conversion?(input_type, output_type)
      conversions[input_type] and conversions[input_type].any? { |t| t == output_type }
    end

    def output_for(input_type)
      conversions[input_type]
    end

    def extension?(extension)
      !Libis::Format::Library.get_field_by(:extension, extension, :format).nil?
    end

  end

end
input_mimetype?(mimetype) click to toggle source
# File lib/libis/format/converter/base.rb, line 89
def input_mimetype?(mimetype)
  type_id = Libis::Format::Library.get_field_by(:mimetype, mimetype, :format)
  input_type? type_id
end
input_type?(type_id) click to toggle source
# File lib/libis/format/converter/base.rb, line 81
def input_type?(type_id)
  input_types.include? type_id
end
input_types() click to toggle source
# File lib/libis/format/converter/base.rb, line 48
def self.input_types
  raise RuntimeError, 'Method #input_types needs to be overridden in converter'
end
new() click to toggle source
# File lib/libis/format/converter/base.rb, line 21
def initialize
  @options = {}
  @flags = {}
end
output_for(input_type) click to toggle source
# File lib/libis/format/converter/base.rb, line 103
def output_for(input_type)
  conversions[input_type]
end
output_mimetype?(mimetype) click to toggle source
# File lib/libis/format/converter/base.rb, line 94
def output_mimetype?(mimetype)
  type_id = Libis::Format::Library.get_field_by(:mimetype, mimetype, :format)
  output_type? type_id
end
output_type?(type_id) click to toggle source
# File lib/libis/format/converter/base.rb, line 85
def output_type?(type_id)
  output_types.include? type_id
end
output_types(_format = nil) click to toggle source
# File lib/libis/format/converter/base.rb, line 52
def self.output_types(_format = nil)
  raise RuntimeError, 'Method #output_types needs to be overridden in converter'
end
using_temp(target) { |tempfile| ... } click to toggle source
# File lib/libis/format/converter/base.rb, line 60
def Base.using_temp(target)
  tempfile = Tools::TempFile.name("convert-#{File.basename(target, '.*').gsub(/\s/, '_')}", File.extname(target))
  result = yield tempfile
  return nil unless result
  FileUtils.move result, target
  target
end

Public Instance Methods

check_file_exist(file) click to toggle source
# File lib/libis/format/converter/base.rb, line 30
def check_file_exist(file)
  unless File.exist? file
    error "Cannot find file '#{file}'."
    return false
  end
  true
end
convert(source, target, format, opts = {}) click to toggle source
# File lib/libis/format/converter/base.rb, line 38
def convert(source, target, format, opts = {})
  if source.is_a?(Array)
    return nil unless source.map { |f| check_file_exist(f) }.reduce(:&)
  else
    return nil unless check_file_exist(source)
  end
  @options.merge!(opts[:options]) if opts[:options]
  @flags.merge!(opts[:flags]) if opts[:flags]
end
using_temp(target, &block) click to toggle source
# File lib/libis/format/converter/base.rb, line 56
def using_temp(target, &block)
  self.class.using_temp(target, &block)
end