Class: Opium::Model::Batchable::Batch

Inherits:
Object
  • Object
show all
Defined in:
lib/opium/model/batchable/batch.rb

Constant Summary collapse

MAX_BATCH_SIZE =
50

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBatch

Returns a new instance of Batch.



7
8
9
10
# File 'lib/opium/model/batchable/batch.rb', line 7

def initialize
  self.depth = 0
  self.queue = []
end

Instance Attribute Details

#depthObject

Returns the value of attribute depth.



12
13
14
# File 'lib/opium/model/batchable/batch.rb', line 12

def depth
  @depth
end

#ownerObject

Returns the value of attribute owner.



12
13
14
# File 'lib/opium/model/batchable/batch.rb', line 12

def owner
  @owner
end

#queueObject

Returns the value of attribute queue.



12
13
14
# File 'lib/opium/model/batchable/batch.rb', line 12

def queue
  @queue
end

Instance Method Details

#ascendObject



18
19
20
# File 'lib/opium/model/batchable/batch.rb', line 18

def ascend
  self.depth -= 1
end

#diveObject



14
15
16
# File 'lib/opium/model/batchable/batch.rb', line 14

def dive
  self.depth += 1
end

#enqueue(operation) ⇒ Object



22
23
24
25
# File 'lib/opium/model/batchable/batch.rb', line 22

def enqueue( operation )
  operation = Operation.new( operation ) if operation.is_a?( Hash )
  self.queue.push( operation ) && operation
end

#executeObject



27
28
29
30
31
32
33
34
# File 'lib/opium/model/batchable/batch.rb', line 27

def execute
  if depth > 0
    ascend
  else
    batches = to_parse
    batches.each {|batch| owner.http_post( batch ) } if batches.present?
  end
end

#to_parseObject



36
37
38
# File 'lib/opium/model/batchable/batch.rb', line 36

def to_parse
  queue.each_slice(MAX_BATCH_SIZE).map {|operations| { requests: operations.map(&:to_parse) } }
end