class Typingpool::Filer::Dir
Convenience wrapper for basic directory operations and for casting files to specific filer types (CSV
, Audio
).
Attributes
Full expanded path to the dir
Public Class Methods
Constructor. Takes full expanded path to the dir and creates the dir in the filesystem. Returns new Filer::Dir
.
# File lib/typingpool/filer/dir.rb, line 21 def create(path) FileUtils.mkdir(path) new(path) end
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.
# File lib/typingpool/filer/dir.rb, line 29 def named(name, in_dir) path = File.join(in_dir, name) if File.exist?(path) && File.directory?(path) new(path) end end
Constructor. Takes full expanded path to the dir. Does NOT create dir in the filesystem.
# File lib/typingpool/filer/dir.rb, line 13 def initialize(path) @path = path end
Public Instance Methods
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.
# File lib/typingpool/filer/dir.rb, line 48 def file(*relative_path) Filer.new(file_path(*relative_path)) end
# File lib/typingpool/filer/dir.rb, line 70 def file_path(*relative_path) File.join(@path, *relative_path) end
Returns the files in the Filer::Dir
directory as Filer
instances. Excludes files whose names start with a dot.
# 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
OS X specific. Opens the dir in the Finder via the 'open' command.
# File lib/typingpool/filer/dir.rb, line 66 def finder_open system('open', @path) end
Takes relative path elements as params just like the file method. Returns a new Filer::Dir
instance wrapping the referenced subdir.
# File lib/typingpool/filer/dir.rb, line 61 def subdir(*relative_path) Dir.new(file_path(*relative_path)) end
Filer::Dir
isntances stringify to their path.
# File lib/typingpool/filer/dir.rb, line 38 def to_s @path end