module I18n::Processes::Data::FileFormats

Public Class Methods

included(base) click to toggle source
# File lib/i18n/processes/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/processes/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/processes/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/processes/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/processes/data/file_formats.rb, line 55
def load_file(path)
  adapter_parse read_file(path), self.class.adapter_name_for_path(path)
end
normalized?(path, tree) click to toggle source
# File lib/i18n/processes/data/file_formats.rb, line 74
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/processes/data/file_formats.rb, line 36
def read_config(format)
  (config[format] || {})[:read]
end
read_file(path) click to toggle source

@return [String]

# File lib/i18n/processes/data/file_formats.rb, line 60
def read_file(path)
  ::File.read(path, encoding: 'UTF-8')
end
write_config(format) click to toggle source
# File lib/i18n/processes/data/file_formats.rb, line 32
def write_config(format)
  (config[format] || {})[:write]
end
write_tree(path, tree) click to toggle source
# File lib/i18n/processes/data/file_formats.rb, line 64
def write_tree(path, tree)
  hash = tree.to_hash(true)
  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