Module: IMW::Schemes::Local::LocalFile
- Defined in:
- lib/imw/schemes/local.rb
Overview
Defines methods for appropriate for a local file.
Instance Method Summary collapse
-
#<<(text) ⇒ Object
Write the text with a trailing newline to this resource.
-
#close ⇒ Object
Close this resource’s file handle if it exists.
-
#emit(data, options = {}) ⇒ Object
Emit
data
into this file. -
#io ⇒ File
Return the IO object at this path.
-
#is_file? ⇒ true, false
Is this resource a regular file?.
-
#load {|String| ... } ⇒ Array
Return the lines in this file.
-
#map {|String| ... } ⇒ Object
Map over the lines in this file.
-
#num_chars ⇒ Integer
Return the number of characters in this file.
-
#num_lines ⇒ Integer
Return the number of lines in this file.
-
#num_words ⇒ Integer
Return the number of words in this file.
-
#read(length = nil) ⇒ String
Read from this file.
-
#readline ⇒ String
Read a line from this file.
-
#rm ⇒ Object
(also: #rm!)
Delete this resource.
-
#snippet ⇒ String
Return a snippet of text from this resource.
-
#summary ⇒ Object
Return a summary of properties of this local file.
-
#write(text) ⇒ Fixnum
Write to this file.
Instance Method Details
#<<(text) ⇒ Object
Write the text with a trailing newline to this resource.
130 131 132 |
# File 'lib/imw/schemes/local.rb', line 130 def << text io.write text.to_s + "\n" end |
#close ⇒ Object
Close this resource’s file handle if it exists.
97 98 99 100 101 102 |
# File 'lib/imw/schemes/local.rb', line 97 def close # explicitly check the @io instance variable b/c self.io # will open up a new handle by default io.close if @io super() end |
#emit(data, options = {}) ⇒ Object
Emit data
into this file.
160 161 162 163 164 |
# File 'lib/imw/schemes/local.rb', line 160 def emit data, ={} data.each do |element| # works if data is an Array or a String io << (element.to_s) end end |
#io ⇒ File
Return the IO object at this path.
92 93 94 |
# File 'lib/imw/schemes/local.rb', line 92 def io @io ||= open(path, mode) end |
#is_file? ⇒ true, false
Is this resource a regular file?
77 78 79 |
# File 'lib/imw/schemes/local.rb', line 77 def is_file? true end |
#load {|String| ... } ⇒ Array
Return the lines in this file.
If passed a block, yield each line of the file to the block.
140 141 142 143 144 145 146 147 148 |
# File 'lib/imw/schemes/local.rb', line 140 def load &block if block_given? io.each do |line| yield line end else read.split("\n") end end |
#map {|String| ... } ⇒ Object
Map over the lines in this file.
153 154 155 |
# File 'lib/imw/schemes/local.rb', line 153 def map &block io.map(&block) end |
#num_chars ⇒ Integer
Return the number of characters in this file.
199 200 201 |
# File 'lib/imw/schemes/local.rb', line 199 def num_chars wc[2] end |
#num_lines ⇒ Integer
Return the number of lines in this file.
185 186 187 |
# File 'lib/imw/schemes/local.rb', line 185 def num_lines wc[0] end |
#num_words ⇒ Integer
Return the number of words in this file.
192 193 194 |
# File 'lib/imw/schemes/local.rb', line 192 def num_words wc[1] end |
#read(length = nil) ⇒ String
Read from this file.
108 109 110 |
# File 'lib/imw/schemes/local.rb', line 108 def read length=nil io.read(length) end |
#readline ⇒ String
Read a line from this file.
115 116 117 |
# File 'lib/imw/schemes/local.rb', line 115 def readline io.readline end |
#rm ⇒ Object Also known as: rm!
Delete this resource.
82 83 84 85 86 |
# File 'lib/imw/schemes/local.rb', line 82 def rm should_exist!("Cannot delete") FileUtils.rm path self end |
#snippet ⇒ String
Return a snippet of text from this resource.
Will read the first 1024 bytes and strip non-ASCII characters from them. For more control, redefine this method in another module.
173 174 175 176 177 178 179 180 |
# File 'lib/imw/schemes/local.rb', line 173 def snippet returning([]) do |snip| io.read(1024).bytes.each do |byte| # CR LF SPACE ~ snip << byte.chr if byte == 13 || byte == 10 || byte >= 32 && byte <= 126 end end.join end |
#summary ⇒ Object
Return a summary of properties of this local file.
Returned properties include
-
basename
-
size
-
extension
-
snippet
210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/imw/schemes/local.rb', line 210 def summary data = { :basename => basename, :size => size, :extension => extension, :num_lines => num_lines } data[:snippet] = snippet if respond_to?(:snippet) data[:schema] = schema if respond_to?(:schema) data end |
#write(text) ⇒ Fixnum
Write to this file
123 124 125 |
# File 'lib/imw/schemes/local.rb', line 123 def write text io.write text end |