Sha256: 3b28410917d4c77caf32ef3fe0dbb355e4af061f0dd9d6ec20fb3ed5ab252d18

Contents?: true

Size: 893 Bytes

Versions: 4

Compression:

Stored size: 893 Bytes

Contents

# frozen_string_literal: true
require 'i18n/tasks/scanners/files/file_reader'
module I18n::Tasks::Scanners::Files
  # Reads the files in 'rb' mode and UTF-8 encoding.
  # Wraps a {FileReader} and caches the results.
  #
  # @note This class is thread-safe. All methods are cached.
  # @since 0.9.0
  class CachingFileReader < FileReader

    def initialize
      super
      @mutex = Mutex.new
      @cache = {}
    end

    # Return the contents of the file at the given path.
    # The file is read in the 'rb' mode and UTF-8 encoding.
    #
    # @param (see FileReader#read_file)
    # @return (see FileReader#read_file)
    # @note This method is cached, it will only access the filesystem on the first invocation.
    def read_file(path)
      absolute_path = File.expand_path(path)
      @cache[absolute_path] || @mutex.synchronize { @cache[absolute_path] ||= super }
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
i18n-tasks-0.9.6 lib/i18n/tasks/scanners/files/caching_file_reader.rb
i18n-tasks-0.9.5 lib/i18n/tasks/scanners/files/caching_file_reader.rb
i18n-tasks-0.9.4 lib/i18n/tasks/scanners/files/caching_file_reader.rb
i18n-tasks-0.9.3 lib/i18n/tasks/scanners/files/caching_file_reader.rb