Sha256: 9dffeff3fd0d108079429d75683cd97180d24ac6a6c1ed1798b24481faa7f624

Contents?: true

Size: 1.02 KB

Versions: 1

Compression:

Stored size: 1.02 KB

Contents

#
# @author: Adam Kubica (caffecoder) <caffecoder@kaizen-step.com>
#

#
# Class for manage hashed file distribution.
#
class FileDistribution

  # Creates new instance with directory prefix.
  #
  # Params:
  # - prefix: directory prefix.
  def initialize(prefix)
    @ext = '.dat'
    @prefix = File.expand_path(prefix)
    @path = @prefix
  end

  # Params:
  # - ext: file extension.
  def set_extension(ext)
    @ext = ext

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

  # Returns Destination path.
  def get_path
    @path
  end

  # Params:
  # - id: database file ID etc.
  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

  # Params:
  # - path: source file path.
  #
  # Raise a SystemCallError if the file cannot be renamed.
  def rename_from(path)
    dst_dir = File.dirname(@path)
    FileUtils.mkpath(dst_dir) unless File.exist?(dst_dir)

    File.rename(path, @path)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
file_distribution-0.2.0 lib/file_distribution.rb