Class: Aeternitas::StorageAdapter::File

Inherits:
Aeternitas::StorageAdapter show all
Defined in:
lib/aeternitas/storage_adapter/file.rb

Overview

A storage adapter that stores the entries on disk.

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ File

Create a new File storage adapter.

Parameters:

  • config (Hash)

    the adapters config

Options Hash (config):

  • :directory (String)

    specifies where the entries are stored



8
9
10
# File 'lib/aeternitas/storage_adapter/file.rb', line 8

def initialize(config)
  super
end

Instance Method Details

#content_size(id) ⇒ Integer

Returns the raw_content’s size in bytes

Parameters:

  • id (String)

    the entries fingerprint

Returns:

  • (Integer)

    the entries size in byte



39
40
41
# File 'lib/aeternitas/storage_adapter/file.rb', line 39

def content_size(id)
  retrieve(id).bytesize
end

#delete(id) ⇒ Object



26
27
28
29
30
# File 'lib/aeternitas/storage_adapter/file.rb', line 26

def delete(id)
  !!::File.delete(file_path(id))
rescue Errno::ENOENT
  false
end

#exist?(id) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/aeternitas/storage_adapter/file.rb', line 32

def exist?(id)
  ::File.exist?(file_path(id))
end

#file_size_disk(id) ⇒ Integer

Returns the raw_content compressed size in bytes

Parameters:

  • id (String)

    the entries fingerprint

Returns:

  • (Integer)

    the entries size on disk in byte



46
47
48
# File 'lib/aeternitas/storage_adapter/file.rb', line 46

def file_size_disk(id)
  ::File.size(file_path(id))
end

#retrieve(id) ⇒ Object



21
22
23
24
# File 'lib/aeternitas/storage_adapter/file.rb', line 21

def retrieve(id)
  raise(Aeternitas::Errors::SourceDataNotFound, id) unless exist?(id)
  Zlib.inflate(::File.read(file_path(id), encoding: "ascii-8bit"))
end

#store(id, raw_content) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/aeternitas/storage_adapter/file.rb', line 12

def store(id, raw_content)
  path = file_path(id)
  ensure_folders_exist(path)
  raise(Aeternitas::Errors::SourceDataExists, id) if ::File.exist?(path)
  ::File.open(path, "w+", encoding: "ascii-8bit") do |f|
    f.write(Zlib.deflate(raw_content, Zlib::BEST_COMPRESSION))
  end
end