Sha256: 672e557f825ad9dbf559968808dc3333d81c966a1125cd75ba0972fa782deab1

Contents?: true

Size: 1.65 KB

Versions: 1

Compression:

Stored size: 1.65 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, options = {}
          @cache_path = "#{cache_path}.memory.#{extension}"
          @empty      = options[:empty]
          @initial    = options[:initial]
        end

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

        # The empty index that is used for putting the index
        # together before it is dumped into the files.
        #
        def empty
          @empty && @empty.clone || empty_hash
        end
        
        def empty_hash
          # TODO Make this an explicit option.
          if defined? GoogleHashSparseRubyToRuby
            GoogleHashSparseRubyToRuby.new # TODO Use GoogleHashDenseIntToRuby where possible.
          else
            {}
          end
        end

        # The initial content before loading from file.
        #
        def initial
          @initial && @initial.clone || {}
        end

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

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

      end

    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
picky-4.26.2 lib/picky/backends/memory/basic.rb