class Gruf::Controllers::ServiceBinder
Binds gRPC services to a gruf controller
Public Class Methods
new(service)
click to toggle source
Initialize a service binder instance with the given service
@param [GRPC::GenericService] service The gRPC service stub to bind
# File lib/gruf/controllers/service_binder.rb, line 34 def initialize(service) @service = service end
Public Instance Methods
bind!(controller)
click to toggle source
Bind all methods on the service to the passed controller
@param [Class<Gruf::Controllers::Base>] controller
# File lib/gruf/controllers/service_binder.rb, line 43 def bind!(controller) rpc_methods.each { |name, desc| bind_method(controller, name, desc) } end
Private Instance Methods
bind_method(controller, method_name, desc)
click to toggle source
Bind the grpc methods to the service, allowing for server interception and execution control
@param [Gruf::Controllers::Base] controller @param [Symbol] method_name @param [BoundDesc] desc
# File lib/gruf/controllers/service_binder.rb, line 56 def bind_method(controller, method_name, desc) method_key = method_name.to_s.underscore.to_sym service_ref = @service @service.class_eval do if desc.request_response? define_method(method_key) do |message, active_call| c = controller.new( method_key: method_key, service: service_ref, message: message, active_call: active_call, rpc_desc: desc ) c.call(method_key) end elsif desc.client_streamer? define_method(method_key) do |active_call| c = controller.new( method_key: method_key, service: service_ref, message: proc { |&block| active_call.each_remote_read(&block) }, active_call: active_call, rpc_desc: desc ) c.call(method_key) end elsif desc.server_streamer? define_method(method_key) do |message, active_call, &block| c = controller.new( method_key: method_key, service: service_ref, message: message, active_call: active_call, rpc_desc: desc ) c.call(method_key, &block) end else # bidi define_method(method_key) do |messages, active_call, &block| c = controller.new( method_key: method_key, service: service_ref, message: messages, active_call: active_call, rpc_desc: desc ) c.call(method_key, &block) end end end end
rpc_methods()
click to toggle source
@return Array<Gruf::Controllers::ServiceBinder::BoundDesc>
# File lib/gruf/controllers/service_binder.rb, line 112 def rpc_methods @service.rpc_descs.map { |rd| BoundDesc.new(rd) } end