Module: IMW::Resources::LocalFile
- Defined in:
- lib/imw/resources/local.rb
Overview
Defines methods for appropriate for a local file.
Instance Method Summary collapse
-
#dump(data, options = {}) ⇒ Object
Dump
data
into this file. -
#io ⇒ File
Return the IO object at this path.
-
#load {|String| ... } ⇒ Array
Return the lines in this file.
-
#map {|String| ... } ⇒ Object
Map over the lines in this file.
-
#read(length = nil) ⇒ String
Read from this file.
-
#rm ⇒ Object
(also: #rm!)
Delete this resource.
-
#write(text) ⇒ Fixnum
Write to this file.
Instance Method Details
#dump(data, options = {}) ⇒ Object
Dump data
into this file.
120 121 122 123 124 125 |
# File 'lib/imw/resources/local.rb', line 120 def dump data, ={} data.each do |element| # works if data is an Array or a String io.puts(element.to_s) end io.close unless [:persist] end |
#io ⇒ File
Return the IO object at this path.
73 74 75 |
# File 'lib/imw/resources/local.rb', line 73 def io @io ||= open(path, mode) end |
#load {|String| ... } ⇒ Array
Return the lines in this file.
If passed a block, yield each line of the file to the block.
99 100 101 102 103 104 105 106 107 |
# File 'lib/imw/resources/local.rb', line 99 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.
112 113 114 |
# File 'lib/imw/resources/local.rb', line 112 def map &block io.map(&block) end |
#read(length = nil) ⇒ String
Read from this file.
81 82 83 |
# File 'lib/imw/resources/local.rb', line 81 def read length=nil io.read(length) end |
#rm ⇒ Object Also known as: rm!
Delete this resource.
63 64 65 66 67 |
# File 'lib/imw/resources/local.rb', line 63 def rm should_exist!("Cannot delete") FileUtils.rm path self end |
#write(text) ⇒ Fixnum
Write to this file
89 90 91 |
# File 'lib/imw/resources/local.rb', line 89 def write text io.write text end |