Sha256: 413d87d09877b18535d03c7cb62712319b7c6f5fad6c6a2cdda4b8c3b06164c8

Contents?: true

Size: 896 Bytes

Versions: 1

Compression:

Stored size: 896 Bytes

Contents

class XBEL::Writer #:nodoc:
  DOCTYPE = '<!DOCTYPE xbel PUBLIC "+//IDN python.org//DTD XML Bookmark Exchange Language 1.0//EN//XML" "http://www.python.org/topics/xml/dtds/xbel-1.0.dtd">'

  attr_reader :document, :lockfile_path, :path
  def new(document, path)
    @document, @path = document, path
    @lockfile_path = "#{ path }.lock"
  end

  def write
    synchronize do
      File.open path, 'w' do |file|
        file.puts DOCTYPE
        file << document
      end
    end
  end

  protected

    def locked?(path)
      File.exists? lockfile_path
    end
    def lock
      File.open(lockfile_path, 'w') { |*| yield }
    ensure
      File.unlink lockfile_path
    end

    def synchronize
      current_thread = Thread.current

      while locked?
        watch(lockfile_path, :delete) { |*| current_thread.run }
        current_thread.sleep
      end

      lock { yield }
    end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
xbel-0.1.2 lib/xbel/writer.rb