class Ro::Lock

Public Class Methods

new(path) click to toggle source
# File lib/ro/lock.rb, line 3
def initialize(path)
  @path = path.to_s
  @fd = false
end

Public Instance Methods

lock(&block) click to toggle source
# File lib/ro/lock.rb, line 8
def lock(&block)
  open!

  if block
    begin
      lock!
      block.call
    ensure
      unlock!
    end
  else
    self
  end
end
lock!() click to toggle source
# File lib/ro/lock.rb, line 43
def lock!
  open!
  @fd.flock File::LOCK_EX
end
open!() click to toggle source
# File lib/ro/lock.rb, line 23
def open!
  @fd ||= (
    fd =
      begin
        open(@path, 'ab+')
      rescue
        unless test(?e, @path)
          FileUtils.mkdir_p(@path)
          FileUtils.touch(@path)
        end

        open(@path, 'ab+')
      end

    fd.close_on_exec = true

    fd
  )
end
unlock!() click to toggle source
# File lib/ro/lock.rb, line 48
def unlock!
  open!
  @fd.flock File::LOCK_UN
end