Class: Libis::Format::Converter::Base
- Inherits:
-
Object
- Object
- Libis::Format::Converter::Base
- Includes:
- Tools::Logger
- Defined in:
- lib/libis/format/converter/base.rb
Direct Known Subclasses
AudioConverter, EmailConverter, FopPdfConverter, ImageConverter, Jp2Converter, OfficeConverter, PdfConverter, SpreadsheetConverter, VideoConverter, XsltConverter
Instance Attribute Summary collapse
-
#flags ⇒ Object
readonly
Returns the value of attribute flags.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Class Method Summary collapse
-
.inherited(klass) ⇒ Object
rubocop:disable Lint/MissingSuper.
- .input_types ⇒ Object
- .output_types(_format = nil) ⇒ Object
- .using_temp(target) ⇒ Object
Instance Method Summary collapse
- #convert(source, _target, _format, opts = {}) ⇒ Object
-
#initialize ⇒ Base
constructor
A new instance of Base.
- #using_temp(target, &block) ⇒ Object
Constructor Details
#initialize ⇒ Base
Returns a new instance of Base.
19 20 21 22 |
# File 'lib/libis/format/converter/base.rb', line 19 def initialize = {} @flags = {} end |
Instance Attribute Details
#flags ⇒ Object (readonly)
Returns the value of attribute flags.
17 18 19 |
# File 'lib/libis/format/converter/base.rb', line 17 def flags @flags end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
17 18 19 |
# File 'lib/libis/format/converter/base.rb', line 17 def end |
Class Method Details
.inherited(klass) ⇒ Object
rubocop:disable Lint/MissingSuper
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 |
# File 'lib/libis/format/converter/base.rb', line 54 def self.inherited(klass) # rubocop:disable Lint/MissingSuper Repository.register klass class << self def conversions input_types.each_with_object({}) do |input_type, hash| hash[input_type] = output_types 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]&.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_types ⇒ Object
33 34 35 |
# File 'lib/libis/format/converter/base.rb', line 33 def self.input_types raise 'Method #input_types needs to be overridden in converter' end |
.output_types(_format = nil) ⇒ Object
37 38 39 |
# File 'lib/libis/format/converter/base.rb', line 37 def self.output_types(_format = nil) raise 'Method #output_types needs to be overridden in converter' end |
.using_temp(target) ⇒ Object
45 46 47 48 49 50 51 52 |
# File 'lib/libis/format/converter/base.rb', line 45 def self.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
#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 .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 |