unique 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ OTg3ODg5YjliNDY5YzJjZTQ4OGI1ODA0NmI1NWIzYjM3MmEwMjZiYw==
5
+ data.tar.gz: !binary |-
6
+ ZDE0MmI0YTAzYTljYWY1NTMyY2I5OTk5MmI3YWQ5NTZjOWVjN2MyMA==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ MTc3ODM0NzY4ZTRmYWVlMzA0MThiMzUxZmFlMzMyZGZlZTliOGJlYjA2ZjVk
10
+ N2ZmM2Q2NWM3ODBkNjVjMzc3NDczMGZiMDhmYjM1NjAwNjE3Mjc5MWZlYTMw
11
+ ZTI4NWE3M2IzYjI3YTA3YTA5ZGViMjZhNjM1MDljMjhhMDhkZDU=
12
+ data.tar.gz: !binary |-
13
+ NWMzN2I4NWMwZmViODI4MWViNTdkMTE0MTA3MjhiYTEwMTE1OTE2MTcwZmEy
14
+ NWUzNmY4N2VhNDJiN2RlOTYxZDc3ZmZkMzFmMzVlZDFiMmYxMDM2YTE5Y2Zm
15
+ ODkyNjU2MTAwNzlhNDAwYzIwMzgyYWZiMWEwZjY4ZTJhNjFmYWM=
data/CHANGELOG.md ADDED
@@ -0,0 +1 @@
1
+ # 0.0.1 Initial Release
data/LICENSE.txt ADDED
@@ -0,0 +1,7 @@
1
+ ISC License
2
+
3
+ Copyright (c) 2016, Brian Giaraffa
4
+
5
+ Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,58 @@
1
+ # unique
2
+
3
+ A lightweight Ruby gem that prevents a block from returning the same object twice.
4
+
5
+ ## Setup
6
+
7
+ Add it to your project's `Gemfile` (preferred) or
8
+
9
+ ```sh
10
+ $ gem install unique
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ ```ruby
16
+ require 'unique'
17
+
18
+ Unique.next! do
19
+ #...
20
+ end
21
+ ```
22
+
23
+ ### Example
24
+ ```ruby
25
+ 1.9.3-p551 :001 > Unique.next!{ rand(5) }
26
+ => 0
27
+ 1.9.3-p551 :002 > Unique.next!{ rand(5) }
28
+ => 4
29
+ 1.9.3-p551 :003 > Unique.next!{ rand(5) }
30
+ => 1
31
+ 1.9.3-p551 :004 > Unique.next!{ rand(5) }
32
+ => 2
33
+ 1.9.3-p551 :005 > Unique.next!{ rand(5) }
34
+ => 3
35
+ 1.9.3-p551 :006 > Unique.next!{ rand(5) }
36
+ Unique::NoUniqueObjects: An unused, unique object could not be found in 4096 tries (["(irb)", 6])
37
+ from /home/devbox/code/unique/lib/unique.rb:22:in `block in next!'
38
+ from /home/devbox/code/unique/lib/unique.rb:20:in `loop'
39
+ from /home/devbox/code/unique/lib/unique.rb:20:in `next!'
40
+ from (irb):6
41
+ from /home/devbox/.rvm/rubies/ruby-1.9.3-p551/bin/irb:12:in `<main>'
42
+ 1.9.3-p551 :007 >
43
+ ```
44
+
45
+ ## Config
46
+
47
+ The maximum number of times the block will be yielded looking for a unique object. Default is 4096.
48
+
49
+ ```ruby
50
+ Unique.max_tries= 10
51
+ ```
52
+
53
+ ## Is it any good?
54
+
55
+ [Yes](https://news.ycombinator.com/item?id=3067434)
56
+
57
+ ## License
58
+ [ISC](https://github.com/b264/unique/blob/master/LICENSE.txt)
@@ -0,0 +1,5 @@
1
+ # coding: utf-8
2
+ # frozen_string_literal: true
3
+ module ::Unique
4
+ VERSION= '0.0.1'
5
+ end
data/lib/unique.rb ADDED
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ # frozen_string_literal: true
3
+ module ::Unique
4
+ DEFAULT_MAX_TRIES= 4096
5
+ NoUniqueObjects= Class.new(StandardError)
6
+ NoArgumentsAllowed= Class.new(StandardError)
7
+ @@instances= Array.new
8
+ class<< self
9
+ def max_tries= num
10
+ def self.max_tries
11
+ @max_tries
12
+ end
13
+ @max_tries= [num, 1].max.to_i
14
+ end
15
+ end
16
+ self.max_tries= DEFAULT_MAX_TRIES
17
+ def self.next! *args, &block
18
+ raise NoArgumentsAllowed, 'Only a block is accepted.' unless args.empty? && block.kind_of?(Proc)
19
+ tries= 0
20
+ loop {
21
+ tries+= 1
22
+ raise NoUniqueObjects, "An unused, unique object could not be found in #{max_tries} tries (#{block.source_location.inspect})" if tries> max_tries
23
+ unless @@instances.include?(instance= yield)
24
+ @@instances.unshift instance
25
+ break instance
26
+ end
27
+ }
28
+ end
29
+ end
30
+ require_relative 'unique/version'
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: unique
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Brian Giaraffa
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-08-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 11.2.2
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 11.2.2
27
+ description: Prevents a block from returning the same object twice.
28
+ email:
29
+ - public88878878887@hotmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - CHANGELOG.md
35
+ - LICENSE.txt
36
+ - README.md
37
+ - lib/unique.rb
38
+ - lib/unique/version.rb
39
+ homepage: https://github.com/b264/unique
40
+ licenses:
41
+ - ISC
42
+ metadata: {}
43
+ post_install_message:
44
+ rdoc_options: []
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ! '>='
50
+ - !ruby/object:Gem::Version
51
+ version: 1.9.3
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ! '>='
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ requirements: []
58
+ rubyforge_project:
59
+ rubygems_version: 2.4.3
60
+ signing_key:
61
+ specification_version: 4
62
+ summary: Prevent duplicates
63
+ test_files: []