Module: DataMapper::Sweatshop::Unique

Module | DataMapper::Sweatshop::Unique
In: | lib/dm-sweatshop/unique.rb

### Methods

unique

### Classes and Modules

Class DataMapper::Sweatshop::Unique::TooManyTriesException

### Public Instance methods

unique(key = nil, &block)

Yields a value to the block. The value is unique for each invocation with the same block. Alternatively, you may provide an explicit key to identify the block.

If a block with no parameter is supplied, unique keeps track of previous invocations, and will continue yielding until a unique value is generated. If a unique value is not generated after @UniqueWorker::MAX_TRIES@, an exception is raised.

ParseTree is required unless an explicit key is provided

      (1..3).collect { unique {|x| x }}     # => [0, 1, 2]
      (1..3).collect { unique {|x| x + 1 }} # => [1, 2, 3]
      (1..3).collect { unique {|x| x }}     # => [3, 4, 5] # Continued on from above
      (1..3).collect { unique(:a) {|x| x }} # => [0, 1, 2] # Explicit key overrides block identity

      a = [1, 1, 1, 2, 2, 3]
      (1..3).collect { unique { a.shift }}  # => [1, 2, 3]
      (1..3).collect { unique { 1 }}        # raises TooManyTriesException

return <Object> the return value of the block

[Validate]