Module: IMW::Schemes::Local::LocalFile
- Includes:
- Metadata::HasMetadata
- 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
datainto this file. -
#external_summary ⇒ Object
Return a summary of properties of this local 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.
-
#write(text) ⇒ Fixnum
Write to this file.
Methods included from Metadata::HasMetadata
#description, #description=, #fields, #fields=, #metadata, #schema
Instance Method Details
#<<(text) ⇒ Object
Write the text with a trailing newline to this resource.
132 133 134 |
# File 'lib/imw/schemes/local.rb', line 132 def << text io.write text.to_s + "\n" end |
#close ⇒ Object
Close this resource’s file handle if it exists.
99 100 101 102 103 104 |
# File 'lib/imw/schemes/local.rb', line 99 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.
162 163 164 165 166 |
# File 'lib/imw/schemes/local.rb', line 162 def emit data, ={} data.each do |element| # works if data is an Array or a String io << (element.to_s) end end |
#external_summary ⇒ Object
Return a summary of properties of this local file.
Returned properties include
-
basename
-
size
-
extension
-
num_lines
212 213 214 215 216 217 |
# File 'lib/imw/schemes/local.rb', line 212 def external_summary super().merge({ :size => size, :num_lines => num_lines }) end |
#io ⇒ File
Return the IO object at this path.
94 95 96 |
# File 'lib/imw/schemes/local.rb', line 94 def io @io ||= open(path, mode) end |
#is_file? ⇒ true, false
Is this resource a regular file?
79 80 81 |
# File 'lib/imw/schemes/local.rb', line 79 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.
142 143 144 145 146 147 148 149 150 |
# File 'lib/imw/schemes/local.rb', line 142 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.
155 156 157 |
# File 'lib/imw/schemes/local.rb', line 155 def map &block io.map(&block) end |
#num_chars ⇒ Integer
Return the number of characters in this file.
201 202 203 |
# File 'lib/imw/schemes/local.rb', line 201 def num_chars wc[2] end |
#num_lines ⇒ Integer
Return the number of lines in this file.
187 188 189 |
# File 'lib/imw/schemes/local.rb', line 187 def num_lines wc[0] end |
#num_words ⇒ Integer
Return the number of words in this file.
194 195 196 |
# File 'lib/imw/schemes/local.rb', line 194 def num_words wc[1] end |
#read(length = nil) ⇒ String
Read from this file.
110 111 112 |
# File 'lib/imw/schemes/local.rb', line 110 def read length=nil io.read(length) end |
#readline ⇒ String
Read a line from this file.
117 118 119 |
# File 'lib/imw/schemes/local.rb', line 117 def readline io.readline end |
#rm ⇒ Object Also known as: rm!
Delete this resource.
84 85 86 87 88 |
# File 'lib/imw/schemes/local.rb', line 84 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.
175 176 177 178 179 180 181 182 |
# File 'lib/imw/schemes/local.rb', line 175 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 |
#write(text) ⇒ Fixnum
Write to this file
125 126 127 |
# File 'lib/imw/schemes/local.rb', line 125 def write text io.write text end |