Sha256: e13eb8040adbef9a96239b779f31339779049230dd0ccd970d378b24239ef488

Contents?: true

Size: 1.31 KB

Versions: 4

Compression:

Stored size: 1.31 KB

Contents

class FluQ::Buffer::File < FluQ::Buffer::Base

  # @attr_reader [File] file instance
  attr_reader :file

  # @see FluQ::Buffer::Base#initialize
  def initialize(*)
    super
    @file = new_file
    @size = 0
  end

  # @see FluQ::Buffer::Base#name
  def name
    @name ||= [super, File.basename(file.path)].join("-")
  end

  # @see FluQ::Buffer::Base#write
  def write(data)
    file.write(data)
  end

  # @see FluQ::Buffer::Base#size
  def size
    file.size
  end

  # @see FluQ::Buffer::Base#close
  def close
    file.close unless file.closed?
    File.unlink(file.path) if File.exists?(file.path)
  end

  # @see FluQ::Buffer::Base#drain
  def drain
    file.close unless file.closed?
    io = File.open(file.path, 'rb', encoding: Encoding::BINARY)
    yield(io)
  ensure
    io.close if io
  end

  protected

    def defaults
      super.merge(path: "tmp/buffers")
    end

    def new_file
      path = nil
      incr = 0
      path = root.join(generate_name(incr+=1)) until path && !path.exist?
      file = path.open("wb", encoding: Encoding::BINARY)
      file.sync = true
      file
    end

    def root
      @root ||= FluQ.root.join(config[:path]).tap do |full_path|
        FileUtils.mkdir_p full_path.to_s
      end
    end

    def generate_name(index)
      "fb-#{(Time.now.utc.to_f * 1000).round}.#{index}"
    end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
fluq-0.7.5 lib/fluq/buffer/file.rb
fluq-0.7.3 lib/fluq/buffer/file.rb
fluq-0.7.1 lib/fluq/buffer/file.rb
fluq-0.7.0 lib/fluq/buffer/file.rb