Class: Translatomatic::Converter

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/translatomatic/converter.rb

Overview

Converts files from one format to another

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Converter

Returns a new instance of Converter.



4
5
6
# File 'lib/translatomatic/converter.rb', line 4

def initialize(options = {})
  @options = options
end

Instance Method Details

#convert(source, target) ⇒ Translatomatic::ResourceFile

Convert a resource file from one format to another.

Parameters:

  • source (String)

    Path to source file. File must exist

  • target (String)

    Path to target file. File will be created or overwritten if it already exists.

Returns:



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# 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