Class: FolderStash::FileUsher

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

Overview

FileUsher stores files in a directory tree with a defined maximum number of #items per directory.

It will create new subdirectories to store files in if a subdirectory has reached the maximum number of items.

Constant Summary collapse

CURRENT_STORE_PATH =
'.current_store_path'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dir, **opts) ⇒ FileUsher

Returns a new instance.

Arguments
  • dir (String) - path for the #directory.

Options
  • nesting_levels - the number of subdirectories below #directory in the path of files that are stored (default: 2).

  • folder_limit - the maximum number of items allowed per directory (default: 10000).

  • link_location - the directory where the #current_directory symlink is stored. When not specified, the symlink will be in #directory

Setting nesting_levels to nil will also set the folder_limit. Conversely, setting folder_limit to nil also set nesting_levels to nil.



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/folder_stash/file_usher.rb', line 36

def initialize(dir, **opts)
  raise Errors::NoDirectoryError, dir: dir unless File.directory? dir

  @options = { nesting_levels: 2, folder_limit: 10_000 }.update(opts)
  @directory = dir
  @current_directory = File.join @options.fetch(:link_location, directory),
                                 CURRENT_STORE_PATH

  @tree = init_existing || init_new(nesting_levels)
  link_target
end

Instance Attribute Details

#directoryObject (readonly)

The working directory where all subdirectories and files are stored.



13
14
15
# File 'lib/folder_stash/file_usher.rb', line 13

def directory
  @directory
end

#treeObject (readonly)

An instance of FolderTree.



16
17
18
# File 'lib/folder_stash/file_usher.rb', line 16

def tree
  @tree
end

Instance Method Details

#copy(file, new_basename = nil, pathtype: :tree) ⇒ Object

Copies file to linked path.

The optional new_basename argument is passed when the file is to be renamed.



52
53
54
55
56
57
# File 'lib/folder_stash/file_usher.rb', line 52

def copy(file, new_basename = nil, pathtype: :tree)
  filename = new_basename || File.basename(file)
  path = store_path(filename)
  File.open(path, 'wb') { |f| f.write(File.new(file).read) }
  file_path path, pathtype
end

#current_directoryObject

Returns the full path (String) to the current directory symlink.



60
61
62
# File 'lib/folder_stash/file_usher.rb', line 60

def current_directory
  File.expand_path @current_directory
end

#current_pathObject

Returns the full directory path for #current_folder



65
66
67
# File 'lib/folder_stash/file_usher.rb', line 65

def current_path
  current_folder.path
end

#folder_limitObject

The number of items allowed in any directory in a nested directory path.

Will be nil if #nesting_levels is nil.



72
73
74
75
76
# File 'lib/folder_stash/file_usher.rb', line 72

def folder_limit
  return unless @options.fetch :nesting_levels

  @options.fetch :folder_limit
end

#linked_pathObject

Returns the directory path the #current_directory symlink points to.



79
80
81
# File 'lib/folder_stash/file_usher.rb', line 79

def linked_path
  File.readlink current_directory
end

#move(file, new_basename = nil, pathtype: :tree) ⇒ Object

Moves file to the #linked_path.

The optional new_basename argument is passed when the file is to be renamed.



87
88
89
90
91
92
# File 'lib/folder_stash/file_usher.rb', line 87

def move(file, new_basename = nil, pathtype: :tree)
  filename = new_basename || File.basename(file)
  path = store_path(filename)
  FileUtils.mv File.expand_path(file), path
  file_path path, pathtype
end

#nesting_levelsObject

The number of nested subdirectories.



95
96
97
98
99
# File 'lib/folder_stash/file_usher.rb', line 95

def nesting_levels
  return unless @options.fetch :folder_limit

  tree&.path_length || @options.fetch(:nesting_levels)
end