module Opium::Model::Batchable::ClassMethods

Public Instance Methods

batch( options = {} ) { || ... } click to toggle source
# File lib/opium/model/batchable.rb, line 12
def batch( options = {} )
  raise ArgumentError, 'no block given' unless block_given?
  create_batch
  fiber = Fiber.new { yield }
  subfibers = []
  subfibers << fiber.resume while fiber.alive?
ensure
  delete_batch
end
batched?() click to toggle source
# File lib/opium/model/batchable.rb, line 22
def batched?
  batch_pool[Thread.current].present?
end
create_batch() click to toggle source
# File lib/opium/model/batchable.rb, line 26
def create_batch
  batch = current_batch_job
  if batch
    batch.dive && batch
  else
    self.current_batch_job = Batch.new
  end
end
current_batch_job() click to toggle source
# File lib/opium/model/batchable.rb, line 46
def current_batch_job
  batch_pool[Thread.current]
end
current_batch_job=( value ) click to toggle source
# File lib/opium/model/batchable.rb, line 50
def current_batch_job=( value )
  batch_pool[Thread.current] = value
end
delete_batch() click to toggle source
# File lib/opium/model/batchable.rb, line 35
def delete_batch
  batch = current_batch_job
  fail 'No current batch job!' unless batch
  if batch.depth == 0
    self.current_batch_job = nil
  else
    batch.execute
    batch
  end
end
http_delete( id, options = {} ) click to toggle source
Calls superclass method
# File lib/opium/model/batchable.rb, line 72
def http_delete( id, options = {} )
  if batched?
    current_batch_job.enqueue( method: :delete, path: resource_name( id ) )
    Fiber.yield
  else
    super
  end
end
http_post( data, options = {} ) click to toggle source
Calls superclass method
# File lib/opium/model/batchable.rb, line 54
def http_post( data, options = {} )
  if batched?
    current_batch_job.enqueue( method: :post, path: resource_name, body: data )
    Fiber.yield
  else
    super
  end
end
http_put( id, data, options = {} ) click to toggle source
Calls superclass method
# File lib/opium/model/batchable.rb, line 63
def http_put( id, data, options = {} )
  if batched?
    current_batch_job.enqueue( method: :put, path: resource_name( id ), body: data )
    Fiber.yield
  else
    super
  end
end

Private Instance Methods

batch_pool() click to toggle source
# File lib/opium/model/batchable.rb, line 83
def batch_pool
  @batch_pool ||= {}
end
thread_local_id() click to toggle source
# File lib/opium/model/batchable.rb, line 87
def thread_local_id
  @thread_local_id ||= :"#{ Module.nesting.first.name.parameterize('_') }_current_batch_job"
end