Class: Nanoc::Core::Store Abstract Private
- Inherits:
-
Object
- Object
- Nanoc::Core::Store
- Includes:
- ContractsSupport
- Defined in:
- lib/nanoc/core/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.
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
ActionSequenceStore, BinaryCompiledContentCache, ChecksumStore, CompiledContentCache, DependencyStore, OutdatednessStore, TextualCompiledContentCache
Instance Attribute Summary collapse
-
#filename ⇒ String
readonly
private
The name of the file where data will be loaded from and stored to.
-
#version ⇒ Numeric
readonly
private
The version number corresponding to the file format the data is in.
Loading and storing data collapse
-
#data ⇒ Object
abstract
private
The data that should be written to the disk.
- #data=(new_data) ⇒ void abstract private
-
#load ⇒ void
private
Loads the data from the filesystem into memory.
-
#store ⇒ void
private
Stores the data contained in memory to the filesystem.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(filename, version) ⇒ Store
constructor
private
Creates a new store for the given filename.
Methods included from ContractsSupport
enabled?, included, setup_once, warn_about_performance
Constructor Details
#initialize(filename, version) ⇒ 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.
Creates a new store for the given filename.
37 38 39 40 |
# File 'lib/nanoc/core/store.rb', line 37 def initialize(filename, version) @filename = filename @version = version end |
Instance Attribute Details
#filename ⇒ String (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.
22 23 24 |
# File 'lib/nanoc/core/store.rb', line 22 def filename @filename end |
#version ⇒ Numeric (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.
27 28 29 |
# File 'lib/nanoc/core/store.rb', line 27 def version @version end |
Class Method Details
.tmp_path_for(store_name:, config:) ⇒ Object
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.
45 46 47 48 49 50 |
# File 'lib/nanoc/core/store.rb', line 45 def self.tmp_path_for(store_name:, config:) File.absolute_path( File.join(tmp_path_prefix(config.output_dir), store_name), config.dir, ) end |
.tmp_path_prefix(output_dir) ⇒ Object
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.
53 54 55 56 |
# File 'lib/nanoc/core/store.rb', line 53 def self.tmp_path_prefix(output_dir) dir = Digest::SHA1.hexdigest(output_dir)[0..12] File.join('tmp', 'nanoc', dir) end |
Instance Method Details
#data ⇒ Object
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 must be implemented by the subclass.
Returns The data that should be written to the disk.
63 64 65 |
# File 'lib/nanoc/core/store.rb', line 63 def data raise NotImplementedError.new('Nanoc::Core::Store subclasses must implement #data and #data=') end |
#data=(new_data) ⇒ void
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 must be implemented by the subclass.
This method returns an undefined value.
72 73 74 |
# File 'lib/nanoc/core/store.rb', line 72 def data=(new_data) # rubocop:disable Lint/UnusedMethodArgument raise NotImplementedError.new('Nanoc::Core::Store subclasses must implement #data and #data=') end |
#load ⇒ void
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.
80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/nanoc/core/store.rb', line 80 def load return unless File.file?(filename) begin pstore.transaction do return if pstore[:version] != version self.data = pstore[:data] end rescue FileUtils.rm_f(filename) load end end |
#store ⇒ void
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.
99 100 101 102 103 104 105 106 |
# File 'lib/nanoc/core/store.rb', line 99 def store FileUtils.mkdir_p(File.dirname(filename)) pstore.transaction do pstore[:data] = data pstore[:version] = version end end |