Class: Libis::Format::Converter::Base

Inherits:
Object
  • Object
show all
Includes:
Tools::Logger
Defined in:
lib/libis/format/converter/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBase

Returns a new instance of Base.



20
21
22
23
# File 'lib/libis/format/converter/base.rb', line 20

def initialize
  @options = {}
  @flags = {}
end

Instance Attribute Details

#flagsObject (readonly)

Returns the value of attribute flags.



18
19
20
# File 'lib/libis/format/converter/base.rb', line 18

def flags
  @flags
end

#optionsObject (readonly)

Returns the value of attribute options.



18
19
20
# File 'lib/libis/format/converter/base.rb', line 18

def options
  @options
end

Class Method Details

.categoryObject



25
26
27
# File 'lib/libis/format/converter/base.rb', line 25

def self.category
  :converter
end

.inherited(klass) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/libis/format/converter/base.rb', line 67

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_typesObject

Raises:

  • (RuntimeError)


47
48
49
# File 'lib/libis/format/converter/base.rb', line 47

def self.input_types
  raise RuntimeError, 'Method #input_types needs to be overridden in converter'
end

.output_types(_format = nil) ⇒ Object

Raises:

  • (RuntimeError)


51
52
53
# File 'lib/libis/format/converter/base.rb', line 51

def self.output_types(_format = nil)
  raise RuntimeError, 'Method #output_types needs to be overridden in converter'
end

.using_temp(target) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/libis/format/converter/base.rb', line 59

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

Instance Method Details

#check_file_exist(file) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/libis/format/converter/base.rb', line 29

def check_file_exist(file)
  unless File.exist? file
    error "Cannot find file '#{file}'."
    return false
  end
  true
end

#convert(source, target, format, opts = {}) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/libis/format/converter/base.rb', line 37

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) ⇒ Object



55
56
57
# File 'lib/libis/format/converter/base.rb', line 55

def using_temp(target, &block)
  self.class.using_temp(target, &block)
end