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, pathtype: :tree) ⇒ Object

Copies file to linked path.



49
50
51
52
53
# File 'lib/folder_stash/file_usher.rb', line 49

def copy(file, pathtype: :tree)
  path = store_path(file)
  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.



56
57
58
# File 'lib/folder_stash/file_usher.rb', line 56

def current_directory
  File.expand_path @current_directory
end

#current_pathObject

Returns the full directory path for #current_folder



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

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.



68
69
70
71
72
# File 'lib/folder_stash/file_usher.rb', line 68

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.



75
76
77
# File 'lib/folder_stash/file_usher.rb', line 75

def linked_path
  File.readlink current_directory
end

#move(file, pathtype: :tree) ⇒ Object

Moves file to the #linked_path.



80
81
82
83
84
# File 'lib/folder_stash/file_usher.rb', line 80

def move(file, pathtype: :tree)
  path = store_path(file)
  FileUtils.mv File.expand_path(file), store_path(file)
  file_path path, pathtype
end

#nesting_levelsObject

The number of nested subdirectories.



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

def nesting_levels
  return unless @options.fetch :folder_limit

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