Class: Riml::FileRollback

Inherits:
Object
  • Object
show all
Defined in:
lib/riml/file_rollback.rb

Class Method Summary collapse

Class Method Details

.clearObject



54
55
56
57
58
59
# File 'lib/riml/file_rollback.rb', line 54

def self.clear
  @m.synchronize do
    @previous_file_states.clear
    @files_created.clear
  end
end

.creating_file(full_path) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/riml/file_rollback.rb', line 45

def self.creating_file(full_path)
  @m.synchronize do
    return unless @guarding > 0
    previous_state = File.file?(full_path) ? File.read(full_path) : nil
    @previous_file_states[full_path] ||= previous_state
    @files_created << full_path
  end
end

.guard(&block) ⇒ Object

NOTE: Used only in main thread. Only call this method in one thread at a time. It’s okay if ‘&block` launches threads, and they compile files though.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/riml/file_rollback.rb', line 14

def self.guard(&block)
  @guarding += 1
  if block
    block.call
  # to increase `@guarding` only, for use with FileRollback.trap()
  else
    return
  end
rescue
  rollback!
  raise
ensure
  if block
    @guarding -= 1
    if @guarding == 0
      clear
    end
  end
end

.rollback!Object



61
62
63
64
65
66
67
68
69
# File 'lib/riml/file_rollback.rb', line 61

def self.rollback!
  @m.synchronize do
    @files_created.each do |path|
      rollback_file!(path)
    end
    @previous_file_states.clear
    @files_created.clear
  end
end

.trap(*signals, &block) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/riml/file_rollback.rb', line 34

def self.trap(*signals, &block)
  signals.each do |sig|
    Signal.trap(sig) do
      if @guarding > 0
        rollback!
        block.call if block
      end
    end
  end
end