Class: Typingpool::Filer::Dir

Inherits:
Files
  • Object
show all
Defined in:
lib/typingpool/filer/dir.rb

Overview

Convenience wrapper for basic directory operations and for casting files to specific filer types (CSV, Audio).

Direct Known Subclasses

Project::Local

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Files

#as, #each, #mv!, #to_streams

Methods included from Utility::Castable

#as, included

Constructor Details

#initialize(path) ⇒ Dir

Constructor. Takes full expanded path to the dir. Does NOT create dir in the filesystem.



13
14
15
# File 'lib/typingpool/filer/dir.rb', line 13

def initialize(path)
  @path = path
end

Instance Attribute Details

#pathObject (readonly)

Full expanded path to the dir



9
10
11
# File 'lib/typingpool/filer/dir.rb', line 9

def path
  @path
end

Class Method Details

.create(path) ⇒ Object

Constructor. Takes full expanded path to the dir and creates the dir in the filesystem. Returns new Filer::Dir.



21
22
23
24
# File 'lib/typingpool/filer/dir.rb', line 21

def create(path)
  FileUtils.mkdir(path)
  new(path)
end

.named(name, in_dir) ⇒ Object

Constructor. Takes directory name and full expanded path of the parent directory. If the so-named directory exists within the parent directory, returns it. If not, returns nil.



29
30
31
32
33
34
# File 'lib/typingpool/filer/dir.rb', line 29

def named(name, in_dir)
  path = File.join(in_dir, name)
  if File.exists?(path) && File.directory?(path)
    new(path)
  end
end

Instance Method Details

#file(*relative_path) ⇒ Object

Takes an aribtrary number of path elements relative to the Filer::Dir instance. So a file in the subdir path/to/file.txt would be referenced via file(‘path’, ‘to’, ‘file.txt’). Returns a new Filer instance wrapping the referenced file. Does not guarantee that the referenced file exists.



48
49
50
# File 'lib/typingpool/filer/dir.rb', line 48

def file(*relative_path)
  Filer.new(file_path(*relative_path))
end

#file_path(*relative_path) ⇒ Object



70
71
72
# File 'lib/typingpool/filer/dir.rb', line 70

def file_path(*relative_path)
  File.join(@path, *relative_path)
end

#filesObject

Returns the files in the Filer::Dir directory as Filer instances. Excludes files whose names start with a dot.



54
55
56
# File 'lib/typingpool/filer/dir.rb', line 54

def files
  ::Dir.entries(@path).select{|entry| File.file? file_path(entry) }.reject{|entry| entry.match(/^\./) }.map{|entry| self.file(entry) }
end

#finder_openObject

OS X specific. Opens the dir in the Finder via the ‘open’ command.



66
67
68
# File 'lib/typingpool/filer/dir.rb', line 66

def finder_open
  system('open', @path)
end

#subdir(*relative_path) ⇒ Object

Takes relative path elements as params just like the file method. Returns a new Filer::Dir instance wrapping the referenced subdir.



61
62
63
# File 'lib/typingpool/filer/dir.rb', line 61

def subdir(*relative_path)
  Dir.new(file_path(*relative_path))
end

#to_sObject Also known as: to_str

Filer::Dir isntances stringify to their path.



38
39
40
# File 'lib/typingpool/filer/dir.rb', line 38

def to_s
  @path
end