Class: FolderStash::Folder

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

Overview

A Folder represents a directory in the filesystem.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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.expand_path path
  @basename = File.basename path
end

Instance Attribute Details

#basenameObject (readonly)

Basename for the folder.



7
8
9
# File 'lib/folder_stash/folder.rb', line 7

def basename
  @basename
end

#pathObject (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

#countObject

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

#createObject

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.

Returns:

  • (Boolean)


46
47
48
# File 'lib/folder_stash/folder.rb', line 46

def exist?
  File.exist? path
end