module I18n::Tasks::Data::FileFormats

Public Class Methods

included(base) click to toggle source
# File lib/i18n/tasks/data/file_formats.rb, line 9
def self.included(base)
  base.extend ClassMethods
end

Public Instance Methods

adapter_dump(tree, format) click to toggle source
# File lib/i18n/tasks/data/file_formats.rb, line 13
def adapter_dump(tree, format)
  adapter_op :dump, format, tree, write_config(format)
end
adapter_op(op, format, tree, config) click to toggle source
# File lib/i18n/tasks/data/file_formats.rb, line 22
def adapter_op(op, format, tree, config)
  self.class.adapter_by_name(format).send(op, tree, config)
rescue Exception => e # rubocop:disable Lint/RescueException
  raise CommandError, "#{format} #{op} error: #{e.message}"
end
adapter_parse(tree, format) click to toggle source

@return [Hash]

# File lib/i18n/tasks/data/file_formats.rb, line 18
def adapter_parse(tree, format)
  adapter_op :parse, format, tree, read_config(format)
end

Protected Instance Methods

load_file(path) click to toggle source

@return [Hash]

# File lib/i18n/tasks/data/file_formats.rb, line 39
def load_file(path)
  adapter_parse read_file(path), self.class.adapter_name_for_path(path)
rescue CommandError => e
  raise(e.class, "#{e.message} (file: #{path})")
end
normalized?(path, tree) click to toggle source
# File lib/i18n/tasks/data/file_formats.rb, line 61
def normalized?(path, tree)
  return false unless File.file?(path)

  read_file(path) == adapter_dump(tree.to_hash(true), self.class.adapter_name_for_path(path))
end
read_config(format) click to toggle source
# File lib/i18n/tasks/data/file_formats.rb, line 34
def read_config(format)
  (config[format] || {})[:read]
end
read_file(path) click to toggle source

@return [String]

# File lib/i18n/tasks/data/file_formats.rb, line 46
def read_file(path)
  ::File.read(path, encoding: 'UTF-8')
end
write_config(format) click to toggle source
# File lib/i18n/tasks/data/file_formats.rb, line 30
def write_config(format)
  (config[format] || {})[:write]
end
write_tree(path, tree, sort = true) click to toggle source
# File lib/i18n/tasks/data/file_formats.rb, line 50
def write_tree(path, tree, sort = true)
  hash = tree.to_hash(sort)
  adapter = self.class.adapter_name_for_path(path)
  content = adapter_dump(hash, adapter)
  # Ignore unchanged data
  return if File.file?(path) && content == read_file(path)

  ::FileUtils.mkpath(File.dirname(path))
  ::File.open(path, 'w') { |f| f.write content }
end