Class: FolderStash::FileUsher
- Inherits:
-
Object
- Object
- FolderStash::FileUsher
- 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
-
#directory ⇒ Object
readonly
The working directory where all subdirectories and files are stored.
-
#tree ⇒ Object
readonly
An instance of FolderTree.
Instance Method Summary collapse
-
#copy(file, pathtype: :tree) ⇒ Object
Copies
fileto linked path. -
#current_directory ⇒ Object
Returns the full path (String) to the current directory symlink.
-
#current_path ⇒ Object
Returns the full directory path for #current_folder.
-
#folder_limit ⇒ Object
The number of items allowed in any directory in a nested directory path.
-
#initialize(dir, **opts) ⇒ FileUsher
constructor
Returns a new instance.
-
#linked_path ⇒ Object
Returns the directory path the #current_directory symlink points to.
-
#move(file, pathtype: :tree) ⇒ Object
Moves
fileto the #linked_path. -
#nesting_levels ⇒ Object
The number of nested subdirectories.
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 = { nesting_levels: 2, folder_limit: 10_000 }.update(opts) @directory = dir @current_directory = File.join .fetch(:link_location, directory), CURRENT_STORE_PATH @tree = init_existing || init_new(nesting_levels) link_target end |
Instance Attribute Details
#directory ⇒ Object (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 |
#tree ⇒ Object (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_directory ⇒ Object
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. @current_directory end |
#current_path ⇒ Object
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_limit ⇒ Object
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 .fetch :nesting_levels .fetch :folder_limit end |
#linked_path ⇒ Object
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.(file), store_path(file) file_path path, pathtype end |
#nesting_levels ⇒ Object
The number of nested subdirectories.
87 88 89 90 91 |
# File 'lib/folder_stash/file_usher.rb', line 87 def nesting_levels return unless .fetch :folder_limit tree&.path_length || .fetch(:nesting_levels) end |