Class: Lexster::Batch
- Inherits:
-
Object
- Object
- Lexster::Batch
- Defined in:
- lib/lexster/batch.rb
Class Method Summary collapse
- .current_batch ⇒ Object
- .current_batch=(batch) ⇒ Object
- .default_options ⇒ Object
- .reset_current_batch ⇒ Object
Instance Method Summary collapse
- #<<(command) ⇒ Object
- #commands ⇒ Object
- #count ⇒ Object
- #default_options=(value) ⇒ Object
-
#initialize(options = {}, &block) ⇒ Batch
constructor
A new instance of Batch.
- #results ⇒ Object
- #run ⇒ Object
- #thens ⇒ Object
Constructor Details
#initialize(options = {}, &block) ⇒ Batch
Returns a new instance of Batch.
23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/lexster/batch.rb', line 23 def initialize(={}, &block) if .respond_to?(:call) && !block block = = {} end .reverse_merge!(self.class.) @options = @block = block end |
Class Method Details
.current_batch ⇒ Object
11 12 13 |
# File 'lib/lexster/batch.rb', line 11 def self.current_batch Thread.current[:lexster_current_batch] end |
.current_batch=(batch) ⇒ Object
15 16 17 |
# File 'lib/lexster/batch.rb', line 15 def self.current_batch=(batch) Thread.current[:lexster_current_batch] = batch end |
.default_options ⇒ Object
7 8 9 |
# File 'lib/lexster/batch.rb', line 7 def self. @default_options ||= { batch_size: 200, individual_promises: true } end |
.reset_current_batch ⇒ Object
19 20 21 |
# File 'lib/lexster/batch.rb', line 19 def self.reset_current_batch Thread.current[:lexster_current_batch] = nil end |
Instance Method Details
#<<(command) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/lexster/batch.rb', line 35 def <<(command) commands << command if commands.length >= @options[:batch_size] flush_batch end if @options[:individual_promises] promise = SingleResultPromiseProxy.new(command) thens << promise promise end end |
#commands ⇒ Object
49 50 51 |
# File 'lib/lexster/batch.rb', line 49 def commands @commands ||= [] end |
#count ⇒ Object
57 58 59 |
# File 'lib/lexster/batch.rb', line 57 def count @commands ? @commands.count : 0 end |
#default_options=(value) ⇒ Object
3 4 5 |
# File 'lib/lexster/batch.rb', line 3 def (value) @default_options = value end |
#results ⇒ Object
61 62 63 |
# File 'lib/lexster/batch.rb', line 61 def results @results ||= [] end |
#run ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/lexster/batch.rb', line 65 def run self.class.current_batch = self begin @block.call(self) ensure self.class.reset_current_batch end Lexster.logger.info "Lexster batch (#{commands.length} commands)" flush_batch BatchPromiseProxy.new(results) end |
#thens ⇒ Object
53 54 55 |
# File 'lib/lexster/batch.rb', line 53 def thens @thens ||= [] end |