callback-adapter 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/Gemfile +11 -0
- data/Gemfile.lock +20 -0
- data/LICENSE.txt +20 -0
- data/README.md +42 -0
- data/Rakefile +29 -0
- data/VERSION +1 -0
- data/lib/callback-adapter.rb +58 -0
- data/test +23 -0
- metadata +98 -0
data/.document
ADDED
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
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
git (1.2.5)
|
5
|
+
jeweler (1.6.4)
|
6
|
+
bundler (~> 1.0)
|
7
|
+
git (>= 1.2.5)
|
8
|
+
rake
|
9
|
+
rake (0.9.2)
|
10
|
+
riot (0.12.4)
|
11
|
+
rr
|
12
|
+
rr (1.0.3)
|
13
|
+
|
14
|
+
PLATFORMS
|
15
|
+
ruby
|
16
|
+
|
17
|
+
DEPENDENCIES
|
18
|
+
bundler (>= 1.0.0)
|
19
|
+
jeweler (>= 1.5.2)
|
20
|
+
riot (>= 0.12.3)
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Martin Kozák (martinkozak@martinkozak.net)
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
Callback Adapter
|
2
|
+
================
|
3
|
+
|
4
|
+
**callback-adapter** adapts a callback backed interface to an standard
|
5
|
+
one, so allows safe thread-synchronized usage of evented or
|
6
|
+
multithreaded libraries like [EventMachine][8] in standard applications
|
7
|
+
without necessary paradigm shift.
|
8
|
+
|
9
|
+
Some trivial example:
|
10
|
+
|
11
|
+
class Foo
|
12
|
+
def bar(arg, &callback)
|
13
|
+
callback.call(arg)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
obj = CallbackAdapter::new(Foo::new)
|
18
|
+
obj.bar("Some argument!") # will return the "Some argument!" string back again
|
19
|
+
|
20
|
+
In case, more return values are given to callback, returns an array
|
21
|
+
of them. Internally, it's thread synchronized, so safe.
|
22
|
+
|
23
|
+
Contributing
|
24
|
+
------------
|
25
|
+
|
26
|
+
1. Fork it.
|
27
|
+
2. Create a branch (`git checkout -b 20101220-my-change`).
|
28
|
+
3. Commit your changes (`git commit -am "Added something"`).
|
29
|
+
4. Push to the branch (`git push origin 20101220-my-change`).
|
30
|
+
5. Create an [Issue][9] with a link to your branch.
|
31
|
+
6. Enjoy a refreshing Diet Coke and wait.
|
32
|
+
|
33
|
+
|
34
|
+
Copyright
|
35
|
+
---------
|
36
|
+
|
37
|
+
Copyright © 2011 [Martin Kozák][10]. See `LICENSE.txt` for
|
38
|
+
further details.
|
39
|
+
|
40
|
+
[8]: http://rubyeventmachine.com/
|
41
|
+
[9]: http://github.com/martinkozak/callback-adapter/issues
|
42
|
+
[10]: http://www.martinkozak.net/
|
data/Rakefile
ADDED
@@ -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-adapter"
|
19
|
+
gem.homepage = "http://github.com/martinkozak/callback-adapter"
|
20
|
+
gem.license = "MIT"
|
21
|
+
gem.summary = 'Adapts a callback backed interface to an standard one, so allows safe thread-synchronized usage of evented or multithreaded libraries like EventMachine in standard applications without necessary paradigm shift.'
|
22
|
+
gem.email = "martinkozak@martinkozak.net"
|
23
|
+
gem.authors = ["Martin Kozák"]
|
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,58 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
# (c) 2011 Martin Kozák (martinkozak@martinkozak.net)
|
3
|
+
|
4
|
+
##
|
5
|
+
# Adapter from an callback-based interface to standard interface.
|
6
|
+
#
|
7
|
+
|
8
|
+
class CallbackAdapter
|
9
|
+
|
10
|
+
##
|
11
|
+
# Holds the proxied object.
|
12
|
+
# @return [Object] the proxied object
|
13
|
+
#
|
14
|
+
|
15
|
+
attr_accessor :object
|
16
|
+
@object
|
17
|
+
|
18
|
+
##
|
19
|
+
# Constructor.
|
20
|
+
# @param [Object] object some proxied object
|
21
|
+
#
|
22
|
+
|
23
|
+
def initialize(object)
|
24
|
+
@object = object
|
25
|
+
end
|
26
|
+
|
27
|
+
##
|
28
|
+
# Methods call catcher. Thread safe.
|
29
|
+
#
|
30
|
+
# @param [Symbol] name name of the method
|
31
|
+
# @param [Array] args call arguments
|
32
|
+
# @return [Object] returned value or array of returned values
|
33
|
+
#
|
34
|
+
|
35
|
+
def method_missing(name, *args)
|
36
|
+
returns = nil
|
37
|
+
mutex = Mutex::new.lock
|
38
|
+
|
39
|
+
@object.send(name, *args) do |*rets|
|
40
|
+
case rets.length
|
41
|
+
when 0
|
42
|
+
returns = nil
|
43
|
+
when 1
|
44
|
+
returns = rets.first
|
45
|
+
else
|
46
|
+
returns = rets
|
47
|
+
end
|
48
|
+
|
49
|
+
mutex.unlock()
|
50
|
+
end
|
51
|
+
|
52
|
+
mutex.synchronize do
|
53
|
+
return returns
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
|
data/test
ADDED
@@ -0,0 +1,23 @@
|
|
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-adapter"
|
9
|
+
require "riot"
|
10
|
+
|
11
|
+
|
12
|
+
class Foo
|
13
|
+
def bar(arg, &callback)
|
14
|
+
callback.call(arg)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
context "CallbackAdapter" do
|
19
|
+
asserts("#method_missing") do
|
20
|
+
obj = CallbackAdapter::new(Foo::new)
|
21
|
+
obj.bar(123) == 123
|
22
|
+
end
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: callback-adapter
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.1.0
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- "Martin Koz\xC3\xA1k"
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-07-27 00:00:00 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: bundler
|
17
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
18
|
+
none: false
|
19
|
+
requirements:
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 1.0.0
|
23
|
+
type: :development
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: *id001
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: jeweler
|
28
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
29
|
+
none: false
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 1.5.2
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: *id002
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: riot
|
39
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ">="
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: 0.12.3
|
45
|
+
type: :development
|
46
|
+
prerelease: false
|
47
|
+
version_requirements: *id003
|
48
|
+
description:
|
49
|
+
email: martinkozak@martinkozak.net
|
50
|
+
executables: []
|
51
|
+
|
52
|
+
extensions: []
|
53
|
+
|
54
|
+
extra_rdoc_files:
|
55
|
+
- LICENSE.txt
|
56
|
+
- README.md
|
57
|
+
files:
|
58
|
+
- .document
|
59
|
+
- Gemfile
|
60
|
+
- Gemfile.lock
|
61
|
+
- LICENSE.txt
|
62
|
+
- README.md
|
63
|
+
- Rakefile
|
64
|
+
- VERSION
|
65
|
+
- lib/callback-adapter.rb
|
66
|
+
- test
|
67
|
+
homepage: http://github.com/martinkozak/callback-adapter
|
68
|
+
licenses:
|
69
|
+
- MIT
|
70
|
+
post_install_message:
|
71
|
+
rdoc_options: []
|
72
|
+
|
73
|
+
require_paths:
|
74
|
+
- lib
|
75
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
76
|
+
none: false
|
77
|
+
requirements:
|
78
|
+
- - ">="
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
hash: 3025330182367464277
|
81
|
+
segments:
|
82
|
+
- 0
|
83
|
+
version: "0"
|
84
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
85
|
+
none: false
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: "0"
|
90
|
+
requirements: []
|
91
|
+
|
92
|
+
rubyforge_project:
|
93
|
+
rubygems_version: 1.8.5
|
94
|
+
signing_key:
|
95
|
+
specification_version: 3
|
96
|
+
summary: Adapts a callback backed interface to an standard one, so allows safe thread-synchronized usage of evented or multithreaded libraries like EventMachine in standard applications without necessary paradigm shift.
|
97
|
+
test_files: []
|
98
|
+
|