class I18n::Processes::Scanners::FileScanner

A base class for a scanner that analyses files.

@abstract The child must implement {#scan_file}. @since 0.9.0

Attributes

config[R]

Public Class Methods

new( config: {}, file_finder_provider: Files::CachingFileFinderProvider.new, file_reader: Files::CachingFileReader.new ) click to toggle source
# File lib/i18n/processes/scanners/file_scanner.rb, line 13
def initialize(
    config: {},
    file_finder_provider: Files::CachingFileFinderProvider.new,
    file_reader: Files::CachingFileReader.new
)
  @config      = config
  @file_reader = file_reader
  @file_finder = file_finder_provider.get(**config.slice(:paths, :only, :exclude))
end

Public Instance Methods

keys() click to toggle source

@return (see Scanner#keys)

# File lib/i18n/processes/scanners/file_scanner.rb, line 24
def keys
  (traverse_files do |path|
    scan_file(path)
  end.reduce(:+) || []).group_by(&:first).map do |key, keys_occurrences|
    Results::KeyOccurrences.new(key: key, occurrences: keys_occurrences.map(&:second))
  end
end

Protected Instance Methods

find_files() click to toggle source

@note This method is cached, it will only access the filesystem on the first invocation. @return (see FileFinder#find_files)

# File lib/i18n/processes/scanners/file_scanner.rb, line 61
def find_files
  @file_finder.find_files
end
read_file(path) click to toggle source

Read a file. Reads of the same path are cached.

@param path [String] @return [String] file contents

# File lib/i18n/processes/scanners/file_scanner.rb, line 45
def read_file(path)
  @file_reader.read_file(path)
end
scan_file(_path) click to toggle source

Extract all occurrences of translate calls from the file at the given path.

@return [Array<[key, Results::KeyOccurrence]>] each occurrence found in the file

# File lib/i18n/processes/scanners/file_scanner.rb, line 37
def scan_file(_path)
  fail 'Unimplemented'
end
traverse_files(&block) click to toggle source

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)

# File lib/i18n/processes/scanners/file_scanner.rb, line 55
def traverse_files(&block)
  @file_finder.traverse_files(&block)
end