unique (DataMapper::Sweatshop::Unique)

    # File lib/dm-sweatshop/unique.rb, line 25
          def unique(key = nil, &block)
            if block.arity < 1
              UniqueWorker.unique_map ||= {}

              key ||= UniqueWorker.key_for(&block)
              set = UniqueWorker.unique_map[key] || Set.new
              result = block[]
              tries = 0
              while set.include?(result)
                result = block[]
                tries += 1

                raise TooManyTriesException.new("Could not generate unique value after #{tries} attempts") if tries >= UniqueWorker::MAX_TRIES
              end
              set << result
              UniqueWorker.unique_map[key] = set
            else
              UniqueWorker.count_map ||= Hash.new() { 0 }

              key ||= UniqueWorker.key_for(&block)
              result = block[UniqueWorker.count_map[key]]
              UniqueWorker.count_map[key] += 1
            end

            result
          end