class Translatomatic::Converter

Converts files from one format to another

Public Class Methods

new(options = {}) click to toggle source
# File lib/translatomatic/converter.rb, line 4
def initialize(options = {})
  @options = options
end

Public Instance Methods

convert(source, target) click to toggle source

Convert a resource file from one format to another. @param source [String] Path to source file. File must exist @param target [String] Path to target file. File will be created

or overwritten if it already exists.

@return [Translatomatic::ResourceFile] The converted file.

# File lib/translatomatic/converter.rb, line 13
def convert(source, target)
  source_file = load_file(source)
  target_file = load_file(target)
  raise t('file.not_found', file: source) unless source_file.path.file?

  if source_file.type == target_file.type
    # if same file type, modify source.
    # this retains internal structure
    target_file = source_file
  else
    # different file type, copy properties from source file to target
    target_file.properties = source_file.properties
  end

  target_file.save(Pathname.new(target), @options)
  target_file
end

Private Instance Methods

load_file(path) click to toggle source
# File lib/translatomatic/converter.rb, line 35
def load_file(path)
  path = Pathname.new(path.to_s)
  raise t('file.directory', file: path) if path.directory?

  file = if path.exist?
           Translatomatic::ResourceFile.load(path)
         else
           Translatomatic::ResourceFile.create(path)
         end
  raise t('file.unsupported', file: path) unless file
  file
end