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-1.0.1 lib/lederhosen/buffer.rb
lederhosen-1.0.0 lib/lederhosen/buffer.rb
lederhosen-0.5.7 lib/lederhosen/buffer.rb
lederhosen-0.5.6 lib/lederhosen/buffer.rb
lederhosen-0.5.5 lib/lederhosen/buffer.rb
lederhosen-0.5.4 lib/lederhosen/buffer.rb
lederhosen-0.5.2 lib/lederhosen/buffer.rb
lederhosen-0.5.1 lib/lederhosen/buffer.rb
lederhosen-0.5.0 lib/lederhosen/buffer.rb
lederhosen-0.4.0 lib/lederhosen/buffer.rb
lederhosen-0.3.9 lib/lederhosen/buffer.rb
lederhosen-0.3.8 lib/lederhosen/buffer.rb
lederhosen-0.3.7 lib/lederhosen/buffer.rb
lederhosen-0.3.6 lib/lederhosen/buffer.rb
lederhosen-0.3.5 lib/lederhosen/buffer.rb
lederhosen-0.3.4 lib/lederhosen/buffer.rb
lederhosen-0.3.3 lib/lederhosen/buffer.rb
lederhosen-0.3.2 lib/lederhosen/buffer.rb
lederhosen-0.3.1 lib/lederhosen/buffer.rb
lederhosen-0.3.0 lib/lederhosen/buffer.rb