Class: Opium::Model::Batchable::Batch
- Defined in:
- lib/opium/model/batchable/batch.rb
Constant Summary collapse
- MAX_BATCH_SIZE =
50
Instance Attribute Summary collapse
-
#depth ⇒ Object
Returns the value of attribute depth.
-
#owner ⇒ Object
Returns the value of attribute owner.
-
#queue ⇒ Object
Returns the value of attribute queue.
Instance Method Summary collapse
- #ascend ⇒ Object
- #dive ⇒ Object
- #enqueue(operation) ⇒ Object
- #execute ⇒ Object
-
#initialize ⇒ Batch
constructor
A new instance of Batch.
- #to_parse ⇒ Object
Constructor Details
#initialize ⇒ Batch
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
#depth ⇒ Object
Returns the value of attribute depth.
12 13 14 |
# File 'lib/opium/model/batchable/batch.rb', line 12 def depth @depth end |
#owner ⇒ Object
Returns the value of attribute owner.
12 13 14 |
# File 'lib/opium/model/batchable/batch.rb', line 12 def owner @owner end |
#queue ⇒ Object
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
#ascend ⇒ Object
18 19 20 |
# File 'lib/opium/model/batchable/batch.rb', line 18 def ascend self.depth -= 1 end |
#dive ⇒ Object
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 |
#execute ⇒ Object
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_parse ⇒ Object
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 |