Class: Applocale::CompareStringFiles

Inherits:
Object
  • Object
show all
Defined in:
lib/applocale/Core/CompareStringFile/compare_string_files.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(platform, applocale_file1, applocale_file2, setting1, setting2, result_file) ⇒ CompareStringFiles

Returns a new instance of CompareStringFiles.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/applocale/Core/CompareStringFile/compare_string_files.rb', line 30

def initialize(platform, applocale_file1, applocale_file2, setting1, setting2, result_file)
  @platform = platform
  @applocale_file1 = applocale_file1
  @applocale_file2 = applocale_file2
  @setting1 = setting1
  @setting2 = setting2
  @result_file = result_file
  @langpath_comparison_list = @setting1.lang_path_list.map do |lang_path_obj|
    file1 = lang_path_obj.filepath
    file2 = @setting2.lang_path_list.detect { |e| e.lang == lang_path_obj.lang }&.filepath
    Applocale::Config::LangPathComparison.new(platform, lang_path_obj.lang, file1,  file2)
  end
  compare_all
end

Instance Attribute Details

#applocale_file1Object (readonly)

Returns the value of attribute applocale_file1.



9
10
11
# File 'lib/applocale/Core/CompareStringFile/compare_string_files.rb', line 9

def applocale_file1
  @applocale_file1
end

#applocale_file2Object (readonly)

Returns the value of attribute applocale_file2.



9
10
11
# File 'lib/applocale/Core/CompareStringFile/compare_string_files.rb', line 9

def applocale_file2
  @applocale_file2
end

#langpath_comparison_listObject (readonly)

Returns the value of attribute langpath_comparison_list.



9
10
11
# File 'lib/applocale/Core/CompareStringFile/compare_string_files.rb', line 9

def langpath_comparison_list
  @langpath_comparison_list
end

#platformObject (readonly)

Returns the value of attribute platform.



9
10
11
# File 'lib/applocale/Core/CompareStringFile/compare_string_files.rb', line 9

def platform
  @platform
end

#result_fileObject (readonly)

Returns the value of attribute result_file.



9
10
11
# File 'lib/applocale/Core/CompareStringFile/compare_string_files.rb', line 9

def result_file
  @result_file
end

#setting1Object (readonly)

Returns the value of attribute setting1.



9
10
11
# File 'lib/applocale/Core/CompareStringFile/compare_string_files.rb', line 9

def setting1
  @setting1
end

#setting2Object (readonly)

Returns the value of attribute setting2.



9
10
11
# File 'lib/applocale/Core/CompareStringFile/compare_string_files.rb', line 9

def setting2
  @setting2
end

Instance Method Details

#compare_allObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/applocale/Core/CompareStringFile/compare_string_files.rb', line 10

def compare_all
  results = @langpath_comparison_list.map do |langpath_comparison|
    lang = langpath_comparison.lang
    file1 = langpath_comparison.filepath1
    file2 = langpath_comparison.filepath2
    if file1.nil? or file2.nil?
      Applocale::Config::LangPathComparisonResult.new(langpath_comparison, "Warning: [#{lang}] missing files for comparison!!!!!")
    else
      Applocale::CompareStringFile.compare(langpath_comparison)
    end
  end
  results.each {| result |
    unless result.warning.nil?
      puts result.warning.yellow
    end
  }
  write_result_to_csv(results)
  puts "Comparison Finished, output: #{result_file} !!!".green
end

#write_result_to_csv(results) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/applocale/Core/CompareStringFile/compare_string_files.rb', line 45

def write_result_to_csv(results)
  filtered_results = results.select do | result |
    result.warning.nil?
  end
  columns = filtered_results
              .map { |result| result.lang }
              .flat_map do | lang |
    ["notSame", "duplicateKey", "mismatch", "missingKeyIn1st", "missingkeyIn2nd"].map { |column| "#{lang}: #{column}" }
  end
  values =  filtered_results.flat_map { |result|
    [result.not_same,
    result.duplicate_key,
    result.mismatch,
    result.missing_in_1,
    result.missing_in_2]
  }
  values_max_length = values.max { |a,b| a.length <=> b.length }.length
  _first_value, *other_values = values
  first_value = values_max_length.times.collect { |i| _first_value[i] }
  csv_values = first_value.zip(*other_values)
  CSV.open(@result_file, "w") do | csv |
    csv << columns
    csv_values.each { | row |  csv << row }
  end
end