no_entry 0.1.00

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. data/no_entry.rb +92 -0
  2. metadata +63 -0
@@ -0,0 +1,92 @@
1
+ if caller.first =~ /`(require|load)'/
2
+ def no_entry(lock_key = :"__no_entry_lock__#{caller.first}")
3
+ raise ArgumentError, "block expected" unless block_given?
4
+ return if Thread.current[lock_key]
5
+ Thread.current[lock_key] = true
6
+ begin
7
+ yield
8
+ ensure
9
+ Thread.current[lock_key] = false
10
+ end
11
+ end
12
+ else
13
+ eval <<-RUBY
14
+ return Gem::Specification.new do |s|
15
+ s.name = "no_entry"
16
+ s.version = "0.1.00"
17
+ s.summary = "Protects a block of code against attempts to re-enter it"
18
+ s.description = "Protects blocks of code against attempts to re-enter them using a thread local"
19
+ s.author = "Charlie Somerville"
20
+ s.email = "charlie@charliesomerville.com"
21
+ s.homepage = "https://gist.github.com/charliesome/4967020"
22
+ s.files = "no_entry.rb"
23
+ s.require_path = "."
24
+
25
+ s.add_development_dependency "rspec"
26
+ end
27
+ RUBY
28
+ end
29
+
30
+ require "rspec" if $0 == __FILE__
31
+ if defined? RSpec
32
+ describe "no_entry" do
33
+ it "should call the passed block in the simple case" do
34
+ called = 0
35
+ no_entry { called += 1 }
36
+ called.should == 1
37
+ end
38
+
39
+ it "should return the value returned by the block" do
40
+ no_entry { 123 }.should == 123
41
+ end
42
+
43
+ context "when passed an explicit key" do
44
+ it "should prevent re-entry on the same key" do
45
+ called = 0
46
+ no_entry :lolz do
47
+ called += 1
48
+ no_entry :lolz do
49
+ called += 1
50
+ end
51
+ end
52
+ called.should == 1
53
+ end
54
+
55
+ it "should prevent re-entry on different keys" do
56
+ called = 0
57
+ no_entry :lolz1 do
58
+ called += 1
59
+ no_entry :lolz2 do
60
+ called += 1
61
+ end
62
+ end
63
+ called.should == 2
64
+ end
65
+ end
66
+
67
+ context "when using the implicit lock key" do
68
+ it "should allow re-entry on different lines" do
69
+ called = 0
70
+ no_entry do
71
+ called += 1
72
+ no_entry do
73
+ called += 1
74
+ end
75
+ end
76
+ called.should == 2
77
+ end
78
+
79
+ it "should prevent re-entry on the same line" do
80
+ locker = ->(&bk) { no_entry &bk }
81
+ called = 0
82
+ locker[&->{
83
+ called += 1
84
+ locker[&->{
85
+ called += 1
86
+ }]
87
+ }]
88
+ called.should == 1
89
+ end
90
+ end
91
+ end
92
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: no_entry
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.00
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Charlie Somerville
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-16 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description: Protects blocks of code against attempts to re-enter them using a thread
31
+ local
32
+ email: charlie@charliesomerville.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - no_entry.rb
38
+ homepage: https://gist.github.com/charliesome/4967020
39
+ licenses: []
40
+ post_install_message:
41
+ rdoc_options: []
42
+ require_paths:
43
+ - .
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ none: false
46
+ requirements:
47
+ - - ! '>='
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ requirements: []
57
+ rubyforge_project:
58
+ rubygems_version: 1.8.24
59
+ signing_key:
60
+ specification_version: 3
61
+ summary: Protects a block of code against attempts to re-enter it
62
+ test_files: []
63
+ has_rdoc: