Class: Variable::IVar

Inherits:
Variable show all
Defined in:
lib/variable.rb

Overview

Shared variable that can be written at most once

ignore :reek:InstanceVariableAssumption

Defined Under Namespace

Classes: Error

Constant Summary

Constants inherited from Variable

EMPTY, TIMEOUT

Instance Method Summary collapse

Methods inherited from Variable

#initialize, #read, #take, #take_timeout, #try_put, #with

Constructor Details

This class inherits a constructor from Variable

Instance Method Details

#populate_withObject

Populate and return value, use block to compute value if empty

The block is guaranteed to be executed at max once.

Subsequent reads are guaranteed to return the block value.

Returns:

  • (Object)


250
251
252
253
254
255
256
257
258
# File 'lib/variable.rb', line 250

def populate_with
  return @value if full?

  synchronize do
    perform_put(yield) if empty?
  end

  @value
end

#put(value) ⇒ self

Put value, raises if already full

Parameters:

  • value (Object)

Returns:

  • (self)

Raises:

  • Error if already full



234
235
236
237
238
239
240
241
# File 'lib/variable.rb', line 234

def put(value)
  synchronize do
    fail Error, 'is immutable' if full?
    perform_put(value)
  end

  self
end