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.



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

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

Instance Attribute Details

#flagsObject (readonly)

Returns the value of attribute flags.



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

def flags
  @flags
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

Class Method Details

.inherited(klass) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
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
# File 'lib/libis/format/converter/base.rb', line 53

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 = TypeDatabase.instance.mime_types(mimetype).first
      input_type? type_id
    end

    def output_mimetype?(mimetype)
      type_id = TypeDatabase.instance.mime_types(mimetype).first
      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)
      !TypeDatabase.ext_types(extension).first.nil?
    end

  end

end

.input_typesObject

Raises:

  • (RuntimeError)


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

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

.output_types(_format = nil) ⇒ Object

Raises:

  • (RuntimeError)


37
38
39
# File 'lib/libis/format/converter/base.rb', line 37

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

.using_temp(target) ⇒ Object



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

def Base.using_temp(target)
  tempfile = File.join(Dir.tmpdir, Dir::Tmpname.make_tmpname(['convert', File.extname(target)], File.basename(target, '.*').gsub(/\s/, '_')))
  result = yield tempfile
  return nil unless result
  FileUtils.move result, target
  target
end

Instance Method Details

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



24
25
26
27
28
29
30
31
# File 'lib/libis/format/converter/base.rb', line 24

def convert(source, target, format, opts = {})
  unless File.exist? source
    error "Cannot find file '#{source}'."
    return nil
  end
  @options.merge!(opts[:options]) if opts[:options]
  @flags.merge!(opts[:flags]) if opts[:flags]
end

#using_temp(target, &block) ⇒ Object



41
42
43
# File 'lib/libis/format/converter/base.rb', line 41

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