callback-locker 0.1.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5e38ddbf64c1989a65c8ae972c59727ffc29e9d8
4
+ data.tar.gz: 93c024695d68ea5e78a6e0867f020b1c373addd4
5
+ SHA512:
6
+ metadata.gz: 5c54a12f7d18440f4a4623ea3b2f91959c1ffeb6fb8a002765ccf72c88857164d58aaf36030882e2488e377425da1b0f53a85ef352af4fc38531b6cf7524b59e
7
+ data.tar.gz: 807f23545b9e4f8a0dab1911c1c9b74795335199379d6a454938a8b2674d26f6a2e6ad98b00b112573f2e4e6fae9e0003f6e786805dedec088172e459db7bcf2
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
@@ -0,0 +1,18 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <projectDescription>
3
+ <name>mp Ruby Callback Locker</name>
4
+ <comment></comment>
5
+ <projects>
6
+ </projects>
7
+ <buildSpec>
8
+ <buildCommand>
9
+ <name>com.aptana.ide.core.unifiedBuilder</name>
10
+ <arguments>
11
+ </arguments>
12
+ </buildCommand>
13
+ </buildSpec>
14
+ <natures>
15
+ <nature>com.aptana.ruby.core.rubynature</nature>
16
+ <nature>com.aptana.projects.webnature</nature>
17
+ </natures>
18
+ </projectDescription>
@@ -0,0 +1,3 @@
1
+
2
+ 0.1.0 (2015-07-18)
3
+ * initial version
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+
5
+ # Add dependencies to develop your gem here.
6
+ # Include everything needed to run rake, tests, features, etc.
7
+ group :development do
8
+ gem "bundler", ">= 1.0.0"
9
+ gem "jeweler", ">= 1.5.2"
10
+ gem "riot", ">= 0.12.3"
11
+ end
@@ -0,0 +1,60 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ addressable (2.3.8)
5
+ builder (3.2.2)
6
+ descendants_tracker (0.0.4)
7
+ thread_safe (~> 0.3, >= 0.3.1)
8
+ faraday (0.9.1)
9
+ multipart-post (>= 1.2, < 3)
10
+ git (1.2.9.1)
11
+ github_api (0.12.3)
12
+ addressable (~> 2.3)
13
+ descendants_tracker (~> 0.0.4)
14
+ faraday (~> 0.8, < 0.10)
15
+ hashie (>= 3.3)
16
+ multi_json (>= 1.7.5, < 2.0)
17
+ nokogiri (~> 1.6.3)
18
+ oauth2
19
+ hashie (3.4.2)
20
+ highline (1.7.2)
21
+ jeweler (2.0.1)
22
+ builder
23
+ bundler (>= 1.0)
24
+ git (>= 1.2.5)
25
+ github_api
26
+ highline (>= 1.6.15)
27
+ nokogiri (>= 1.5.10)
28
+ rake
29
+ rdoc
30
+ jwt (1.5.1)
31
+ mini_portile (0.6.2)
32
+ multi_json (1.11.2)
33
+ multi_xml (0.5.5)
34
+ multipart-post (2.0.0)
35
+ nokogiri (1.6.6.2)
36
+ mini_portile (~> 0.6.0)
37
+ oauth2 (1.0.0)
38
+ faraday (>= 0.8, < 0.10)
39
+ jwt (~> 1.0)
40
+ multi_json (~> 1.3)
41
+ multi_xml (~> 0.5)
42
+ rack (~> 1.2)
43
+ rack (1.6.4)
44
+ rake (10.4.2)
45
+ rdoc (4.2.0)
46
+ riot (0.12.7)
47
+ rr
48
+ rr (1.1.2)
49
+ thread_safe (0.3.5)
50
+
51
+ PLATFORMS
52
+ ruby
53
+
54
+ DEPENDENCIES
55
+ bundler (>= 1.0.0)
56
+ jeweler (>= 1.5.2)
57
+ riot (>= 0.12.3)
58
+
59
+ BUNDLED WITH
60
+ 1.10.5
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 - 2015 Martin Poljak (martin@poljak.cz)
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,46 @@
1
+ Callback Locker
2
+ ===============
3
+
4
+ "Locker" is lockable box for equipment -- at this case box for
5
+ collecting the callbacks (in its locked state) and running them
6
+ after unlocking. So, in fact, it serves as some kind of callback
7
+ semaphore or mutex.
8
+
9
+ Some trivial example:
10
+
11
+ ```ruby
12
+ require "callback-locker"
13
+ locker = CallbackLocker::new
14
+
15
+ foo = nil
16
+ locker.synchronize do
17
+ foo = "bar"
18
+ end
19
+
20
+ # ^^^ locker is unlocked, so #synchronize will execute callback
21
+ # immediately
22
+
23
+ foo = nil
24
+ locker.lock!
25
+ locker.synchronize do
26
+ foo = "1"
27
+ end
28
+ locker.synchronize do
29
+ foo << "2"
30
+ end
31
+ locker.unlock!
32
+
33
+ # ^^^ locker is locked, so callbacks are stacked and executed
34
+ # immediately after the #unlock! method is call, so foo
35
+ # will contain "12"
36
+ ```
37
+
38
+ Copyright
39
+ ---------
40
+
41
+ Copyright &copy; 2011 &ndash; 2015 [Martin Poljak][10]. See `LICENSE.txt` for
42
+ further details.
43
+
44
+ [8]: http://rubyeventmachine.com/
45
+ [9]: http://github.com/martinkozak/callback-adapter/issues
46
+ [10]: http://www.martinpoljak.net/
@@ -0,0 +1,29 @@
1
+ # encoding: utf-8
2
+ require 'rubygems'
3
+ require 'bundler'
4
+
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+
13
+ require 'rake'
14
+ require 'jeweler'
15
+
16
+ Jeweler::Tasks.new do |gem|
17
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
18
+ gem.name = "callback-locker"
19
+ gem.homepage = "http://github.com/martinkozak/callback-locker"
20
+ gem.license = "MIT"
21
+ gem.summary = 'Semaphore (mutex) like primitive for synchronizing callbacks.'
22
+ gem.email = "martin@poljak.net"
23
+ gem.authors = ["Martin Poljak"]
24
+ # Include your dependencies below. Runtime dependencies are required when using your gem,
25
+ # and development dependencies are only needed for development (ie running rake tasks, tests, etc)
26
+ # gem.add_runtime_dependency 'jabber4r', '> 0.1'
27
+ # gem.add_development_dependency 'rspec', '> 1.2.3'
28
+ end
29
+ Jeweler::RubygemsDotOrgTasks.new
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,57 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+ # stub: callback-locker 0.1.0 ruby lib
6
+
7
+ Gem::Specification.new do |s|
8
+ s.name = "callback-locker"
9
+ s.version = "0.1.0"
10
+
11
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
12
+ s.require_paths = ["lib"]
13
+ s.authors = ["Martin Poljak"]
14
+ s.date = "2015-07-18"
15
+ s.email = "martin@poljak.net"
16
+ s.extra_rdoc_files = [
17
+ "LICENSE.txt",
18
+ "README.md"
19
+ ]
20
+ s.files = [
21
+ ".document",
22
+ ".project",
23
+ "CHANGES.txt",
24
+ "Gemfile",
25
+ "Gemfile.lock",
26
+ "LICENSE.txt",
27
+ "README.md",
28
+ "Rakefile",
29
+ "VERSION",
30
+ "callback-locker.gemspec",
31
+ "lib/callback-locker.rb",
32
+ "test"
33
+ ]
34
+ s.homepage = "http://github.com/martinkozak/callback-locker"
35
+ s.licenses = ["MIT"]
36
+ s.rubygems_version = "2.4.5"
37
+ s.summary = "Semaphore (mutex) like primitive for synchronizing callbacks."
38
+
39
+ if s.respond_to? :specification_version then
40
+ s.specification_version = 4
41
+
42
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
43
+ s.add_development_dependency(%q<bundler>, [">= 1.0.0"])
44
+ s.add_development_dependency(%q<jeweler>, [">= 1.5.2"])
45
+ s.add_development_dependency(%q<riot>, [">= 0.12.3"])
46
+ else
47
+ s.add_dependency(%q<bundler>, [">= 1.0.0"])
48
+ s.add_dependency(%q<jeweler>, [">= 1.5.2"])
49
+ s.add_dependency(%q<riot>, [">= 0.12.3"])
50
+ end
51
+ else
52
+ s.add_dependency(%q<bundler>, [">= 1.0.0"])
53
+ s.add_dependency(%q<jeweler>, [">= 1.5.2"])
54
+ s.add_dependency(%q<riot>, [">= 0.12.3"])
55
+ end
56
+ end
57
+
@@ -0,0 +1,123 @@
1
+ # encoding: utf-8
2
+ # (c) 2011 Martin Kozák (martinkozak@martinkozak.net)
3
+
4
+ ##
5
+ # Runs or defers callbacks according to its internal locked
6
+ # or unlocked state.
7
+ #
8
+
9
+ class CallbackLocker
10
+
11
+ ##
12
+ # Indicates locker is locked.
13
+ # @returns [Boolean] +true+ if it's, +false+ otherwise
14
+ #
15
+
16
+ @locked
17
+
18
+ ##
19
+ # Indicates, locker is just in synchronization state,
20
+ # so stack of callbacks is evaluated just now.
21
+ #
22
+ # @returns [Boolean] +true+ if it's, +false+ otherwise
23
+ #
24
+
25
+ @syncing
26
+
27
+ ##
28
+ # Holds callback stack.
29
+ # @return [Array] array of callbacks
30
+ #
31
+
32
+ attr_accessor :stack
33
+ @stack
34
+
35
+ ##
36
+ # Constructor.
37
+ #
38
+
39
+ def initialize
40
+ @syncing = false
41
+ @locked = false
42
+ @stack = [ ]
43
+ end
44
+
45
+ ##
46
+ # Locks the locker. Note, it's also alias for
47
+ # {#try_lock} which is multiple pass-in so sets
48
+ # lock although it's locked.
49
+ #
50
+
51
+ def lock
52
+ @locked = true
53
+ end
54
+
55
+ alias :try_lock :lock
56
+ alias :lock! :lock
57
+ alias :try_lock! :try_lock
58
+
59
+ ##
60
+ # Unlocks the locker.
61
+ #
62
+
63
+ def unlock
64
+ @locked = false
65
+ self.eval!
66
+ end
67
+
68
+ alias :unlock! :unlock
69
+
70
+ ##
71
+ # Indicates, locker is locked.
72
+ # @return [Boolean] +true+ if it's, +false+ otherwise
73
+ #
74
+
75
+ def locked?
76
+ @locked
77
+ end
78
+
79
+ ##
80
+ # Indicates, locker is unlocked.
81
+ # @return [Boolean] +true+ if it's, +false+ otherwise
82
+ #
83
+
84
+ def unlocked?
85
+ not self.locked?
86
+ end
87
+
88
+ ##
89
+ # Synchronizes using the lock. Works by similiar
90
+ # way as standard +Mutex#synchronize+.
91
+ #
92
+ # @yield if locker is unlocked the given callback
93
+ #
94
+
95
+ def synchronize(&block)
96
+ if self.locked? or @syncing
97
+ @stack << block
98
+ else
99
+ @syncing = true
100
+ yield
101
+ @syncing = false
102
+
103
+ if not @stack.empty?
104
+ self.eval!
105
+ end
106
+ end
107
+ end
108
+
109
+
110
+ ##
111
+ # Evals the stack.
112
+ #
113
+
114
+ protected
115
+ def eval!
116
+ @syncing = true
117
+ @stack.each { |b| b.call() }
118
+ @stack.clear()
119
+ @syncing = false
120
+ end
121
+
122
+ end
123
+
data/test ADDED
@@ -0,0 +1,46 @@
1
+ #!/usr/bin/ruby
2
+ # encoding: utf-8
3
+ # (c) 2011 Martin Kozák (martinkozak@martinkozak.net)
4
+
5
+ $:.push("./lib")
6
+ $:.unshift("./lib")
7
+
8
+ require "callback-locker"
9
+ require "riot"
10
+
11
+ context "CallbackLocker" do
12
+ setup do
13
+ CallbackLocker::new
14
+ end
15
+
16
+ asserts("#initialize") do
17
+ not topic.locked? and topic.stack == [ ]
18
+ end
19
+ asserts("#lock, #locked?") do
20
+ topic.lock
21
+ topic.locked?
22
+ end
23
+ asserts("#unlock, #unlocked?") do
24
+ topic.unlock
25
+ topic.unlocked?
26
+ end
27
+ asserts("#synchronize") do
28
+ foo1 = nil
29
+ topic.synchronize do
30
+ foo1 = :a
31
+ end
32
+
33
+ foo2 = nil
34
+ topic.lock!
35
+ topic.synchronize do
36
+ foo2 = "1"
37
+ end
38
+ topic.synchronize do
39
+ foo2 << "2"
40
+ end
41
+ foo3 = foo2
42
+ topic.unlock!
43
+
44
+ foo1 == :a and foo2 == "12" and foo3.nil?
45
+ end
46
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: callback-locker
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Martin Poljak
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-07-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 1.0.0
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 1.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: jeweler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 1.5.2
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 1.5.2
41
+ - !ruby/object:Gem::Dependency
42
+ name: riot
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 0.12.3
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 0.12.3
55
+ description:
56
+ email: martin@poljak.net
57
+ executables: []
58
+ extensions: []
59
+ extra_rdoc_files:
60
+ - LICENSE.txt
61
+ - README.md
62
+ files:
63
+ - ".document"
64
+ - ".project"
65
+ - CHANGES.txt
66
+ - Gemfile
67
+ - Gemfile.lock
68
+ - LICENSE.txt
69
+ - README.md
70
+ - Rakefile
71
+ - VERSION
72
+ - callback-locker.gemspec
73
+ - lib/callback-locker.rb
74
+ - test
75
+ homepage: http://github.com/martinkozak/callback-locker
76
+ licenses:
77
+ - MIT
78
+ metadata: {}
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ requirements: []
94
+ rubyforge_project:
95
+ rubygems_version: 2.4.5
96
+ signing_key:
97
+ specification_version: 4
98
+ summary: Semaphore (mutex) like primitive for synchronizing callbacks.
99
+ test_files: []