class PhraseAppUpdater::LocaleFile

Attributes

content[R]
locale_name[R]
parsed_content[R]

Public Class Methods

class_for_file_format(type) click to toggle source
# File lib/phraseapp_updater/locale_file.rb, line 36
def class_for_file_format(type)
  case type.downcase
  when "json"
    JSONFile
  when "yml", "yaml"
    YAMLFile
  else
    raise BadFileTypeError.new("Invalid file type: #{type}")
  end
end
dump(_hash) click to toggle source
# File lib/phraseapp_updater/locale_file.rb, line 59
def dump(_hash)
  raise RuntimeError.new("Abstract method")
end
extension() click to toggle source
# File lib/phraseapp_updater/locale_file.rb, line 47
def extension
  raise RuntimeError.new("Abstract method")
end
from_file_content(locale_name, content) click to toggle source
# File lib/phraseapp_updater/locale_file.rb, line 15
def from_file_content(locale_name, content)
  new(locale_name, load(content))
end
from_hash(locale_name, hash) click to toggle source
# File lib/phraseapp_updater/locale_file.rb, line 11
def from_hash(locale_name, hash)
  new(locale_name, hash)
end
load(_content) click to toggle source
# File lib/phraseapp_updater/locale_file.rb, line 55
def load(_content)
  raise RuntimeError.new("Abstract method")
end
load_directory(directory) click to toggle source
# File lib/phraseapp_updater/locale_file.rb, line 19
def load_directory(directory)
  Dir[File.join(directory, "*.#{extension}")].map do |filename|
    load_file(filename)
  end
end
load_file(filename) click to toggle source
# File lib/phraseapp_updater/locale_file.rb, line 25
def load_file(filename)
  unless File.readable?(filename) && File.file?(filename)
    raise RuntimeError.new("Couldn't read localization file at #{filename}")
  end

  locale_name = File.basename(filename).chomp(".#{extension}")
  from_file_content(locale_name, File.read(filename))
end
new(locale_name, parsed_content) click to toggle source

Expects a Ruby hash

# File lib/phraseapp_updater/locale_file.rb, line 75
def initialize(locale_name, parsed_content)
  @locale_name    = locale_name
  @parsed_content = normalize_hash(parsed_content)
  @content        = self.class.dump(@parsed_content)
  freeze
end
phraseapp_type() click to toggle source
# File lib/phraseapp_updater/locale_file.rb, line 51
def phraseapp_type
  raise RuntimeError.new("Abstract method")
end

Public Instance Methods

filename() click to toggle source
# File lib/phraseapp_updater/locale_file.rb, line 68
def filename
  "#{locale_name}.#{self.class.extension}"
end
to_s() click to toggle source
# File lib/phraseapp_updater/locale_file.rb, line 64
def to_s
  locale_name
end

Private Instance Methods

normalize_hash(hash) click to toggle source
# File lib/phraseapp_updater/locale_file.rb, line 82
def normalize_hash(hash)
  hash.keys.sort_by(&:to_s).each_with_object({}) do |key, normalized_hash|
    val = hash[key]
    next if val == '' || (val.is_a?(Hash) && val.empty?)

    val = normalize_hash(val) if val.is_a?(Hash)
    key = key.to_s if key.is_a?(Symbol)
    normalized_hash[key] = val
  end
end