Class: Picky::Backends::File::Basic

Inherits:
Object
  • Object
show all
Includes:
Helpers::File
Defined in:
lib/picky/backends/file/basic.rb

Overview

Base class for all file-based index files.

Provides necessary helper methods for its subclasses. Not directly useable, as it does not provide dump/load methods.

Direct Known Subclasses

JSON

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cache_path, options = {}) ⇒ Basic

An index cache takes a path, without file extension, which will be provided by the subclasses.



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/picky/backends/file/basic.rb', line 24

def initialize cache_path, options = {}
  @cache_path = "#{cache_path}.file.#{extension}"

  # This is the mapping file with the in-memory hash for the
  # file position/offset mappings.
  #
  @mapping_file = Memory::JSON.new "#{cache_path}.file_mapping"

  @empty   = options[:empty]
  @initial = options[:initial]
end

Instance Attribute Details

#cache_pathObject (readonly)

This index file’s location.



18
19
20
# File 'lib/picky/backends/file/basic.rb', line 18

def cache_path
  @cache_path
end

#mapping_fileObject (readonly)

This index file’s location.



18
19
20
# File 'lib/picky/backends/file/basic.rb', line 18

def mapping_file
  @mapping_file
end

Instance Method Details

#deleteObject

Deletes the file.



60
61
62
63
64
# File 'lib/picky/backends/file/basic.rb', line 60

def delete
  mapping_file.delete

  `rm -Rf #{cache_path}`
end

#emptyObject

The empty index that is used for putting the index together before it is saved into the files.



45
46
47
# File 'lib/picky/backends/file/basic.rb', line 45

def empty
  @empty && @empty.clone || {}
end

#extensionObject

The default extension for index files is “index”.



38
39
40
# File 'lib/picky/backends/file/basic.rb', line 38

def extension
  :index
end

#initialObject

The initial content before loading.

Note: We could also load the mapping file

as in #load.


54
55
56
# File 'lib/picky/backends/file/basic.rb', line 54

def initial
  @initial && @initial.clone || {}
end

#to_sObject



68
69
70
# File 'lib/picky/backends/file/basic.rb', line 68

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