class Opium::Model::Batchable::Batch

Constants

MAX_BATCH_SIZE

Attributes

depth[RW]
owner[RW]
queue[RW]

Public Class Methods

new() click to toggle source
# File lib/opium/model/batchable/batch.rb, line 7
def initialize
  self.depth = 0
  self.queue = []
end

Public Instance Methods

ascend() click to toggle source
# File lib/opium/model/batchable/batch.rb, line 18
def ascend
  self.depth -= 1
end
dive() click to toggle source
# File lib/opium/model/batchable/batch.rb, line 14
def dive
  self.depth += 1
end
enqueue( operation ) click to toggle source
# 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
execute() click to toggle source
# 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_parse() click to toggle source
# 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