Class: Nodule::Tempfile

Inherits:
Base
  • Object
show all
Defined in:
lib/nodule/tempfile.rb

Direct Known Subclasses

UnixServer, UnixSocket, ZeroMQ

Instance Attribute Summary collapse

Attributes inherited from Base

#prefix, #read_count, #readers, #running, #topology

Instance Method Summary collapse

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

#fileObject (readonly)

Returns the value of attribute file.



6
7
8
# File 'lib/nodule/tempfile.rb', line 6

def file
  @file
end

Instance Method Details

#stopObject



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_sObject



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