Class: Chicago::ETL::Counter Private

Inherits:
Object
  • Object
show all
Defined in:
lib/chicago/etl/counter.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Provides a thread-safe wrapper around an incrementing number.

Intended to be used for key builders, rather than using the database’s AUTO INCREMENT functionality.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(current_number = 0, &block) ⇒ Counter

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Creates a new counter.

May optionally be created with a starting count, either as a number or as a block which generates a number.

Counter.new(41).next # returns 42
Counter.new { 2 + 2 }.next # returns 5


23
24
25
26
27
28
29
30
# File 'lib/chicago/etl/counter.rb', line 23

def initialize(current_number=0, &block)
  @mutex = Mutex.new
  if block
    @block = block
  else
    @current = current_number || 0
  end
end

Instance Attribute Details

#currentObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the current number this counter is on.



13
14
15
# File 'lib/chicago/etl/counter.rb', line 13

def current
  @current
end

Instance Method Details

#nextObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the next number.

Modifies the current state of the counter.



35
36
37
38
39
40
# File 'lib/chicago/etl/counter.rb', line 35

def next
  @current = (@block.call || 0) if @current.nil?
  @mutex.synchronize do
    @current += 1
  end
end