Class: RoutesCoverage::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/routes_coverage/result.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(all_routes, hit_routes, settings) ⇒ Result

Returns a new instance of Result.



84
85
86
87
88
# File 'lib/routes_coverage/result.rb', line 84

def initialize(all_routes, hit_routes, settings)
  @all_routes = all_routes
  @route_hit_counts = hit_routes
  @settings = settings
end

Instance Attribute Details

#all_routesObject (readonly)

Returns the value of attribute all_routes.



90
91
92
# File 'lib/routes_coverage/result.rb', line 90

def all_routes
  @all_routes
end

#route_hit_countsObject (readonly)

Returns the value of attribute route_hit_counts.



90
91
92
# File 'lib/routes_coverage/result.rb', line 90

def route_hit_counts
  @route_hit_counts
end

Instance Method Details

#all_routes_with_hitsObject



153
154
155
156
157
158
159
160
161
162
163
# File 'lib/routes_coverage/result.rb', line 153

def all_routes_with_hits
  res = if Inspector::NEW_RAILS
          Inspector.new(all_routes).collect_all_routes
        else
          Inspector.new.collect_all_routes(all_routes)
        end
  res.each do |val|
    val[:hits] = @route_hit_counts[val[:original]] || 0
  end
  res
end

#avg_hitsObject



145
146
147
# File 'lib/routes_coverage/result.rb', line 145

def avg_hits
  (@route_hit_counts.values.sum.to_f / hit_routes_count).round(@settings.round_precision)
end

#coverageObject



139
140
141
142
143
# File 'lib/routes_coverage/result.rb', line 139

def coverage
  return 0 unless expected_routes.any?

  (hit_routes_count * 100.0 / expected_routes_count).round(@settings.round_precision)
end

#coverage_pass?Boolean

Returns:

  • (Boolean)


149
150
151
# File 'lib/routes_coverage/result.rb', line 149

def coverage_pass?
  !@settings.minimum_coverage || (coverage.to_f >= @settings.minimum_coverage)
end

#excluded_routesObject



113
114
115
116
# File 'lib/routes_coverage/result.rb', line 113

def excluded_routes
  expected_routes
  @excluded_routes
end

#excluded_routes_countObject



131
132
133
# File 'lib/routes_coverage/result.rb', line 131

def excluded_routes_count
  excluded_routes.size
end

#expected_routesObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/routes_coverage/result.rb', line 92

def expected_routes
  @expected_routes ||= begin
    filter_regex = Regexp.union(@settings.exclude_patterns)
    namespaces_regex = Regexp.union(@settings.exclude_namespaces.map { |n| %r{^/#{n}} })

    routes_groups = all_routes.group_by do |r|
      (
        ("#{r.verb.to_s[8..-3]} #{r.path.spec}".strip =~ filter_regex) ||
        (r.path.spec.to_s =~ namespaces_regex)
      ).present?
    end

    @excluded_routes = routes_groups[true] || []
    routes_groups[false] || []
  end
end

#expected_routes_countObject



127
128
129
# File 'lib/routes_coverage/result.rb', line 127

def expected_routes_count
  expected_routes.size
end

#hit_routesObject



118
119
120
121
# File 'lib/routes_coverage/result.rb', line 118

def hit_routes
  # TODO: sort?
  @route_hit_counts.keys
end

#hit_routes_countObject



123
124
125
# File 'lib/routes_coverage/result.rb', line 123

def hit_routes_count
  @route_hit_counts.size
end

#pending_routesObject



109
110
111
# File 'lib/routes_coverage/result.rb', line 109

def pending_routes
  expected_routes - hit_routes
end

#total_countObject



135
136
137
# File 'lib/routes_coverage/result.rb', line 135

def total_count
  all_routes.size
end