Class: RouteGuard::Rules::Statistics
- Defined in:
- lib/route_guard/rules/statistics.rb
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
Methods inherited from Base
#expand_path, #initialize, #path_shadows?, #split_segments, #verb_matches?
Constructor Details
This class inherits a constructor from RouteGuard::Rules::Base
Instance Method Details
#analyze(routes, report) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/route_guard/rules/statistics.rb', line 8 def analyze(routes, report) return [] if routes.empty? # Calculate metrics total_routes = routes.length # REST Resources: count unique controller resource names or use RouteTracker if available resource_calls = RouteTracker.resource_calls rest_resources = if resource_calls.any? resource_calls.map { |c| c[:args].first }.uniq.size else # Fallback: estimate from controller count routes.map(&:controller).compact.uniq.size end # Namespaces: count of unique directories in controller names namespaces = routes.map(&:controller).compact .select { |c| c.include?("/") } .map { |c| c.split("/")[0...-1].join("/") } .uniq.size # Scopes: count of unique non-parameter, non-wildcard first-level path prefixes prefixes = routes.map { |r| r.path.split("/")[1] } .compact .reject { |p| p.start_with?(":") || p.start_with?("*") } .uniq scopes = prefixes.size # Wildcards: routes containing '*' wildcards = routes.select { |r| r.path.include?("*") }.size # Nesting depth: count of dynamic parameters (:id, etc.) in route path depths = routes.map { |r| r.path.scan(/:\w+/).size } max_depth = depths.max || 0 avg_depth = (depths.sum.to_f / total_routes).round(2) # Most common controller controller_counts = Hash.new(0) routes.each { |r| controller_counts[r.controller] += 1 if r.controller } most_common_controller = nil most_common_count = 0 if controller_counts.any? raw_controller, most_common_count = controller_counts.max_by { |_, count| count } most_common_controller = camelize_controller(raw_controller) end # Store in report stats (will be updated with issues later) report.stats = { total_routes: total_routes, rest_resources: rest_resources, namespaces: namespaces, scopes: scopes, wildcards: wildcards, duplicate_paths: 0, # Will be set by analyzer after running all rules shadowed_routes: 0, # Will be set by analyzer average_nesting_depth: avg_depth, maximum_nesting_depth: max_depth, most_common_controller: most_common_controller, most_common_count: most_common_count } [] # Statistics rule does not generate issues itself end |