class Typingpool::Filer
Convenience wrapper for basic file operations. Base class for wrappers for specialized file types (audio, CSV
) and for file collections.
Attributes
Fully-expanded path to file
Public Class Methods
Constructor.
==== Params
- path
-
Fully expanded path to file.
- encoding
-
Optional. Encoding for all text operations on the file. Should be compatiable with :encoding arg to IO.read. Default is 'UTF-8'.
# File lib/typingpool/filer.rb, line 20 def initialize(path, encoding='UTF-8') @path = path @encoding = encoding end
Public Instance Methods
# File lib/typingpool/filer.rb, line 25 def <=>(other) path <=> other.path end
Cast this file into a new Filer
subtype, e.g. Filer::Audio
.
==== Params [sym] Symbol corresponding to Filer subclass to cast into. For example, passing :audio will cast into a Filer::Audio. ==== Returns Instance of new Filer subclass
Typingpool::Utility::Castable#as
# File lib/typingpool/filer.rb, line 78 def as(sym) #super calls into Utility::Castable mixin super(sym, @path) end
Returns the parent dir of the underlying file as a Filer::Dir
instance.
# File lib/typingpool/filer.rb, line 68 def dir Filer::Dir.new(File.dirname(@path)) end
Moves the underlying file AND updates the @path of the Filer
instance.
# File lib/typingpool/filer.rb, line 44 def mv!(to) FileUtils.mv(@path, to) if File.directory? to to = File.join(to, File.basename(path)) end @path = to end
Returns contents of file or nil if the file does not exist.
# File lib/typingpool/filer.rb, line 30 def read if File.exist? @path IO.read(@path, :encoding => @encoding) end end
Filer
objects always stringify to their path. We might change this later such that to_str
gives the path but to_s
gives the content of the file as text.
# File lib/typingpool/filer.rb, line 61 def to_s @path end
Returns the underlying file as an IO stream. Convenient for Project::Remote#put.
# File lib/typingpool/filer.rb, line 54 def to_stream(mode='r') File.new(@path, mode, :encoding => @encoding) end
Write data to the file.
# File lib/typingpool/filer.rb, line 37 def write(data, mode='w') File.open(@path, mode, :encoding => @encoding) do |out| out << data end end