Class: I18nContextGenerator::PathPolicy
- Inherits:
-
Object
- Object
- I18nContextGenerator::PathPolicy
- Defined in:
- lib/i18n_context_generator/path_policy.rb
Overview
Compiles client ignore globs once and applies them consistently while traversing configured source roots.
Class Method Summary collapse
Instance Method Summary collapse
- #each_file(path) ⇒ Object
- #files(paths, &predicate) ⇒ Object
- #ignored?(path, directory: false) ⇒ Boolean
-
#initialize(ignore_patterns:, roots: []) ⇒ PathPolicy
constructor
A new instance of PathPolicy.
Constructor Details
#initialize(ignore_patterns:, roots: []) ⇒ PathPolicy
Returns a new instance of PathPolicy.
10 11 12 13 |
# File 'lib/i18n_context_generator/path_policy.rb', line 10 def initialize(ignore_patterns:, roots: []) @ignore_patterns = self.class.compile_globs(ignore_patterns) @roots = roots.map { |root| normalize(root) }.uniq end |
Class Method Details
.compile_globs(patterns) ⇒ Object
58 59 60 |
# File 'lib/i18n_context_generator/path_policy.rb', line 58 def compile_globs(patterns) Array(patterns).map { |pattern| glob_to_regex(pattern) }.freeze end |
.glob_to_regex(glob_pattern) ⇒ Object
62 63 64 65 66 67 68 69 |
# File 'lib/i18n_context_generator/path_policy.rb', line 62 def glob_to_regex(glob_pattern) regex = Regexp.escape(normalize(glob_pattern)) .gsub('\*\*/', '(.*/)?') .gsub('\*\*', '.*') .gsub('\*', '[^/]*') .gsub('\?', '.') Regexp.new("(?:^|/)#{regex}(?:$|/)") end |
Instance Method Details
#each_file(path) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/i18n_context_generator/path_policy.rb', line 22 def each_file(path) return enum_for(__method__, path) unless block_given? if File.file?(path) yield path unless ignored?(path) return end return unless File.directory?(path) return if ignored?(path, directory: true) Find.find(path) do |candidate| if File.directory?(candidate) Find.prune if candidate != path && ignored?(candidate, directory: true) next end yield candidate unless ignored?(candidate) end end |
#files(paths, &predicate) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/i18n_context_generator/path_policy.rb', line 42 def files(paths, &predicate) seen = {} paths.each_with_object([]) do |path, files| each_file(path) do |file| next if predicate && !predicate.call(file) identity = File.(file) next if seen[identity] seen[identity] = true files << file end end end |
#ignored?(path, directory: false) ⇒ Boolean
15 16 17 18 19 20 |
# File 'lib/i18n_context_generator/path_policy.rb', line 15 def ignored?(path, directory: false) candidates = path_candidates(path, directory: directory) @ignore_patterns.any? do |pattern| candidates.any? { |candidate| pattern.match?(candidate) } end end |