Sha256: f2676b39e9aacf5a2ea357afba0f8ebc7f1481038379d6c61ad3e1aaa4a714c7

Contents?: true

Size: 1002 Bytes

Versions: 37

Compression:

Stored size: 1002 Bytes

Contents

module Lederhosen

  class Buffer
    # for when you need to write out to a shitload of files.

    #
    # Create a new buffer
    #
    def initialize(args={})
      @buffer = Hash.new { |h, k| h[k] = Array.new }
      @buffer_max = args[:buffer_max] || 100_000
    end

    #
    # Add an object to the buffer
    #
    def add_to bucket, obj

      @buffer[bucket] << obj.to_s

      if @buffer[bucket].length > @buffer_max
        # write out
        File.open(bucket, 'a+') do |out|
          @buffer[bucket].each do |v|
            out.puts v
          end
        end

        # clear that bucket
        @buffer[bucket].clear
      end
    end

    def [] k
      @buffer[k]
    end

    #
    # Writes out leftover objects
    #
    def finalize
      @buffer.each_key do |bucket|
        File.open(bucket, 'a+') do |out|
          @buffer[bucket].each do |v|
            out.puts v
          end
        end
      end
      @buffer = Hash.new { |h, k| h[k] = Array.new }
    end

  end

end

Version data entries

37 entries across 37 versions & 1 rubygems

Version Path
lederhosen-0.2.13 lib/lederhosen/buffer.rb
lederhosen-0.2.12 lib/lederhosen/buffer.rb
lederhosen-0.2.11 lib/lederhosen/buffer.rb
lederhosen-0.2.10 lib/lederhosen/buffer.rb
lederhosen-0.2.9 lib/lederhosen/buffer.rb
lederhosen-0.2.8 lib/lederhosen/buffer.rb
lederhosen-0.2.7 lib/lederhosen/buffer.rb
lederhosen-0.2.6 lib/lederhosen/buffer.rb
lederhosen-0.2.5 lib/lederhosen/buffer.rb
lederhosen-0.2.4 lib/lederhosen/buffer.rb
lederhosen-0.2.3 lib/lederhosen/buffer.rb
lederhosen-0.2.2 lib/lederhosen/buffer.rb
lederhosen-0.2.1 lib/lederhosen/buffer.rb
lederhosen-0.2.0 lib/lederhosen/buffer.rb
lederhosen-0.1.9 lib/lederhosen/buffer.rb
lederhosen-0.1.8 lib/lederhosen/buffer.rb
lederhosen-0.1.7 lib/lederhosen/buffer.rb