Class: Rekki::Core

Inherits:
Object
  • Object
show all
Defined in:
lib/rekki/core/count_images.rb

Class Method Summary collapse

Class Method Details

.count_images_in_view_fileObject



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
# File 'lib/rekki/core/count_images.rb', line 39

def self.count_images_in_view_file
  # select_view_files_and_ruby_filesからの返り値を使用して、それぞれのファイルでgrepを行う。
  # select_image_filesの画像がヒットするかを判別する。
  # ヒットした画像のパスが何回登場したのかを配列に持たせる。
  # できれば、それをグラフ化したい。簡易的ならこんな感じ
  # kojo: ********
  # aoki: ****
  files_name = generate_files_name
  view_files_path = select_view_files_path(files_name)
  imags_file_name = select_image_files(files_name)
  result = {}

  imags_file_name.each do |image|
    image_count = 0
    view_files_path.each do |file_path|
      command = "rg \"#{image}\" #{file_path}"

      stdout, stderr, status = Open3.capture3(command)
      image_count += stdout.split("\n").count
    end
    result = { image => image_count }
  end

  puts 'IMAGE COUNT RESULT'
  result.each do |k, v|
    puts "#{k}: #{'*' * v}"
  end
end

.generate_files_nameObject



5
6
7
8
9
10
11
12
13
# File 'lib/rekki/core/count_images.rb', line 5

def self.generate_files_name
  files_path = []
  Dir.glob('./**/*') do |path|
    next if FileTest.directory?(path)	# ディレクトリは無視

    files_path << path
  end
  files_path
end

.select_image_files(files_path) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/rekki/core/count_images.rb', line 15

def self.select_image_files(files_path)
  return ['nothings'] if files_path.nil?

  select_image_files = files_path.select do |file|
    if file.include?('.webp') || file.include?('.jpeg') || file.include?('.jpg') || file.include?('.png') || file.include?('.svg')
      file.gsub!(%r{.[/0-9a-zA-Z_-]+/image/}, '')
    end
  end
  return 'Image files count is 0' if select_image_files.count.zero? || select_image_files.nil?

  select_image_files
end

.select_view_files_path(files_path) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/rekki/core/count_images.rb', line 28

def self.select_view_files_path(files_path)
  return [] if files_path.nil?

  view_files_path = files_path.select do |file_path|
    file_path.include?('.html.haml') || file_path.include?('.slim') || file_path.include?('.erb')
  end
  return 'View files and Ruby files count is 0' if view_files_path.count.zero? || view_files_path.nil?

  view_files_path
end