Class: Suma::Cli::FileViolations
- Inherits:
-
Object
- Object
- Suma::Cli::FileViolations
- Defined in:
- lib/suma/cli/validate_ascii.rb
Overview
Represents all non-ASCII characters in a file
Instance Attribute Summary collapse
-
#directory ⇒ Object
readonly
Returns the value of attribute directory.
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#unique_characters ⇒ Object
readonly
Returns the value of attribute unique_characters.
-
#violations ⇒ Object
readonly
Returns the value of attribute violations.
Instance Method Summary collapse
- #add_violation(line_number, column, match, char_details, line) ⇒ Object
- #display_path ⇒ Object
- #full_path ⇒ Object
-
#initialize(file_path) ⇒ FileViolations
constructor
A new instance of FileViolations.
- #to_h ⇒ Object
- #violation_count ⇒ Object
Constructor Details
#initialize(file_path) ⇒ FileViolations
Returns a new instance of FileViolations.
61 62 63 64 65 66 67 |
# File 'lib/suma/cli/validate_ascii.rb', line 61 def initialize(file_path) @path = file_path @filename = File.basename(file_path) @directory = File.dirname(file_path) @characters = {} # Map of characters to NonAsciiCharacter objects @violations = [] # List of violations (line, column, etc.) end |
Instance Attribute Details
#directory ⇒ Object (readonly)
Returns the value of attribute directory.
59 60 61 |
# File 'lib/suma/cli/validate_ascii.rb', line 59 def directory @directory end |
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
59 60 61 |
# File 'lib/suma/cli/validate_ascii.rb', line 59 def filename @filename end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
59 60 61 |
# File 'lib/suma/cli/validate_ascii.rb', line 59 def path @path end |
#unique_characters ⇒ Object (readonly)
Returns the value of attribute unique_characters.
59 60 61 |
# File 'lib/suma/cli/validate_ascii.rb', line 59 def unique_characters @unique_characters end |
#violations ⇒ Object (readonly)
Returns the value of attribute violations.
59 60 61 |
# File 'lib/suma/cli/validate_ascii.rb', line 59 def violations @violations end |
Instance Method Details
#add_violation(line_number, column, match, char_details, line) ⇒ Object
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 94 95 96 |
# File 'lib/suma/cli/validate_ascii.rb', line 69 def add_violation(line_number, column, match, char_details, line) violation = { line_number: line_number, column: column, match: match, char_details: char_details, line: line, } @violations << violation # Register each character char_details.each do |detail| char = detail[:char] unless @characters[char] @characters[char] = NonAsciiCharacter.new( char, detail[:hex], detail[:utf8], detail[:is_math], detail[:replacement], detail[:replacement_type], ) end @characters[char].add_occurrence(line_number, column, line) end end |
#display_path ⇒ Object
106 107 108 |
# File 'lib/suma/cli/validate_ascii.rb', line 106 def display_path "#{File.basename(@directory)}/#{@filename}" end |
#full_path ⇒ Object
110 111 112 |
# File 'lib/suma/cli/validate_ascii.rb', line 110 def full_path File.(@path) end |
#to_h ⇒ Object
114 115 116 117 118 119 120 |
# File 'lib/suma/cli/validate_ascii.rb', line 114 def to_h { file: display_path, count: violation_count, non_ascii_characters: unique_characters.map(&:to_h), } end |
#violation_count ⇒ Object
98 99 100 |
# File 'lib/suma/cli/validate_ascii.rb', line 98 def violation_count @violations.size end |