forked from ManageIQ/manageiq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeature_support_matrix.rb
executable file
·68 lines (57 loc) · 1.5 KB
/
feature_support_matrix.rb
1
2
3
4
5
6
7
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
require File.expand_path('../config/environment', __dir__)
require 'csv'
def usage
<<-USAGE
Usage:
`ruby tools/feature_support_matrix.rb`
USAGE
end
FeatureMatrix = Struct.new(:model, :features, :subclasses) do
def supports_nothing?
features.nil? || features.values.none?
end
def accept(visitor)
visitor.visit(self)
subclasses.each do |subclass|
subclass.accept(visitor)
end
end
end
def matrix_for(model)
matrix = FeatureMatrix.new
matrix.model = model
if model.included_modules.include?(SupportsFeatureMixin)
matrix.features = SupportsFeatureMixin::QUERYABLE_FEATURES.keys.each_with_object({}) do |feature, features|
features[feature] = model.supports?(feature)
end
end
matrix.subclasses = []
model.subclasses.each do |subclass|
matrix.subclasses << matrix_for(subclass)
end
matrix
end
class CsvVisitor
def initialize
@rows = []
end
def visit(subject)
unless subject.supports_nothing?
row = CSV::Row.new([], [])
row << {:model => subject.model.name}.merge(subject.features.transform_values { |v| v ? 'x' : nil })
@rows << row
end
end
def to_s
headers = @rows.first.headers
CSV.generate('', :headers => headers) do |csv|
header_row = CSV::Row.new(headers, %w(Model) + SupportsFeatureMixin::QUERYABLE_FEATURES.values)
csv << header_row
@rows.each { |row| csv << row }
end
end
end
matrix = matrix_for(ApplicationRecord)
csv = CsvVisitor.new
matrix.accept(csv)
puts csv.to_s