Class: Basil::Buffer
- Inherits:
-
Object
- Object
- Basil::Buffer
- Defined in:
- lib/basil/buffer.rb
Instance Method Summary collapse
- #[](k) ⇒ Object
-
#add_to(bucket, obj) ⇒ Object
Add an object to the buffer.
-
#finalize ⇒ Object
Writes out leftover objects.
-
#initialize(args = {}) ⇒ Buffer
constructor
Create a new buffer.
Constructor Details
#initialize(args = {}) ⇒ Buffer
Create a new buffer
7 8 9 10 |
# File 'lib/basil/buffer.rb', line 7 def initialize(args={}) @buffer = Hash.new { |h, k| h[k] = Array.new } @buffer_max = args[:buffer_max] || 100_000 end |
Instance Method Details
#[](k) ⇒ Object
32 33 34 |
# File 'lib/basil/buffer.rb', line 32 def [](k) @buffer[k] end |
#add_to(bucket, obj) ⇒ Object
Add an object to the buffer
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/basil/buffer.rb', line 15 def add_to(bucket, obj) @buffer[bucket] << obj 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 |
#finalize ⇒ Object
Writes out leftover objects
39 40 41 42 43 44 45 46 47 |
# File 'lib/basil/buffer.rb', line 39 def finalize @buffer.each_key do |bucket| File.open(bucket, 'a+') do |out| @buffer[bucket].each do |v| out.puts v end end end end |