Class: FileDistribution
- Inherits:
-
Object
- Object
- FileDistribution
- Defined in:
- lib/file_distribution.rb
Overview
Class for manage hashed file distribution.
Instance Method Summary collapse
-
#get_path ⇒ Object
Returns Destination path.
-
#hex_path(id) ⇒ Object
Params: - id: database file ID etc.
-
#initialize(prefix) ⇒ FileDistribution
constructor
Creates new instance with directory prefix.
-
#rename_from(path) ⇒ Object
Params: - path: source file path.
-
#set_extension(ext) ⇒ Object
Params: - ext: file extension.
Constructor Details
#initialize(prefix) ⇒ FileDistribution
Creates new instance with directory prefix.
Params:
-
prefix: directory prefix.
13 14 15 16 17 |
# File 'lib/file_distribution.rb', line 13 def initialize(prefix) @ext = '.dat' @prefix = File.(prefix) @path = @prefix end |
Instance Method Details
#get_path ⇒ Object
Returns Destination path.
28 29 30 |
# File 'lib/file_distribution.rb', line 28 def get_path @path end |
#hex_path(id) ⇒ Object
Params:
-
id: database file ID etc.
34 35 36 37 38 39 |
# File 'lib/file_distribution.rb', line 34 def hex_path(id) hex = format('%x', id) hex = format('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.
45 46 47 48 49 50 |
# File 'lib/file_distribution.rb', line 45 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.
21 22 23 24 25 |
# File 'lib/file_distribution.rb', line 21 def set_extension(ext) @ext = ext @ext = format('.%s', ext) if !ext.empty? && ext.chars.first != '.' end |