Class: Expressir::Coverage::Report

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

Overview

Represents a documentation coverage report for EXPRESS schemas

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repository, skip_types = [], ignored_files = {}) ⇒ Report

Initialize a coverage report

Parameters:

  • repository (Expressir::Model::Repository)

    The repository to analyze

  • skip_types (Array<String>) (defaults to: [])

    Array of entity type names to skip from coverage

  • ignored_files (Hash) (defaults to: {})

    Hash mapping absolute file paths to their matched patterns



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/expressir/coverage.rb', line 68

def initialize(repository, skip_types = [], ignored_files = {})
  @repository = repository
  @skip_types = skip_types
  @ignored_files = ignored_files
  @schema_reports = []
  @total_entities = []
  @documented_entities = []
  @undocumented_entities = []

  process_repository
end

Instance Attribute Details

#documented_entitiesObject (readonly)

Returns the value of attribute documented_entities.



61
62
63
# File 'lib/expressir/coverage.rb', line 61

def documented_entities
  @documented_entities
end

#ignored_filesObject (readonly)

Returns the value of attribute ignored_files.



61
62
63
# File 'lib/expressir/coverage.rb', line 61

def ignored_files
  @ignored_files
end

#repositoryObject (readonly)

Returns the value of attribute repository.



61
62
63
# File 'lib/expressir/coverage.rb', line 61

def repository
  @repository
end

#schema_reportsObject (readonly)

Returns the value of attribute schema_reports.



61
62
63
# File 'lib/expressir/coverage.rb', line 61

def schema_reports
  @schema_reports
end

#total_entitiesObject (readonly)

Returns the value of attribute total_entities.



61
62
63
# File 'lib/expressir/coverage.rb', line 61

def total_entities
  @total_entities
end

#undocumented_entitiesObject (readonly)

Returns the value of attribute undocumented_entities.



61
62
63
# File 'lib/expressir/coverage.rb', line 61

def undocumented_entities
  @undocumented_entities
end

Class Method Details

.from_file(path, skip_types = [], ignored_files = {}) ⇒ Report

Create a report from a schema file

Parameters:

  • path (String)

    Path to the schema file

  • skip_types (Array<String>) (defaults to: [])

    Array of entity type names to skip from coverage

  • ignored_files (Hash) (defaults to: {})

    Hash mapping absolute file paths to their matched patterns

Returns:

  • (Report)

    The coverage report



94
95
96
97
# File 'lib/expressir/coverage.rb', line 94

def self.from_file(path, skip_types = [], ignored_files = {})
  repository = Expressir::Express::Parser.from_file(path)
  new(repository, skip_types, ignored_files)
end

.from_repository(repository, skip_types = [], ignored_files = {}) ⇒ Report

Create a report from a repository

Parameters:

  • repository (Expressir::Model::Repository)

    The repository to analyze

  • skip_types (Array<String>) (defaults to: [])

    Array of entity type names to skip from coverage

  • ignored_files (Hash) (defaults to: {})

    Hash mapping absolute file paths to their matched patterns

Returns:

  • (Report)

    The coverage report



85
86
87
# File 'lib/expressir/coverage.rb', line 85

def self.from_repository(repository, skip_types = [], ignored_files = {})
  new(repository, skip_types, ignored_files)
end

Instance Method Details

#coverage_percentageFloat

Calculate the overall coverage percentage

Returns:

  • (Float)

    The coverage percentage



101
102
103
104
105
# File 'lib/expressir/coverage.rb', line 101

def coverage_percentage
  return 100.0 if @total_entities.empty?

  (@documented_entities.size.to_f / @total_entities.size) * 100
end

#directory_reportsArray<Hash>

Get directory-level reports

Returns:

  • (Array<Hash>)

    Array of directory report hashes



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/expressir/coverage.rb', line 168

def directory_reports
  # Group by directory (absolute path)
  by_directory = file_reports.group_by { |r| r["directory"] }

  # Aggregate stats for each directory
  by_directory.map do |directory, reports|
    # Convert directory to relative path
    relative_directory = begin
      Pathname.new(directory).relative_path_from(Pathname.pwd).to_s
    rescue ArgumentError
      # If paths are on different drives or otherwise incompatible,
      # fall back to the absolute path
      directory
    end

    total = reports.sum { |r| r["total"] }
    documented = reports.sum { |r| r["documented"] }
    coverage = total.positive? ? (documented.to_f / total) * 100 : 100.0

    {
      "directory" => relative_directory,
      "total" => total,
      "documented" => documented,
      "undocumented" => total - documented,
      "coverage" => coverage,
      "files" => reports.size,
    }
  end
end

#file_reportsArray<Hash>

Get file-level reports

Returns:

  • (Array<Hash>)

    Array of file report hashes



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/expressir/coverage.rb', line 109

def file_reports
  @schema_reports.map do |report|
    absolute_path = report[:schema].file
    relative_path = begin
      Pathname.new(absolute_path).relative_path_from(Pathname.pwd).to_s
    rescue ArgumentError
      # If paths are on different drives or otherwise incompatible,
      # fall back to the absolute path
      absolute_path
    end

    file_report = {
      "file" => relative_path,
      "file_basename" => File.basename(absolute_path),
      "directory" => File.dirname(absolute_path),
      "total" => report[:total].size,
      "documented" => report[:documented].size,
      "undocumented" => report[:undocumented],
      "coverage" => report[:coverage],
      "ignored" => report[:ignored] || false,
    }

    # Add matched pattern for ignored files
    if report[:ignored] && report[:matched_pattern]
      file_report["matched_pattern"] = report[:matched_pattern]
    end

    file_report
  end
end

#ignored_file_reportsArray<Hash>

Get ignored file reports

Returns:

  • (Array<Hash>)

    Array of ignored file report hashes



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/expressir/coverage.rb', line 142

def ignored_file_reports
  @schema_reports.select { |report| report[:ignored] }.map do |report|
    absolute_path = report[:schema].file
    relative_path = begin
      Pathname.new(absolute_path).relative_path_from(Pathname.pwd).to_s
    rescue ArgumentError
      # If paths are on different drives or otherwise incompatible,
      # fall back to the absolute path
      absolute_path
    end

    {
      "file" => relative_path,
      "file_basename" => File.basename(absolute_path),
      "directory" => File.dirname(absolute_path),
      "total" => report[:total].size,
      "documented" => report[:documented].size,
      "undocumented" => report[:undocumented],
      "coverage" => report[:coverage],
      "matched_pattern" => report[:matched_pattern],
    }
  end
end

#to_hHash

Convert report to a hash representation

Returns:

  • (Hash)

    The report as a hash



200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/expressir/coverage.rb', line 200

def to_h
  {
    "overall" => {
      "total" => @total_entities.size,
      "documented" => @documented_entities.size,
      "undocumented" => @undocumented_entities.size,
      "coverage" => coverage_percentage,
    },
    "directories" => directory_reports,
    "files" => file_reports,
  }
end