pipe_rpc 2.2.3 → 2.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f11c770e36c36885a8ca8a81401aac2cec26469c
4
- data.tar.gz: 3a5f50f02c861b2b734f1c6d4e98930968e3cd0c
3
+ metadata.gz: 0358adf8660892a8dcd7f7fa2cdc00fa7aec1e7c
4
+ data.tar.gz: 260d9f45bd8c159d9a27c5a0f8630da1edc1ad15
5
5
  SHA512:
6
- metadata.gz: a43880de70e696db2ca33bf80b27c3fd896bdc61f3611497b948453925ed8046b923d933b338c2e2242eda36252b2a2c72bd4b5e40ea122a874ea2c0412332ea
7
- data.tar.gz: d378cb4fa296eac256178cddc280f5805c2f5e7a78e71395af5194afbe57d0e2c6570cb2dff6c54dd734b22275128072f0dd404c83d1df3d94876b9c61d80de6
6
+ metadata.gz: eeb67540050b3e85b54f21e0c33e2cc0880ef9b77bafe2aaa0c056b4691dc083087a212e015fe44237d4ba5b709c8f493c013c8e676196a3c606a848b3f4b8ae
7
+ data.tar.gz: 7ee01e84a9ae90ac0c4f86820345a9a7c1ea2aab5a5dd1dcb3426649f5a9283c811b4f9b2cdf79098d20ce18e5fac6c376169c0425cf95a57c4df5a3fb1e6779
@@ -1,13 +1,13 @@
1
1
  module PipeRpc
2
2
  class Client < BasicInterface
3
+ TRANSPORT_PREFIX = "__rpc_client__"
4
+
3
5
  def initialize(server_id = :default, hub)
4
- ::Kernel.raise ClientInitializationError.new("server id #{server_id.inspect} no symbol") unless server_id.is_a? ::Symbol
6
+ Kernel.raise ClientInitializationError.new("server id #{server_id.inspect} no symbol") unless server_id.is_a? Symbol
5
7
  @__rpc_server_id__ = server_id
6
8
  @hub = hub
7
9
  end
8
10
 
9
- attr_reader :__rpc_server_id__
10
-
11
11
  def respond_to?(method)
12
12
  super or method_missing(:respond_to?, method)
13
13
  end
@@ -16,5 +16,23 @@ module PipeRpc
16
16
  request = Client::Request.new(@hub, server: @__rpc_server_id__, method: method, arguments: args)
17
17
  @hub.requests.evaluate(request)
18
18
  end
19
+
20
+ def to_rpc_transport(hub = nil)
21
+ "#{TRANSPORT_PREFIX}#{@__rpc_server_id__}"
22
+ end
23
+ end
24
+
25
+ class << Client
26
+ def const_missing(name)
27
+ ::Object.const_get(name)
28
+ end
29
+
30
+ def from_rpc_transport?(transport)
31
+ (transport.is_a? String) and (transport.start_with? Hub::Server::TRANSPORT_PREFIX)
32
+ end
33
+
34
+ def from_rpc_transport(hub, transport)
35
+ hub.clients[transport.sub(Hub::Server::TRANSPORT_PREFIX, '').to_sym]
36
+ end
19
37
  end
20
38
  end
@@ -4,7 +4,7 @@ module PipeRpc
4
4
  class Client::Request < Request
5
5
  def initialize(hub, request)
6
6
  @hub = hub
7
- request[:arguments] = DeepMapper.to_transport(request[:arguments], hub)
7
+ request[:arguments] = TransportMapper.to_transport(request[:arguments], hub)
8
8
  super request
9
9
  # discard first three entries mentioning Client::Request#initialize,
10
10
  # Client::Request.new and Client#method_missing in stacktrace
@@ -18,7 +18,7 @@ module PipeRpc
18
18
  @value.set_backtrace(@value.backtrace.to_a + @request.stacktrace) if @value.respond_to? :set_backtrace
19
19
  raise @value
20
20
  else
21
- DeepMapper.from_transport(@value, @request.hub)
21
+ TransportMapper.from_transport(@value, @request.hub)
22
22
  end
23
23
  end
24
24
  end
@@ -12,7 +12,7 @@ module PipeRpc
12
12
  def handle
13
13
  evaluate_result do
14
14
  # the block is passed for asynchronous evaluation
15
- mapped_arguments = DeepMapper.from_transport(arguments, @hub)
15
+ mapped_arguments = TransportMapper.from_transport(arguments, @hub)
16
16
  @hub.servers[server].__send__(method, *mapped_arguments, &evaluate_result_proc)
17
17
  end
18
18
  end
@@ -5,7 +5,7 @@ module PipeRpc
5
5
  end
6
6
 
7
7
  def value
8
- DeepMapper.to_transport(@result, @request.hub)
8
+ TransportMapper.to_transport(@result, @request.hub)
9
9
  end
10
10
 
11
11
  def to_response
@@ -1,5 +1,7 @@
1
1
  module PipeRpc
2
2
  class Hub::Server < BasicInterface
3
+ TRANSPORT_PREFIX = "__rpc_server__"
4
+
3
5
  kernel = ::Kernel.dup
4
6
  kernel.class_eval do
5
7
  alias_method :__rpc_server_class__, :class
@@ -8,14 +10,31 @@ module PipeRpc
8
10
  end
9
11
  include kernel
10
12
 
11
- SERVER_ID_GETTER = :__id__
13
+ def to_rpc_server_id
14
+ "#{__rpc_server_class__}##{__id__}".to_sym
15
+ end
16
+
17
+ def to_rpc_transport(hub)
18
+ hub.servers.add(self) unless hub.servers.registered? to_rpc_server_id
19
+ "#{TRANSPORT_PREFIX}#{to_rpc_server_id}"
20
+ end
21
+ end
12
22
 
13
- def self.const_missing(name)
23
+ class << Hub::Server
24
+ def const_missing(name)
14
25
  ::Object.const_get(name)
15
26
  end
16
27
 
17
- def to_rpc_server_id
18
- "#{__rpc_server_class__.to_s}##{__send__(__rpc_server_class__::SERVER_ID_GETTER)}".to_sym
28
+ def instance_eval_for(server, *args, &block)
29
+ Object.instance_method(:instance_eval).bind(server).call(*args, &block)
30
+ end
31
+
32
+ def from_rpc_transport?(transport)
33
+ (transport.is_a? String) and (transport.start_with? Client::TRANSPORT_PREFIX)
34
+ end
35
+
36
+ def from_rpc_transport(hub, transport)
37
+ hub.servers[transport.sub(Client::TRANSPORT_PREFIX, '').to_sym]
19
38
  end
20
39
  end
21
40
  end
@@ -0,0 +1,39 @@
1
+ module PipeRpc
2
+ class TransportMapper
3
+ def self.map(object, &mapper)
4
+ if Client === object
5
+ # first filter out clients so for them subsequent checks are not sent
6
+ # through the pipe
7
+ yield object
8
+ elsif Hash === object
9
+ object.map{ |k, v| [k, map(v, &mapper)] }.to_h
10
+ elsif object.respond_to? :map
11
+ object.map{ |item| map(item, &mapper) }
12
+ else
13
+ yield object
14
+ end
15
+ end
16
+
17
+ def self.to_transport(object, hub)
18
+ map(object) do |value|
19
+ if value.respond_to? :to_rpc_transport
20
+ value.to_rpc_transport(hub)
21
+ else
22
+ value
23
+ end
24
+ end
25
+ end
26
+
27
+ def self.from_transport(object, hub)
28
+ map(object) do |value|
29
+ if Hub::Server.from_rpc_transport? value
30
+ Hub::Server.from_rpc_transport(hub, value)
31
+ elsif Client.from_rpc_transport? value
32
+ Client.from_rpc_transport(hub, value)
33
+ else
34
+ value
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -1,3 +1,3 @@
1
1
  module PipeRpc
2
- VERSION = "2.2.3"
2
+ VERSION = "2.3.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pipe_rpc
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.3
4
+ version: 2.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Christopher Aue
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-09-21 00:00:00.000000000 Z
11
+ date: 2016-09-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: msgpack
@@ -105,7 +105,6 @@ files:
105
105
  - lib/pipe_rpc/client_request_response.rb
106
106
  - lib/pipe_rpc/client_request_result.rb
107
107
  - lib/pipe_rpc/client_request_result_response.rb
108
- - lib/pipe_rpc/deep_mapper.rb
109
108
  - lib/pipe_rpc/error.rb
110
109
  - lib/pipe_rpc/error_response.rb
111
110
  - lib/pipe_rpc/gateway.rb
@@ -123,6 +122,7 @@ files:
123
122
  - lib/pipe_rpc/hub_socket.rb
124
123
  - lib/pipe_rpc/request.rb
125
124
  - lib/pipe_rpc/result_response.rb
125
+ - lib/pipe_rpc/transport_mapper.rb
126
126
  - lib/pipe_rpc/version.rb
127
127
  - pipe_rpc.gemspec
128
128
  homepage: https://github.com/christopheraue/ruby-pipe_rpc
@@ -1,44 +0,0 @@
1
- module PipeRpc
2
- class DeepMapper
3
- def self.map(object, &mapper)
4
- if object.respond_to? :__rpc_server_id__
5
- # first filter out clients so for them subsequent checks are not sent
6
- # through the pipe
7
- yield object
8
- elsif Hash === object
9
- object.map{ |k, v| [k, map(v, &mapper)] }.to_h
10
- elsif object.respond_to? :map
11
- object.map{ |item| map(item, &mapper) }
12
- else
13
- yield object
14
- end
15
- end
16
-
17
- def self.to_transport(object, hub)
18
- map(object) do |value|
19
- if value.respond_to? :__rpc_server_id__
20
- # first filter out clients so for them subsequent checks are not sent
21
- # through the pipe
22
- "__rpc_client__#{value.__rpc_server_id__}"
23
- elsif value.respond_to? :to_rpc_server_id
24
- hub.servers.add(value) unless hub.servers.registered? value.to_rpc_server_id
25
- "__rpc_server__#{value.to_rpc_server_id}"
26
- else
27
- value
28
- end
29
- end
30
- end
31
-
32
- def self.from_transport(object, hub)
33
- map(object) do |value|
34
- if value.is_a?(String) and value.start_with?('__rpc_client__')
35
- hub.servers[value.sub('__rpc_client__', '').to_sym]
36
- elsif value.is_a?(String) and value.start_with?('__rpc_server__')
37
- Client.new(value.sub('__rpc_server__', '').to_sym, hub)
38
- else
39
- value
40
- end
41
- end
42
- end
43
- end
44
- end