Sha256: 4680052719f792943143280f935869c2adb84d57c75e6f9bd5f0a8a9cc3a58cf

Contents?: true

Size: 1.83 KB

Versions: 4

Compression:

Stored size: 1.83 KB

Contents

require 'i18n/tasks/scanners/scanner'

module I18n::Tasks::Scanners
  # A base class for a scanner that analyses files.
  #
  # @abstract The child must implement {#scan_file}.
  # @since 0.9.0
  class FileScanner < Scanner
    attr_reader :config

    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

    # @return (see Scanner#keys)
    def keys
      (traverse_files { |path|
        scan_file(path)
      }.reduce(:+) || []).group_by(&:first).map { |key, keys_occurrences|
        Results::KeyOccurrences.new(key: key, occurrences: keys_occurrences.map(&:second))
      }
    end

    protected

    # Extract all occurrences of translate calls from the file at the given path.
    #
    # @return [Array<[key, Results::KeyOccurrence]>] each occurrence found in the file
    def scan_file(path)
      fail 'Unimplemented'
    end

    # Read a file. Reads of the same path are cached.
    #
    # @param path [String]
    # @return [String] file contents
    def read_file(path)
      @file_reader.read_file(path)
    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(&block)
      @file_finder.traverse_files(&block)
    end

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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
i18n-tasks-0.9.2 lib/i18n/tasks/scanners/file_scanner.rb
i18n-tasks-0.9.1 lib/i18n/tasks/scanners/file_scanner.rb
i18n-tasks-0.9.0 lib/i18n/tasks/scanners/file_scanner.rb
i18n-tasks-0.9.0.rc2 lib/i18n/tasks/scanners/file_scanner.rb