Module: Opium::Model::Batchable::ClassMethods

Defined in:
lib/opium/model/batchable.rb

Instance Method Summary collapse

Instance Method Details

#batch(options = {}) ⇒ Object



12
13
14
15
16
17
18
19
20
# 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?Boolean

Returns:



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

def batched?
  batch_pool[Thread.current].present?
end

#create_batchObject



26
27
28
29
30
31
32
33
# 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_jobObject



46
47
48
# File 'lib/opium/model/batchable.rb', line 46

def current_batch_job
  batch_pool[Thread.current]
end

#current_batch_job=(value) ⇒ Object



50
51
52
# File 'lib/opium/model/batchable.rb', line 50

def current_batch_job=( value )
  batch_pool[Thread.current] = value
end

#delete_batchObject



35
36
37
38
39
40
41
42
43
44
# 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 = {}) ⇒ Object



72
73
74
75
76
77
78
79
# 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 = {}) ⇒ Object



54
55
56
57
58
59
60
61
# 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 = {}) ⇒ Object



63
64
65
66
67
68
69
70
# 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