class Typingpool::Filer

Convenience wrapper for basic file operations. Base class for wrappers for specialized file types (audio, CSV) and for file collections.

Attributes

path[R]

Fully-expanded path to file

Public Class Methods

new(path, encoding='UTF-8') click to toggle source

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

<=>(other) click to toggle source
# File lib/typingpool/filer.rb, line 25
def <=>(other)
  path <=> other.path
end
as(sym) click to toggle source

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
Calls superclass method Typingpool::Utility::Castable#as
# File lib/typingpool/filer.rb, line 78
def as(sym)
  #super calls into Utility::Castable mixin
  super(sym, @path)
end
dir() click to toggle source

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
mv!(to) click to toggle source

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
read() click to toggle source

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
to_s() click to toggle source

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
Also aliased as: to_str
to_str()
Alias for: to_s
to_stream(mode='r') click to toggle source

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, mode='w') click to toggle source

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