em_remote_call 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -10,35 +10,32 @@ module EM::RemoteCall
10
10
  define_method name do |*method_opts, &callb|
11
11
  return unless self.class.remote_connection
12
12
 
13
- if !block_given? && method_opts.last.is_a?(Proc)
14
- callb = method_opts.pop
15
- end
16
- if method_opts.last.is_a? Proc
17
- return_block = method_opts.pop
18
- end
19
- argument = method_opts.shift
13
+ callback = EM::RemoteCall::Deferrable.new
14
+ callback.callback(&callb) if callb
20
15
 
21
- remote_method = opts[:calls] || name
22
- klass = opts[:class_name] || self.class.to_s
23
- id = opts[:find_by] && send(opts[:find_by]) # when it's nil, it's considered a class method.
16
+ call = {
17
+ :argument => method_opts.first, # evt. other args will get dropped
18
+ :instance => {
19
+ :class => opts[:class_name] || self.class.to_s, # own class name by default
20
+ :id => opts[:find_by] && send(opts[:find_by]) # when it's nil, it's considered a class method.
21
+ },
22
+ :method => opts[:calls] || name, # same method name by default
23
+ :deferrable_id => callback.object_id # store the callbacks object_id to retrieve it later
24
+ }
24
25
 
25
- call = {:argument => argument, :instance => {:class => klass, :id => id}, :method => remote_method}
26
- self.class.remote_connection.call(call, callb, return_block)
26
+ self.class.remote_connection.call call
27
+ return callback
27
28
  end
28
29
  end
29
30
  end
30
31
 
31
- class EM::RemoteCall::Callback
32
+ class EM::RemoteCall::Deferrable
33
+ include EventMachine::Deferrable
32
34
  is_a_collection :object_id
33
35
 
34
- def initialize(&callb)
35
- @callback = callb
36
- super
37
- end
38
-
39
- def call(arg)
40
- @callback.call arg
36
+ def set_deferred_status status, *args
41
37
  remove_from_collection
38
+ super
42
39
  end
43
40
  end
44
41
 
@@ -46,22 +43,13 @@ module EM::RemoteCall::Client
46
43
  include EM::JsonConnection::Client
47
44
 
48
45
  def json_parsed(hash)
49
- if id = hash[:callback_id]
50
- if callb = EM::RemoteCall::Callback.find(id)
51
- callb.call hash[:argument]
52
- end
53
- end
46
+ id = hash[:deferrable_id]
47
+ deffr = EM::RemoteCall::Deferrable.find(id)
48
+ deffr.succeed hash[:success] if hash.has_key? :success
49
+ deffr.fail hash[:error] if hash.has_key? :error
54
50
  end
55
51
 
56
- def call(call, callb, return_block)
57
- if callb
58
- callb = EM::RemoteCall::Callback.new(&callb)
59
- call.merge!({ :callback_id => callb.object_id })
60
- end
61
- if return_block
62
- return_block = EM::RemoteCall::Callback.new(&return_block)
63
- call.merge!({ :return_block_id => return_block.object_id })
64
- end
52
+ def call(call)
65
53
  send_data call
66
54
  end
67
55
  end
@@ -1,30 +1,28 @@
1
1
  module EM::RemoteCall; end
2
2
 
3
3
  class EM::RemoteCall::Call
4
+ class Error < StandardError; end
5
+ class NoInstanceError < Error ; end
6
+ class NoMethodGivenError < Error; end
7
+ class NoMethodOfInstanceError < Error; end
8
+
4
9
  def initialize(instance_opts, method, argument)
5
- klass = instance_opts[:class] or return false
6
-
7
- @method, @argument = method, argument
8
-
9
- if indentifier = instance_opts[:id]
10
- klass = EM::RemoteCall::Utils.constantize(klass) or return false
11
- @instance = klass.find(indentifier)
12
- else
13
- @instance = EM::RemoteCall::Utils.constantize(klass) or return false
14
- end
15
-
10
+ @argument = argument
11
+ @method = method or raise NoMethodError.new method
12
+ @instance = find_instance(instance_opts) or raise NoInstanceError.new instance_opts
13
+ @instance.respond_to?(@method) or raise NoMethodOfInstanceError.new "#{@instance}##{method}"
16
14
  end
17
15
 
18
- def valid?
19
- @instance && @method && @instance.respond_to?(@method)
16
+ def find_instance(args)
17
+ if args[:id]
18
+ klass = EM::RemoteCall::Utils.constantize(args[:class]) or return false
19
+ @instance = klass.find(args[:id]) or return false
20
+ else
21
+ @instance = EM::RemoteCall::Utils.constantize(args[:class]) or return false
22
+ end
20
23
  end
21
24
 
22
25
  def call(&callb)
23
- unless valid?
24
- puts "invalid remote call: :instance => #{@instance}, :method => #{@method}, :argument => #{@argument}"
25
- return false
26
- end
27
-
28
26
  if @argument
29
27
  @instance.__send__ @method, @argument, &callb
30
28
  else
@@ -38,10 +36,13 @@ module EM::RemoteCall::Server
38
36
 
39
37
  def json_parsed(hash)
40
38
  remote_call = EM::RemoteCall::Call.new hash[:instance], hash[:method].to_sym, hash[:argument]
41
- return_value = remote_call.call do |blk_value|
42
- send_data({:callback_id => hash[:callback_id], :argument => blk_value}) if hash[:callback_id]
39
+
40
+ remote_call.call do |blk_value|
41
+ send_data({:deferrable_id => hash[:deferrable_id], :success => blk_value})
43
42
  end
44
- send_data({:callback_id => hash[:return_block_id], :argument => return_value}) if hash[:return_block_id]
45
43
 
44
+ rescue => e
45
+ puts "#{e}: #{e.message}"
46
+ send_data( { :deferrable_id => hash[:deferrable_id], :error => {:class => e.class.name, :message => e.message} } )
46
47
  end
47
48
  end
@@ -1,3 +1,3 @@
1
1
  module EmRemoteCall
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -5,7 +5,7 @@ require 'em_remote_call'
5
5
  class Track
6
6
  attr_reader :title, :artist
7
7
 
8
- def initialize(opts)
8
+ def initialize(opts={})
9
9
  @title, @artist = opts[:title], opts[:artist]
10
10
  super
11
11
  end
@@ -27,12 +27,16 @@ class ServerTrack < Track
27
27
  callb.call "finished #{id}"
28
28
  "started #{id}"
29
29
  end
30
+ def raise_hell
31
+ EM.next_tick{raise 'foobar'}
32
+ end
30
33
  end
31
34
 
32
35
  class ClientTrack < Track
33
36
  extend EM::RemoteCall
34
37
  remote_method :init_track_on_server, :class_name => 'ServerTrack', :calls => :new
35
38
  remote_method :play, :class_name => 'ServerTrack', :find_by => :id
39
+ remote_method :raise_hell, :class_name => 'ServerTrack', :find_by => :id
36
40
  end
37
41
 
38
42
  class EMController
@@ -63,7 +67,7 @@ describe EM::RemoteCall do
63
67
  test_on_client do
64
68
  callb = mock(:callb)
65
69
  callb.should_receive(:foo)
66
- ClientTrack.new({}).init_track_on_server({}){callb.foo}
70
+ ClientTrack.new.init_track_on_server({}){callb.foo}
67
71
  end
68
72
  end
69
73
  end
@@ -78,16 +82,16 @@ describe EM::RemoteCall do
78
82
  end
79
83
  end
80
84
  end
81
- describe "return values" do
82
- it "should be passed to the second proc" do
85
+ describe "errbacks" do
86
+ it "should take an errback method" do
83
87
  test_on_client do
84
88
  callb = mock(:callb)
85
- callb.should_receive(:bar).with('started a - b')
89
+ callb.should_receive(:foo).with({:class=>"RuntimeError", :message=>"foobar"})
86
90
  c = ClientTrack.new(:title => 'a', :artist => 'b')
87
91
  c.init_track_on_server(:title => 'a', :artist => 'b')
88
- c.play proc{|a| callb.bar a}, proc{}
92
+ play_call = c.raise_hell
93
+ play_call.errback{|a| callb.foo a}
89
94
  end
90
95
  end
91
96
  end
92
97
  end
93
-
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 3
9
- version: 0.0.3
8
+ - 4
9
+ version: 0.0.4
10
10
  platform: ruby
11
11
  authors:
12
12
  - Niko Dittmann
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-12-01 00:00:00 +01:00
17
+ date: 2010-12-22 00:00:00 +01:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency