Class: IncreasingMicrosecondClock

Inherits:
Object
  • Object
show all
Defined in:
lib/increasing_microsecond_clock.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(timestamp_factory = lambda { Time.stamp }, mutex = Mutex.new) ⇒ IncreasingMicrosecondClock

Returns a new instance of IncreasingMicrosecondClock.



4
5
6
7
8
9
# File 'lib/increasing_microsecond_clock.rb', line 4

def initialize(timestamp_factory = lambda { Time.stamp },
               mutex             = Mutex.new)
  @timestamp_factory = timestamp_factory
  @mutex             = mutex
  @time              = @timestamp_factory.call
end

Class Attribute Details

.instanceObject

Returns the value of attribute instance.



27
28
29
# File 'lib/increasing_microsecond_clock.rb', line 27

def instance
  @instance
end

Class Method Details

.callObject



29
30
31
# File 'lib/increasing_microsecond_clock.rb', line 29

def call
  instance.call
end

Instance Method Details

#callObject



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/increasing_microsecond_clock.rb', line 11

def call
  @mutex.synchronize {
    new_time = @timestamp_factory.call

    @time =
      if new_time > @time
        new_time
      else
        @time + 1
      end
  }
end