-
Notifications
You must be signed in to change notification settings - Fork 264
/
caching_file_finder.rb
34 lines (31 loc) · 1.08 KB
/
caching_file_finder.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
# frozen_string_literal: true
require 'i18n/tasks/scanners/files/file_finder'
module I18n::Tasks::Scanners::Files
# Finds the files in the specified search paths with support for exclusion / inclusion patterns.
# Wraps a {FileFinder} and caches the results.
#
# @note This class is thread-safe. All methods are cached.
# @since 0.9.0
class CachingFileFinder < FileFinder
# @param (see FileFinder#initialize)
def initialize(**args)
super
@mutex = Mutex.new
@cached_paths = nil
end
# Traverse the paths and yield the matching ones.
#
# @note This method is cached, it will only access the filesystem on the first invocation.
# @param (see FileFinder#traverse_files)
# @yieldparam (see FileFinder#traverse_files)
# @return (see FileFinder#traverse_files)
def traverse_files
super
end
# @note This method is cached, it will only access the filesystem on the first invocation.
# @return (see FileFinder#find_files)
def find_files
@cached_paths || @mutex.synchronize { @cached_paths ||= super }
end
end
end