Module: Relaton::Index::FileStorage

Extended by:
FileStorage
Included in:
FileStorage
Defined in:
lib/relaton/index/file_storage.rb

Overview

File storage module contains methods to read and write files

Instance Method Summary collapse

Instance Method Details

#ctime(file) ⇒ Time?

Return file creation time

Parameters:

  • file (String)

    file path

Returns:

  • (Time, nil)

    file creation time or nil if file does not exist



14
15
16
# File 'lib/relaton/index/file_storage.rb', line 14

def ctime(file)
  File.exist?(file) && File.ctime(file)
end

#read(file) ⇒ String?

Read file

Parameters:

  • file (String)

    file path

Returns:

  • (String, nil)

    file content or nil if file does not exist



25
26
27
28
29
# File 'lib/relaton/index/file_storage.rb', line 25

def read(file)
  return unless File.exist?(file)

  File.read file, encoding: "UTF-8"
end

#remove(file) ⇒ void

This method returns an undefined value.

Remove file

Parameters:

  • file (String)

    file path



52
53
54
55
56
# File 'lib/relaton/index/file_storage.rb', line 52

def remove(file)
  return unless File.exist? file

  File.delete file
end

#write(file, data) ⇒ void

This method returns an undefined value.

Write file

Parameters:

  • file (String)

    file path

  • data (String)

    content to write



39
40
41
42
43
# File 'lib/relaton/index/file_storage.rb', line 39

def write(file, data)
  dir = File.dirname file
  FileUtils.mkdir_p dir
  File.write file, data, encoding: "UTF-8"
end