Class: FileDistribution

Inherits:
Object
  • Object
show all
Defined in:
lib/file_distribution.rb

Overview

Class for manage hashed file distribution.

Instance Method Summary collapse

Constructor Details

#initialize(prefix) ⇒ FileDistribution

Creates new instance with directory prefix.

Params:

  • prefix: directory prefix.



14
15
16
17
18
# File 'lib/file_distribution.rb', line 14

def initialize(prefix)
  @ext = '.dat'
  @prefix = File.expand_path(prefix)
  @path = @prefix
end

Instance Method Details

#get_pathObject

Returns Destination path.



29
30
31
# File 'lib/file_distribution.rb', line 29

def get_path
  @path
end

#hex_path(id) ⇒ Object

Params:

  • id: database file ID etc.



35
36
37
38
39
40
# File 'lib/file_distribution.rb', line 35

def hex_path(id)
  hex = sprintf('%x', id)
  hex = sprintf('0%s', hex) if hex.length.odd?
  @path = File.join(@prefix, hex.scan(/../))
  @path += @ext
end

#rename_from(path) ⇒ Object

Params:

  • path: source file path.

Raise a SystemCallError if the file cannot be renamed.



46
47
48
49
50
51
# File 'lib/file_distribution.rb', line 46

def rename_from(path)
  dst_dir = File.dirname(@path)
  FileUtils.mkpath(dst_dir) unless File.exist?(dst_dir)

  File.rename(path, @path)
end

#set_extension(ext) ⇒ Object

Params:

  • ext: file extension.



22
23
24
25
26
# File 'lib/file_distribution.rb', line 22

def set_extension(ext)
  @ext = ext

  @ext = sprintf('.%s', ext) if !ext.empty? && ext.chars.first != '.'
end