Class: Nanoc::Store Abstract Private

Inherits:
Object
  • Object
show all
Defined in:
lib/nanoc/base/store.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

This class is abstract.

Subclasses must implement #data and #data=, and may implement #no_data_found and #version_mismatch_detected.

An abstract superclass for classes that need to store data to the filesystem, such as checksums, cached compiled content and dependency graphs.

Each store has a version number. When attempting to load data from a store that has an incompatible version number, no data will be loaded, but #version_mismatch_detected will be called.

Direct Known Subclasses

ChecksumStore, CompiledContentCache, DependencyTracker, RuleMemoryStore

Instance Attribute Summary (collapse)

Loading and storing data (collapse)

Callback methods (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Store) initialize(filename, version)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Creates a new store for the given filename.

Parameters:

  • filename (String)

    The name of the file where data will be loaded from and stored to.

  • version (Numeric)

    The version number corresponding to the file format the data is in. When the file format changes, the version number should be incremented.



34
35
36
37
# File 'lib/nanoc/base/store.rb', line 34

def initialize(filename, version)
  @filename = filename
  @version  = version
end

Instance Attribute Details

- (String) filename (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns The name of the file where data will be loaded from and stored to.

Returns:

  • (String)

    The name of the file where data will be loaded from and stored to.



19
20
21
# File 'lib/nanoc/base/store.rb', line 19

def filename
  @filename
end

- (Numeric) version (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns The version number corresponding to the file format the data is in. When the file format changes, the version number should be incremented.

Returns:

  • (Numeric)

    The version number corresponding to the file format the data is in. When the file format changes, the version number should be incremented.



24
25
26
# File 'lib/nanoc/base/store.rb', line 24

def version
  @version
end

Instance Method Details

- (Object) data

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method is abstract.

This method must be implemented by the subclass.

Returns The data that should be written to the disk

Returns:

  • The data that should be written to the disk

Raises:

  • (NotImplementedError)


44
45
46
# File 'lib/nanoc/base/store.rb', line 44

def data
  raise NotImplementedError.new('Nanoc::Store subclasses must implement #data and #data=')
end

- (void) data=(new_data)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method is abstract.

This method must be implemented by the subclass.

This method returns an undefined value.

Parameters:

  • new_data

    The data that has been loaded from the disk

Raises:

  • (NotImplementedError)


53
54
55
# File 'lib/nanoc/base/store.rb', line 53

def data=(new_data) # rubocop:disable Lint/UnusedMethodArgument
  raise NotImplementedError.new('Nanoc::Store subclasses must implement #data and #data=')
end

- (void) load

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Loads the data from the filesystem into memory. This method will set the loaded data using the #data= method.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/nanoc/base/store.rb', line 61

def load
  # Don’t load twice
  if @loaded
    return
  end

  # Check file existance
  unless File.file?(filename)
    no_data_found
    @loaded = true
    return
  end

  begin
    pstore.transaction do
      # Check version
      if pstore[:version] != version
        version_mismatch_detected
        @loaded = true
        return
      end

      # Load
      self.data = pstore[:data]
      @loaded = true
    end
  rescue
    FileUtils.rm_f(filename)
    load
  end
end

- (void) no_data_found

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Callback method that is called when no data file was found. By default, this implementation does nothing, but it should probably be overridden by the subclass.



119
120
# File 'lib/nanoc/base/store.rb', line 119

def no_data_found
end

- (void) store

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Stores the data contained in memory to the filesystem. This method will use the #data method to fetch the data that should be written.



103
104
105
106
107
108
109
110
# File 'lib/nanoc/base/store.rb', line 103

def store
  FileUtils.mkdir_p(File.dirname(filename))

  pstore.transaction do
    pstore[:data]    = data
    pstore[:version] = version
  end
end

- (Object) unload

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Undoes the effects of #load. Used when #load raises an exception.



96
97
# File 'lib/nanoc/base/store.rb', line 96

def unload
end

- (void) version_mismatch_detected

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Callback method that is called when a version mismatch is detected. By default, this implementation does nothing, but it should probably be overridden by the subclass.



127
128
# File 'lib/nanoc/base/store.rb', line 127

def version_mismatch_detected
end