class MissingText::Diff

Attributes

current_batch_id[RW]
diffmap[RW]
files[RW]
hashes[RW]
langmap[RW]
languages[RW]
parent_dir[RW]
writer[RW]

Public Class Methods

new(options = []) click to toggle source
# File lib/missing_text/diff.rb, line 20
def initialize(options = [])
  setup!

  # save the name of the parent directory that we are operating on
  self.parent_dir = File.basename(File.expand_path("..", options[0][:path])) 

  options.each do |locale_file|

    # store all languages we are examining for this directory and the files we are examining
    
    parsed_locale = open_locale_file(locale_file)

    # if the file was unable to be parsed, it will be skipped. The warning for the file has already been logged, so the operation can continue
    if parsed_locale.present?

      languages << locale_file[:lang].try(:to_sym)
      files << locale_file

      parsed_locale.each do |lang, body|
        key = File.basename(files.last[:path], File.extname(files.last[:path])).to_sym
        hashes[key] = body
      end

    end
  end
end

Public Instance Methods

begin!(options = {}) click to toggle source

Runner

# File lib/missing_text/diff.rb, line 53
def begin!(options = {})
  create_langmap!
  create_diffmap!
  print_missing_translations
end
clear!()
Alias for: setup!
create_diffmap!() click to toggle source

KEYMAP DIFFs

# File lib/missing_text/diff.rb, line 86
def create_diffmap!
  # for each language, step through the other languages and find the diffs
  languages.each do |language|
    current_language = language

    # get all target languages we are examining
    target_languages = languages - [current_language]

    target_languages.each { |target_language| generate_diff_for_language(current_language, target_language) }
  end
end
create_langmap!() click to toggle source

call this after initialize in order to begin parsing all files

# File lib/missing_text/diff.rb, line 121
def create_langmap!
  self.langmap = {}
  languages.each do |lang|
    # initialize key paths for this language hash
    langmap[lang] = []

    # check to make sure that we have an entry for this filename
    # recursively build keymap
    make_keymap(langmap[lang], hashes[lang])
  end
end
generate_diff_for_language(current_language, target_language) click to toggle source

a diffmap shows what is missing between two languages the key is a two-element array, the first element is the current language and the second element is the target language

for example diffmap: {[:en, :fr] => [[:obj3], …]}

means that fr was examined against en, where en had an entry for obj3 that fr didn’t

# File lib/missing_text/diff.rb, line 107
def generate_diff_for_language(current_language, target_language)
  current_langmap = langmap[current_language]
  target_langmap = langmap[target_language]
  diffmap_key = [current_language, target_language]
  diffmap[diffmap_key] = current_langmap - target_langmap
end
make_keymap(langmap_entry, language) click to toggle source

outer method for creating keymap on parent hash

# File lib/missing_text/diff.rb, line 134
def make_keymap(langmap_entry, language)
  language.each do |key, value|
    if value.is_a? Hash
      make_keymap_for(langmap_entry, value, [key.to_sym])
    else
      langmap_entry << [key.to_sym]
    end
  end
end
make_keymap_for(langmap_entry, language, key_path) click to toggle source

recursive helper for creating keymap on children hashes

# File lib/missing_text/diff.rb, line 145
def make_keymap_for(langmap_entry, language, key_path)
  language.each do |key, value|
    # need a new value of this for every value we are looking at because we want all route traces to have single threading for when they are called again
    new_path = Array.new key_path
    if value.is_a? Hash
      make_keymap_for(langmap_entry, value, new_path.push(key.to_s.to_sym))
    else
      langmap_entry << new_path.push(key.to_s.to_sym)
    end
  end
end
print_missing_translations() click to toggle source

PRINT MISSING TRANSLATIONS

setup!() click to toggle source
# File lib/missing_text/diff.rb, line 10
def setup!
  self.hashes = {}
  self.languages = []
  self.diffmap = {}
  self.files = []
  self.current_batch_id = MissingText::Batch.last.id
end
Also aliased as: clear!
symbolize_keys_nested!(hash) click to toggle source
# File lib/missing_text/diff.rb, line 157
def symbolize_keys_nested!(hash) 
  hash.symbolize_keys!
  hash.values.each { |value| symbolize_keys_nested!(value) if value.is_a?(Hash)}
  hash
end

Private Instance Methods

open_locale_file(file) click to toggle source
# File lib/missing_text/diff.rb, line 165
def open_locale_file(file)
  if file[:type] == ".yml"
    parsed_file = open_yaml(file[:path])
  elsif file[:type] == ".rb"
    parsed_file = open_rb(file[:path], file[:lang])
  else
    # Just incase something has slipped through, at least tell the user that it won't be processed.
    MissingText::Warning.create(
      filename: file[:path],
      warning_type: MissingText::Warning::FILE_TYPE_ERROR,
      missing_text_batch_id: self.current_batch_id
      )
    parsed_file = {}
  end
  parsed_file
end
open_rb(rb, lang) click to toggle source
# File lib/missing_text/diff.rb, line 196
def open_rb(rb, lang)
  # It seems like when rb files are parsed, it doesn't keep the parent key. Just in case, we will use the language of the file name as the parent key.
  begin
    file = symbolize_keys_nested!(eval(File.read(rb)))
    file = {lang.to_sym => file}
  rescue => error
    MissingText::Warning.create(
      filename: rb,
      warning_type: MissingText::Warning::RB_PARSE,
      missing_text_batch_id: self.current_batch_id
      )
    file = {}
  end
  file
end
open_yaml(yml) click to toggle source
# File lib/missing_text/diff.rb, line 182
def open_yaml(yml)
  begin
    file = symbolize_keys_nested!(YAML.load_file(yml))
  rescue => error
    MissingText::Warning.create(
      filename: yml,
      warning_type: MissingText::Warning::YAML_PARSE,
      missing_text_batch_id: self.current_batch_id
    )
    file = {}
  end
  file
end