Class: Expressir::Commands::FileViolations

Inherits:
Object
  • Object
show all
Defined in:
lib/expressir/commands/validate_ascii.rb

Overview

Represents all non-ASCII characters in a file

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_path) ⇒ FileViolations

Returns a new instance of FileViolations.



58
59
60
61
62
63
64
# File 'lib/expressir/commands/validate_ascii.rb', line 58

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

#directoryObject (readonly)

Returns the value of attribute directory.



56
57
58
# File 'lib/expressir/commands/validate_ascii.rb', line 56

def directory
  @directory
end

#filenameObject (readonly)

Returns the value of attribute filename.



56
57
58
# File 'lib/expressir/commands/validate_ascii.rb', line 56

def filename
  @filename
end

#pathObject (readonly)

Returns the value of attribute path.



56
57
58
# File 'lib/expressir/commands/validate_ascii.rb', line 56

def path
  @path
end

#unique_charactersObject (readonly)

Returns the value of attribute unique_characters.



56
57
58
# File 'lib/expressir/commands/validate_ascii.rb', line 56

def unique_characters
  @unique_characters
end

#violationsObject (readonly)

Returns the value of attribute violations.



56
57
58
# File 'lib/expressir/commands/validate_ascii.rb', line 56

def violations
  @violations
end

Instance Method Details

#add_violation(line_number, column, match, char_details, line) ⇒ Object



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/expressir/commands/validate_ascii.rb', line 66

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_pathObject



103
104
105
# File 'lib/expressir/commands/validate_ascii.rb', line 103

def display_path
  "#{File.basename(@directory)}/#{@filename}"
end

#full_pathObject



107
108
109
# File 'lib/expressir/commands/validate_ascii.rb', line 107

def full_path
  File.expand_path(@path)
end

#to_hObject



111
112
113
114
115
116
117
# File 'lib/expressir/commands/validate_ascii.rb', line 111

def to_h
  {
    file: display_path,
    count: violation_count,
    non_ascii_characters: unique_characters.map(&:to_h),
  }
end

#violation_countObject



95
96
97
# File 'lib/expressir/commands/validate_ascii.rb', line 95

def violation_count
  @violations.size
end