Class: Antoinette::ElmAppUsageAnalyzer

Inherits:
Object
  • Object
show all
Defined in:
lib/antoinette/services/elm_app_usage_analyzer.rb

Defined Under Namespace

Classes: ElmApp, Matrix, ViewFile

Instance Method Summary collapse

Constructor Details

#initialize(layout_dirs: ["app/views/layouts"], custom_view_paths: []) ⇒ ElmAppUsageAnalyzer

Returns a new instance of ElmAppUsageAnalyzer.



11
12
13
14
# File 'lib/antoinette/services/elm_app_usage_analyzer.rb', line 11

def initialize(layout_dirs: ["app/views/layouts"], custom_view_paths: [])
  @layout_dirs = layout_dirs
  @custom_view_paths = custom_view_paths
end

Instance Method Details

#all_app_names ⇒ Object



59
60
61
# File 'lib/antoinette/services/elm_app_usage_analyzer.rb', line 59

def all_app_names
  @all_app_names ||= views.flat_map(&:elm_apps).uniq.sort
end

#elm_apps(content) ⇒ Object



45
46
47
48
# File 'lib/antoinette/services/elm_app_usage_analyzer.rb', line 45

def elm_apps(content)
  app_names = content.scan(/Elm\.(\w+)\.init/).flatten.uniq
  app_names.map { |name| ElmApp.new(name) }
end

#generate_csv ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/antoinette/services/elm_app_usage_analyzer.rb', line 78

def generate_csv
  CSV.generate do |csv|
    csv << ["View File"] + all_app_names

    views.each do |view_file|
      row = [view_file.path]
      all_app_names.each do |app_name|
        row << (view_file.elm_apps.include?(app_name) ? "X" : "")
      end
      csv << row
    end
  end
end

#mappings ⇒ Object



70
71
72
73
74
75
76
# File 'lib/antoinette/services/elm_app_usage_analyzer.rb', line 70

def mappings
  @mappings ||= views.group_by { |vf| vf.elm_apps.sort }
    .sort_by { |apps, _| -apps.count }
    .each_with_object({}) do |(apps, view_files), result|
      result[apps] = view_files.map(&:path).sort
    end
end

#matrix ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/antoinette/services/elm_app_usage_analyzer.rb', line 50

def matrix
  @matrix ||= Matrix.new.tap do |m|
    all_app_names.each do |app_name|
      m[app_name] = views.select { |v| v.elm_apps.include?(app_name) }
        .map(&:path)
    end
  end
end

#per_file ⇒ Object



63
64
65
66
67
68
# File 'lib/antoinette/services/elm_app_usage_analyzer.rb', line 63

def per_file
  @per_file ||= views.sort_by { |vf| -vf.elm_apps.count }
    .each_with_object({}) do |view_file, result|
      result[view_file.path] = view_file.elm_apps
  end
end

#view_path_globs ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/antoinette/services/elm_app_usage_analyzer.rb', line 32

def view_path_globs
  globs = [Rails.root.join("app", "views", "**", "*.html.erb")]
  @custom_view_paths.each do |custom_path|
    full_path = Rails.root.join(custom_path)
    globs << if File.file?(full_path)
      full_path
    else
      full_path.join("**", "*.html.erb")
    end
  end
  globs
end

#views ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/antoinette/services/elm_app_usage_analyzer.rb', line 16

def views
  @views ||= view_path_globs.each_with_object([]) do |glob_pattern, result|
    Dir.glob(glob_pattern).each do |file_path|
      content = File.read(file_path)
      apps = elm_apps(content)
      next if apps.empty?

      relative_path = file_path.sub("#{Rails.root}/", "")
      next if in_layout_dir?(relative_path)

      app_names = apps.map(&:name)
      result << ViewFile.new(relative_path, app_names)
    end
  end
end