Class: Tumugi::AtomicFile

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/tumugi/atomic_file.rb

Direct Known Subclasses

Plugin::AtomicLocalFile

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ AtomicFile

Returns a new instance of AtomicFile.



12
13
14
# File 'lib/tumugi/atomic_file.rb', line 12

def initialize(path)
  @path = path
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



16
17
18
# File 'lib/tumugi/atomic_file.rb', line 16

def path
  @path
end

Instance Method Details

#closeObject



31
32
33
34
35
36
37
# File 'lib/tumugi/atomic_file.rb', line 31

def close
  if @temp_file
    move_to_final_destination(@temp_file)
    @temp_file.close
    @temp_file = nil
  end
end

#move_to_final_destination(temp_file) ⇒ Object

Raises:

  • (NotImplementedError)


39
40
41
# File 'lib/tumugi/atomic_file.rb', line 39

def move_to_final_destination(temp_file)
  raise NotImplementedError, "You must implement #{self.class}##{__method__}"
end

#open(&block) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/tumugi/atomic_file.rb', line 18

def open(&block)
  if block_given?
    Tempfile.open(basename) do |fp|
      @temp_file = fp
      block.call(self)
      close
    end
  else
    @temp_file = Tempfile.open(basename)
  end
  self
end