Module: Condenser::Utils

Defined in:
lib/condenser/utils.rb

Class Method Summary collapse

Class Method Details

.atomic_write(filename) ⇒ Object

Public: Write to a file atomically. Useful for situations where you don’t want other processes or threads to see half-written files.

Utils.atomic_write('important.file') do |file|
  file.write('hello')
end

Returns nothing.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/condenser/utils.rb', line 12

def self.atomic_write(filename)
  dirname, basename = File.split(filename)
  basename = [
    basename,
    Thread.current.object_id,
    Process.pid,
    rand(1000000)
  ].join('.'.freeze)
  tmpname = File.join(dirname, basename)

  File.open(tmpname, 'wb+') do |f|
    yield f
  end

  File.rename(tmpname, filename)
ensure
  File.delete(tmpname) if File.exist?(tmpname)
end

.module_include(base, mod) ⇒ Object

Internal: Inject into target module for the duration of the block.

mod - Module

Returns result of block.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/condenser/utils.rb', line 36

def self.module_include(base, mod)
  old_methods = {}

  mod.instance_methods.each do |sym|
    old_methods[sym] = base.instance_method(sym) if base.method_defined?(sym)
  end

  mod.instance_methods.each do |sym|
    method = mod.instance_method(sym)
    base.send(:define_method, sym, method)
  end

  yield
ensure
  mod.instance_methods.each do |sym|
    base.send(:undef_method, sym) if base.method_defined?(sym)
  end
  old_methods.each do |sym, method|
    base.send(:define_method, sym, method)
  end
end