Class: Typingpool::Filer
- Inherits:
-
Object
- Object
- Typingpool::Filer
- Includes:
- Comparable, Utility::Castable
- Defined in:
- lib/typingpool/filer.rb,
lib/typingpool/filer/csv.rb,
lib/typingpool/filer/dir.rb,
lib/typingpool/filer/audio.rb,
lib/typingpool/filer/files.rb,
lib/typingpool/filer/files/audio.rb
Overview
Convenience wrapper for basic file operations. Base class for wrappers for specialized file types (audio, CSV) and for file collections.
Defined Under Namespace
Classes: Audio, CSV, Dir, Files
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Fully-expanded path to file.
Instance Method Summary collapse
- #<=>(other) ⇒ Object
-
#as(sym) ⇒ Object
Cast this file into a new Filer subtype, e.g.
-
#dir ⇒ Object
Returns the parent dir of the underlying file as a Filer::Dir instance.
-
#initialize(path, encoding = 'UTF-8') ⇒ Filer
constructor
Constructor.
-
#mv!(to) ⇒ Object
Moves the underlying file AND updates the @path of the Filer instance.
-
#read ⇒ Object
Returns contents of file or nil if the file does not exist.
-
#to_s ⇒ Object
(also: #to_str)
Filer objects always stringify to their path.
-
#to_stream(mode = 'r') ⇒ Object
Returns the underlying file as an IO stream.
-
#write(data, mode = 'w') ⇒ Object
Write data to the file.
Methods included from Utility::Castable
Constructor Details
#initialize(path, encoding = 'UTF-8') ⇒ Filer
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'.
20 21 22 23 |
# File 'lib/typingpool/filer.rb', line 20 def initialize(path, encoding='UTF-8') @path = path @encoding = encoding end |
Instance Attribute Details
#path ⇒ Object (readonly)
Fully-expanded path to file
12 13 14 |
# File 'lib/typingpool/filer.rb', line 12 def path @path end |
Instance Method Details
#<=>(other) ⇒ Object
25 26 27 |
# File 'lib/typingpool/filer.rb', line 25 def <=>(other) path <=> other.path end |
#as(sym) ⇒ Object
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
78 79 80 81 |
# File 'lib/typingpool/filer.rb', line 78 def as(sym) #super calls into Utility::Castable mixin super(sym, @path) end |
#dir ⇒ Object
Returns the parent dir of the underlying file as a Filer::Dir instance.
68 69 70 |
# File 'lib/typingpool/filer.rb', line 68 def dir Filer::Dir.new(File.dirname(@path)) end |
#mv!(to) ⇒ Object
Moves the underlying file AND updates the @path of the Filer instance.
44 45 46 47 48 49 50 |
# 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 ⇒ Object
Returns contents of file or nil if the file does not exist.
30 31 32 33 34 |
# File 'lib/typingpool/filer.rb', line 30 def read if File.exists? @path IO.read(@path, :encoding => @encoding) end end |
#to_s ⇒ Object Also known as: to_str
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.
61 62 63 |
# File 'lib/typingpool/filer.rb', line 61 def to_s @path end |
#to_stream(mode = 'r') ⇒ Object
Returns the underlying file as an IO stream. Convenient for Project::Remote#put.
54 55 56 |
# File 'lib/typingpool/filer.rb', line 54 def to_stream(mode='r') File.new(@path, mode, :encoding => @encoding) end |
#write(data, mode = 'w') ⇒ Object
Write data to the file.
37 38 39 40 41 |
# File 'lib/typingpool/filer.rb', line 37 def write(data, mode='w') File.open(@path, mode, :encoding => @encoding) do |out| out << data end end |