Class: FolderStash::Folder
- Inherits:
-
Object
- Object
- FolderStash::Folder
- Defined in:
- lib/folder_stash/folder.rb
Overview
A Folder represents a directory in the filesystem.
Instance Attribute Summary collapse
-
#basename ⇒ Object
readonly
Basename for the folder.
-
#path ⇒ Object
readonly
Absolute path for the folder.
Class Method Summary collapse
Instance Method Summary collapse
-
#count ⇒ Object
Returns the number of visible files in the folder.
-
#create ⇒ Object
Creates the directory path in the immediate parent.
-
#create! ⇒ Object
Creates the directory #path with all parents.
-
#entries(include_hidden: false) ⇒ Object
Returns a list of entries (files or folders) in the folder.
-
#exist? ⇒ Boolean
Returns
trueif the directory #path exists. -
#initialize(path) ⇒ Folder
constructor
Returns a new instance.
Constructor Details
#initialize(path) ⇒ Folder
Returns a new instance.
Arguments
-
path(String) - path to the directory for the folder.
17 18 19 20 |
# File 'lib/folder_stash/folder.rb', line 17 def initialize(path) @path = File. path @basename = File.basename path end |
Instance Attribute Details
#basename ⇒ Object (readonly)
Basename for the folder.
7 8 9 |
# File 'lib/folder_stash/folder.rb', line 7 def basename @basename end |
#path ⇒ Object (readonly)
Absolute path for the folder.
10 11 12 |
# File 'lib/folder_stash/folder.rb', line 10 def path @path end |
Class Method Details
.folders_for_path_segment(root, segment) ⇒ Object
22 23 24 25 26 27 28 |
# File 'lib/folder_stash/folder.rb', line 22 def self.folders_for_path_segment(root, segment) root_folder = Folder.new root segment.inject([root_folder]) do |dirs, dir| path = File.join dirs.last.path, dir dirs << Folder.new(path) end end |
Instance Method Details
#count ⇒ Object
Returns the number of visible files in the folder.
31 32 33 |
# File 'lib/folder_stash/folder.rb', line 31 def count entries.count end |
#create ⇒ Object
Creates the directory path in the immediate parent.
36 37 38 |
# File 'lib/folder_stash/folder.rb', line 36 def create FileUtils.mkdir path unless exist? end |
#create! ⇒ Object
Creates the directory #path with all parents.
41 42 43 |
# File 'lib/folder_stash/folder.rb', line 41 def create! FileUtils.mkdir_p path unless exist? end |
#entries(include_hidden: false) ⇒ Object
Returns a list of entries (files or folders) in the folder.
Options
-
include_hidden-
true- list visible and hidden entries. -
false(default) - list only visible entries.
-
57 58 59 60 61 62 |
# File 'lib/folder_stash/folder.rb', line 57 def entries(include_hidden: false) children = Dir.children path return children if include_hidden == true children.reject { |entry| entry.start_with? '.' } end |
#exist? ⇒ Boolean
Returns true if the directory #path exists.
46 47 48 |
# File 'lib/folder_stash/folder.rb', line 46 def exist? File.exist? path end |