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



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

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



42
43
44
# File 'lib/aeternitas/storage_adapter/file.rb', line 42

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

#delete(id) ⇒ Object



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

def delete(id)
  begin
    !!::File.delete(file_path(id))
  rescue Errno::ENOENT => e
    return false
  end
end

#exist?(id) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/aeternitas/storage_adapter/file.rb', line 35

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



49
50
51
# File 'lib/aeternitas/storage_adapter/file.rb', line 49

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

#retrieve(id) ⇒ Object



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

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



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

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