Class: Apstrings::Validator

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

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.resultObject

Returns the value of attribute result.



49
50
51
# File 'lib/apstrings/strings_validator.rb', line 49

def result
  @result
end

Class Method Details

.paredFile(file) ⇒ Object



173
174
175
176
# File 'lib/apstrings/strings_validator.rb', line 173

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

.validate(file, masterFile) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/apstrings/strings_validator.rb', line 52

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

	valid_master, valid_file , no_missing_key  = true,true,true

	begin
		@string_file = Validator::paredFile(file);
	rescue Exception => e
		Log::error(e)
		@result.valid_file_format = false
		@result.file_format_errors << e
		return false,@result.to_hash
	end

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

.validate_duplicates(sf) ⇒ Object



140
141
142
143
144
145
# File 'lib/apstrings/strings_validator.rb', line 140

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

.validate_format(sf) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/apstrings/strings_validator.rb', line 95

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

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

.validate_missing(sf, sf_masterFile) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/apstrings/strings_validator.rb', line 126

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

.validate_special_characters(sf) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/apstrings/strings_validator.rb', line 148

def self.validate_special_characters(sf)
	# puts "apstrings: checking syntax for #{file}..."
	variables_regex = /%[hlqLztj]?[@%dDuUxXoOfeEgGcCsSpaAF]/
	mismatchs = []
	sf.key_values.each {
		|e|  e.each_pair {
			|key,value|	
			fixed_key = Validator::value_in_master(key)	
			if fixed_key != nil
				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
			else
				# key in slavor file but does not exist in master file .
				Log.warn("There is missing key in master file comparing to slavor. As we only check  missing  keys in slavor file, just ignore. \n ");
			end
		}
	}
	mismatchs
end

.value_in_master(key) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/apstrings/strings_validator.rb', line 178

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