Class: MissingText::Diff

Inherits:
Object
  • Object
show all
Defined in:
lib/missing_text/diff.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = []) ⇒ Diff

Returns a new instance of Diff.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# 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

Instance Attribute Details

#current_batch_idObject

Returns the value of attribute current_batch_id.



8
9
10
# File 'lib/missing_text/diff.rb', line 8

def current_batch_id
  @current_batch_id
end

#diffmapObject

Returns the value of attribute diffmap.



8
9
10
# File 'lib/missing_text/diff.rb', line 8

def diffmap
  @diffmap
end

#filesObject

Returns the value of attribute files.



8
9
10
# File 'lib/missing_text/diff.rb', line 8

def files
  @files
end

#hashesObject

Returns the value of attribute hashes.



8
9
10
# File 'lib/missing_text/diff.rb', line 8

def hashes
  @hashes
end

#langmapObject

Returns the value of attribute langmap.



8
9
10
# File 'lib/missing_text/diff.rb', line 8

def langmap
  @langmap
end

#languagesObject

Returns the value of attribute languages.



8
9
10
# File 'lib/missing_text/diff.rb', line 8

def languages
  @languages
end

#parent_dirObject

Returns the value of attribute parent_dir.



8
9
10
# File 'lib/missing_text/diff.rb', line 8

def parent_dir
  @parent_dir
end

#writerObject

Returns the value of attribute writer.



8
9
10
# File 'lib/missing_text/diff.rb', line 8

def writer
  @writer
end

Instance Method Details

#begin!(options = {}) ⇒ Object

Runner



53
54
55
56
57
# File 'lib/missing_text/diff.rb', line 53

def begin!(options = {})
  create_langmap!
  create_diffmap!
  print_missing_translations
end

#create_diffmap!Object

KEYMAP DIFFs



86
87
88
89
90
91
92
93
94
95
96
# 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!Object

call this after initialize in order to begin parsing all files



121
122
123
124
125
126
127
128
129
130
131
# 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) ⇒ Object

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: :fr] => [[:obj3], …]

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



107
108
109
110
111
112
# 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) ⇒ Object

outer method for creating keymap on parent hash



134
135
136
137
138
139
140
141
142
# 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) ⇒ Object

recursive helper for creating keymap on children hashes



145
146
147
148
149
150
151
152
153
154
155
# 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



65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/missing_text/diff.rb', line 65

def print_missing_translations
  # if there is nothing to detect, then just return
  return unless self.diffmap.present?
  
  self.writer = MissingText::Writer.new({
    languages: self.languages,
    diffmap: self.diffmap,
    langmap: self.langmap,
    files: self.files,
    hashes: self.hashes,
    parent_dir: self.parent_dir
    })
  self.writer.write
end

#setup!Object Also known as: clear!



10
11
12
13
14
15
16
# 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

#symbolize_keys_nested!(hash) ⇒ Object



157
158
159
160
161
# 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