Class: Batchr

Inherits:
Object
  • Object
show all
Defined in:
lib/batchr.rb,
lib/batchr/version.rb

Constant Summary collapse

VERSION =
"0.1.0"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Batchr

Returns a new instance of Batchr.



14
15
16
17
# File 'lib/batchr.rb', line 14

def initialize opts = {}
  @batch_size = opts[:batch_size] ? opts[:batch_size] : 400
  @bucket = []
end

Instance Attribute Details

#batch_sizeObject

Returns the value of attribute batch_size.



3
4
5
# File 'lib/batchr.rb', line 3

def batch_size
  @batch_size
end

#bucketObject (readonly)

Returns the value of attribute bucket.



3
4
5
# File 'lib/batchr.rb', line 3

def bucket
  @bucket
end

#messageObject

Returns the value of attribute message.



4
5
6
# File 'lib/batchr.rb', line 4

def message
  @message
end

#receiverObject

Returns the value of attribute receiver.



4
5
6
# File 'lib/batchr.rb', line 4

def receiver
  @receiver
end

Class Method Details

.batch(receiver, message) {|batchr| ... } ⇒ Object

Yields:

  • (batchr)


6
7
8
9
10
11
12
# File 'lib/batchr.rb', line 6

def self.batch receiver, message
  batchr = new
  batchr.receiver = receiver
  batchr.message = message
  yield batchr
  batchr.finish
end

Instance Method Details

#<<(value) ⇒ Object



24
25
26
27
# File 'lib/batchr.rb', line 24

def << value
  @bucket << value
  run_if_necessary
end

#empty_bucketObject



33
34
35
# File 'lib/batchr.rb', line 33

def empty_bucket
  @bucket = []
end

#run_batchObject Also known as: finish



37
38
39
40
41
42
# File 'lib/batchr.rb', line 37

def run_batch
  unless bucket.empty?
    receiver.send(message, bucket)
    empty_bucket
  end
end

#run_if_necessaryObject



29
30
31
# File 'lib/batchr.rb', line 29

def run_if_necessary
  run_batch if @bucket.size >= @batch_size
end