Class: Expressir::Commands::NonAsciiViolationCollection
- Inherits:
-
Object
- Object
- Expressir::Commands::NonAsciiViolationCollection
- Defined in:
- lib/expressir/commands/validate_ascii.rb
Overview
Collection of all violations across multiple files
Instance Attribute Summary collapse
-
#file_violations ⇒ Object
readonly
Returns the value of attribute file_violations.
-
#total_files ⇒ Object
readonly
Returns the value of attribute total_files.
Instance Method Summary collapse
- #files_with_violations ⇒ Object
-
#initialize(check_remarks: false) ⇒ NonAsciiViolationCollection
constructor
A new instance of NonAsciiViolationCollection.
- #print_table_output ⇒ Object
- #print_text_output ⇒ Object
- #process_file(file) ⇒ Object
- #to_yaml_data ⇒ Object
- #total_occurrence_count ⇒ Object
- #total_violations ⇒ Object
- #unique_character_count ⇒ Object
Constructor Details
#initialize(check_remarks: false) ⇒ NonAsciiViolationCollection
Returns a new instance of NonAsciiViolationCollection.
124 125 126 127 128 129 |
# File 'lib/expressir/commands/validate_ascii.rb', line 124 def initialize(check_remarks: false) @file_violations = {} # Map of file paths to FileViolations objects @total_files = 0 @unicode_to_asciimath = nil @check_remarks = check_remarks end |
Instance Attribute Details
#file_violations ⇒ Object (readonly)
Returns the value of attribute file_violations.
122 123 124 |
# File 'lib/expressir/commands/validate_ascii.rb', line 122 def file_violations @file_violations end |
#total_files ⇒ Object (readonly)
Returns the value of attribute total_files.
122 123 124 |
# File 'lib/expressir/commands/validate_ascii.rb', line 122 def total_files @total_files end |
Instance Method Details
#files_with_violations ⇒ Object
143 144 145 |
# File 'lib/expressir/commands/validate_ascii.rb', line 143 def files_with_violations @file_violations.size end |
#print_table_output ⇒ Object
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 |
# File 'lib/expressir/commands/validate_ascii.rb', line 243 def print_table_output return if @file_violations.empty? # Build rows array rows = [] total_occurrences = 0 @file_violations.each_value do |file_violation| file_violation.unique_characters.each do |character| occurrence_count = character.occurrence_count total_occurrences += occurrence_count rows << { file: file_violation.display_path, symbol: "\"#{character.char}\" (#{character.hex})", replacement: character.replacement_text, occurrences: occurrence_count, } end end # Add total row rows << { file: "TOTAL", symbol: "#{unique_character_count} unique", replacement: "", occurrences: total_occurrences, } # Use TableTennis to render = { title: "Non-ASCII Characters Summary", columns: %i[file symbol replacement occurrences], headers: { file: "File", symbol: "Symbol", replacement: "Replacement", occurrences: "Occurrences", }, mark: ->(row) { row[:file] == "TOTAL" }, } puts "\n#{TableTennis.new(rows, )}\n" end |
#print_text_output ⇒ Object
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
# File 'lib/expressir/commands/validate_ascii.rb', line 184 def print_text_output # Print each file's violations if any unless @file_violations.empty? @file_violations.each_value do |file_violation| puts "\n#{Paint[file_violation.display_path, :cyan, :bold]}:" file_violation.violations.each do |v| puts " #{Paint['Line', :blue]} #{Paint[v[:line_number], :yellow]}, #{Paint['Column', :blue]} #{Paint[v[:column], :yellow]}:" puts " #{v[:line]}" puts " #{' ' * v[:column]}#{Paint['^' * v[:match].length, :red]} #{Paint['Non-ASCII sequence', :red]}" v[:char_details].each do |cd| character = file_violation.unique_characters.find do |c| c.char == cd[:char] end next unless character puts " #{Paint["\"#{cd[:char]}\"", :yellow]} - Hex: #{Paint[cd[:hex], :magenta]}, UTF-8 bytes: #{Paint[cd[:utf8], :magenta]}" puts " #{Paint['Replacement:', :green]} #{character.replacement_text}" end puts "" end puts " #{Paint['Found', :green]} #{Paint[file_violation.violation_count, :red]} #{Paint['non-ASCII sequence(s) in', :green]} #{Paint[file_violation.filename, :cyan]}\n" end end # Always print summary validation_scope = @check_remarks ? "code and remarks" : "code only (remarks excluded)" puts "\n#{Paint['Summary:', :blue, :bold]}" puts " #{Paint['Validation scope:', :green]} #{Paint[validation_scope, :cyan]}" puts " #{Paint['Scanned', :green]} #{Paint[@total_files, :yellow]} #{Paint['EXPRESS file(s)', :green]}" puts " #{Paint['Found', :green]} #{Paint[total_violations, :red]} #{Paint['non-ASCII sequence(s) in', :green]} #{Paint[files_with_violations, :red]} #{Paint['file(s)', :green]}" end |
#process_file(file) ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/expressir/commands/validate_ascii.rb', line 131 def process_file(file) @total_files += 1 # Initialize the mapping once @unicode_to_asciimath ||= build_unicode_to_asciimath_map file_violations = process_file_violations(file) return if file_violations.violations.empty? @file_violations[file] = file_violations end |
#to_yaml_data ⇒ Object
169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/expressir/commands/validate_ascii.rb', line 169 def to_yaml_data { summary: { total_files: @total_files, files_with_violations: files_with_violations, total_violations: total_violations, total_unique_characters: unique_character_count, total_occurrences: total_occurrence_count, }, violations: @file_violations.transform_keys do |k| File.(k) end.transform_values(&:to_h), } end |
#total_occurrence_count ⇒ Object
162 163 164 165 166 167 |
# File 'lib/expressir/commands/validate_ascii.rb', line 162 def total_occurrence_count # Sum all occurrences of all characters across all files @file_violations.values.sum do |file_violation| file_violation.unique_characters.sum(&:occurrence_count) end end |
#total_violations ⇒ Object
147 148 149 |
# File 'lib/expressir/commands/validate_ascii.rb', line 147 def total_violations @file_violations.values.sum(&:violation_count) end |
#unique_character_count ⇒ Object
151 152 153 154 155 156 157 158 159 160 |
# File 'lib/expressir/commands/validate_ascii.rb', line 151 def unique_character_count # Get total unique characters across all files all_chars = Set.new @file_violations.each_value do |file_violation| file_violation.unique_characters.each do |char| all_chars.add(char.char) end end all_chars.size end |