Sha256: 6a7e9a0f48088b04701de33905b90c3c1122f800fb83994e64dde7bcf87a2cf3

Contents?: true

Size: 1.24 KB

Versions: 1

Compression:

Stored size: 1.24 KB

Contents

class PipeRpc::Request; end

module PipeRpc
  class Client::Request < Request
    def initialize(hub, request)
      @hub = hub
      request[:arguments] = arguments_to_server_ids(request[:arguments])
      super request
      # discard first three entries mentioning Client::Request#initialize,
      # Client::Request.new and Client#method_missing in stacktrace
      @stacktrace = Kernel.respond_to?(:caller) ? Kernel.caller(3).to_a : []
      @result = Result.new(self)
    end

    attr_reader :hub, :stacktrace, :result

    def id=(id)
      raise 'id already set' if @id
      @id = id
    end

    def id
      @id or raise 'id not set'
    end

    def arguments_to_server_ids(arguments)
      deep_map(arguments) do |value|
        if value.respond_to? :__rpc_server_id__ # is a client
          value.__rpc_server_id__
        elsif value.respond_to? :to_rpc_server_id # is a servable
          @hub.servers.add(value)
          value.to_rpc_server_id
        else
          value
        end
      end
    end

    def deep_map(object, &mapper)
      case object
      when Hash then object.map{ |k, v| [k, deep_map(v, &mapper)] }.to_h
      when Array then object.map{ |item| deep_map(item, &mapper) }
      else yield object
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pipe_rpc-2.1.0 lib/pipe_rpc/client_request.rb