Class: Nodule::Tempfile
Direct Known Subclasses
Instance Attribute Summary collapse
-
#file ⇒ Object
readonly
Returns the value of attribute file.
Attributes inherited from Base
#prefix, #read_count, #readers, #running, #topology
Instance Method Summary collapse
-
#initialize(opts = {}) ⇒ Tempfile
constructor
A new instance of Tempfile.
- #stop ⇒ Object
- #to_s ⇒ Object
- #touch(target = nil) ⇒ Object
Methods inherited from Base
#add_reader, #add_readers, #clear!, #done?, #join_topology!, #output, #output!, #output?, #read_until, #require_read_count, #run, #run_readers, #stop!, #verbose, #wait, #wait_with_backoff
Constructor Details
#initialize(opts = {}) ⇒ Tempfile
Returns a new instance of Tempfile.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/nodule/tempfile.rb', line 8 def initialize(opts={}) suffix = opts[:suffix] || '' prefix = opts[:prefix] || 'nodule' @file = "#{prefix}-#{::Process.pid}-#{Nodule.next_seq}#{suffix}" if opts[:directory] @is_dir = true if opts[:directory].kind_of? String FileUtils.mkdir_p File.join(opts[:directory], @file) else FileUtils.mkdir @file end else @is_dir = false # require an explicit request to create an empty file if opts[:touch] File.open @file, "w" do |f| f.puts "" end end end @cleanup = opts.has_key?(:cleanup) ? opts[:cleanup] : true super(opts) end |
Instance Attribute Details
#file ⇒ Object (readonly)
Returns the value of attribute file.
6 7 8 |
# File 'lib/nodule/tempfile.rb', line 6 def file @file end |
Instance Method Details
#stop ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/nodule/tempfile.rb', line 38 def stop if @cleanup # Ruby caches stat_t somewhere and causes race conditions, but we don't really # care here as long as the file is gone. begin FileUtils.rm_r(@file) if @is_dir File.unlink(@file) rescue Errno::ENOENT end end super end |
#to_s ⇒ Object
52 53 54 |
# File 'lib/nodule/tempfile.rb', line 52 def to_s @file end |
#touch(target = nil) ⇒ Object
33 34 35 36 |
# File 'lib/nodule/tempfile.rb', line 33 def touch(target=nil) File.open(@file, "w+").close @file end |