Sha256: eb32238d4b3e16eb88fba56c3dcecdcf9055e5996667d79810c47f7cb106bc57

Contents?: true

Size: 1.85 KB

Versions: 9

Compression:

Stored size: 1.85 KB

Contents

module Picky

  module Backends

    class Memory

      # Base class for all memory-based index files.
      #
      # Provides necessary helper methods for its
      # subclasses.
      # Not directly useable, as it does not provide
      # dump/load methods.
      #
      class Basic

        include Helpers::File

        # This file's location.
        #
        attr_reader :cache_path

        # An index cache takes a path, without file extension,
        # which will be provided by the subclasses.
        #
        def initialize cache_path
          @cache_path = "#{cache_path}.memory.#{extension}"
        end

        # The default extension for index files is "index".
        #
        def extension
          :index
        end

        # Will copy the index file to a location that
        # is in a directory named "backup" right under
        # the directory the index file is in.
        #
        def backup
          prepare_backup backup_directory
          FileUtils.cp cache_path, target, verbose: true
        end

        # Copies the file from its backup location back
        # to the original location.
        #
        def restore
          FileUtils.cp backup_file_path_of(cache_path), cache_path, verbose: true
        end

        # Deletes the file.
        #
        def delete
          `rm -Rf #{cache_path}`
        end

        # Checks.
        #

        # Is this cache file suspiciously small?
        # (less than 8 Bytes of size)
        #
        def cache_small?
          size_of(cache_path) < 8
        end

        # Is the cache ok? (existing and larger than
        # zero Bytes in size)
        #
        # A small cache is still ok.
        #
        def cache_ok?
          size_of(cache_path) > 0
        end

        #
        #
        def to_s
          "#{self.class}(#{cache_path})"
        end

      end

    end

  end

end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
picky-3.1.11 lib/picky/backends/memory/basic.rb
picky-3.1.10 lib/picky/backends/memory/basic.rb
picky-3.1.9 lib/picky/backends/memory/basic.rb
picky-3.1.8 lib/picky/backends/memory/basic.rb
picky-3.1.7 lib/picky/backends/memory/basic.rb
picky-3.1.6 lib/picky/backends/memory/basic.rb
picky-3.1.5 lib/picky/backends/memory/basic.rb
picky-3.1.4 lib/picky/backends/memory/basic.rb
picky-3.1.3 lib/picky/backends/memory/basic.rb