Class: Apstrings::Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/apstrings/strings_validator.rb

Class Method Summary collapse

Class Method Details

.paredFile(file) ⇒ Object



107
108
109
110
# File 'lib/apstrings/strings_validator.rb', line 107

def self.paredFile(file)
  file = Reader.read(file)
  StringsParser.new(file).parse_file
end

.validate(file, masterFile) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/apstrings/strings_validator.rb', line 8

def self.validate(file,masterFile)
  @master = nil
  # puts "apstrings: start validate  strings file  ..."
  if nil == masterFile
    Log::warn("No master file provided, validating file format for #{file} only ...")
  else
    @master = Validator::paredFile(masterFile)
  end

  valid_master, valid_file , no_missing_key  = true,true,true

  valid_file = Validator::validate_format(file)
  if masterFile != nil
    valid_master = Validator::validate_format(masterFile)
    no_missing_key = Validator::validate_missing(file,masterFile)
  end
  
  if valid_master && valid_file &&  no_missing_key
    # Log::info("Yeah! 🍻 🍻 ")
    return true
  else if valid_master && valid_file && !no_missing_key
      Log::warn("Missing keys found.")
      return true
    else
      Log::error("Oh no! Invalid file.")
      return false
    end
  end
end

.validate_duplicates(file) ⇒ Object



77
78
79
80
81
82
83
# File 'lib/apstrings/strings_validator.rb', line 77

def self.validate_duplicates(file)
  # puts "apstrings: checking dup-keys for #{file}..."
  sf = Validator::paredFile(file)
  sf.keys.detect {
    |e| sf.keys.count(e) > 1
  }
end

.validate_format(file) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/apstrings/strings_validator.rb', line 38

def self.validate_format(file)
  is_valid = true
  # puts "apstrings: start validate format for #{file} ..."
  dup_keys_in_file = Validator::validate_duplicates(file)
  mismatchs_in_file = Validator::validate_special_characters(file)
  if nil != dup_keys_in_file && !dup_keys_in_file.empty?
    Log::warn("Dup-keys found in #{file}: \n `#{dup_keys_in_file}`.")
  else
    # Log::info("OK . .")
  end

  if !mismatchs_in_file.empty?
    is_valid = false
    mismatchs_in_file.each { |e| e.each_pair {
        |key,value|
        Log::error("Mismatch format found in `#{file}`: \n `#{key}` ====>  `#{value}`")
      }
    }
  else
    # Log::info("OK ...  \n ")
  end
  is_valid
end

.validate_missing(file, masterFile) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/apstrings/strings_validator.rb', line 62

def self.validate_missing(file,masterFile)
  # puts "apstrings: checking missing keys for #{file}..."
  sf = Validator::paredFile(file)
  sf_masterFile = Validator::paredFile(masterFile)
  no_missing = true
  missing_keys = sf_masterFile.keys - sf.keys
  if !missing_keys.empty?
    no_missing =false
    Log::warn("#{missing_keys.count.to_s} missing keys found in #{file} comparing to master file: #{masterFile} : \n #{missing_keys.to_s}")
  else
    # Log::info("OK...")
  end
  no_missing
end

.validate_special_characters(file) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/apstrings/strings_validator.rb', line 86

def self.validate_special_characters(file)
  # puts "apstrings: checking syntax for #{file}..."
  sf = Validator::paredFile(file)
  variables_regex = /%[hlqLztj]?[@%dDuUxXoOfeEgGcCsSpaAF]/
  mismatchs = []
  sf.key_values.each {
    |e|  e.each_pair {
      |key,value|  
      fixed_key = Validator::value_in_master(key)  
      striped_key =  fixed_key.gsub(/%\d\$/,'%') # Strip numbered format placeholders , e.g. %1$@ --> %@ 
      striped_value = value.gsub(/%\d\$/,'%')   
      key_variables = striped_key.scan(variables_regex) 
      value_variables = striped_value.scan(variables_regex) 
      if !(key_variables.sort == value_variables.sort)
        mismatchs << {key => value}   
      end
    }
  }
  mismatchs
end

.value_in_master(key) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/apstrings/strings_validator.rb', line 112

def self.value_in_master(key)
  if @master
    value_comment = @master.to_hash[key] # 
    if value_comment == nil
      return key 
    else
      return value_comment.keys[0]
    end
  else
    key
  end
end