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.



31
32
33
# File 'lib/file_distribution.rb', line 31

def get_path
  @path
end

#hex_path(id) ⇒ Object

Params:

  • id: database file ID etc.



37
38
39
40
41
42
# File 'lib/file_distribution.rb', line 37

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

#rename_from(path) ⇒ Object

Params:

  • path: source file path.

Raises a SystemCallError if the file cannot be renamed.



48
49
50
51
52
53
# File 'lib/file_distribution.rb', line 48

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

  File.rename(path,@path)
end

#set_extension(ext) ⇒ Object

Params:

  • ext: file extension.



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

def set_extension(ext)
  if ext.length > 0 && ext.chars.first != '.'
    @ext = '.' + ext
  else
    @ext = ext
  end
end