Class: Gruf::Controllers::ServiceBinder

Inherits:
Object
  • Object
show all
Defined in:
lib/gruf/controllers/service_binder.rb

Overview

Binds gRPC services to a gruf controller

Defined Under Namespace

Classes: BoundDesc

Class Method Summary collapse

Class Method Details

.bind!(service:, controller:) ⇒ Object

Bind all methods on the service to the passed controller

Parameters:



35
36
37
38
# File 'lib/gruf/controllers/service_binder.rb', line 35

def bind!(service:, controller:)
  rpc_methods = service.rpc_descs.map { |rd| BoundDesc.new(rd) }
  rpc_methods.each { |name, desc| bind_method(service, controller, name, desc) }
end

.bind_method(service_ref, controller, method_name, desc) ⇒ Object

Bind the grpc methods to the service, allowing for server interception and execution control

Parameters:



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/gruf/controllers/service_binder.rb', line 47

def bind_method(service_ref, controller, method_name, desc)
  method_key = method_name.to_s.underscore.to_sym
  controller_name = controller.name
  service_ref.class_eval do
    if desc.request_response?
      define_method(method_key) do |message, active_call|
        Gruf::Autoloaders.controllers.with_fresh_controller(controller_name) do |fresh_controller|
          c = fresh_controller.new(
            method_key: method_key,
            service: service_ref,
            message: message,
            active_call: active_call,
            rpc_desc: desc
          )
          c.call(method_key)
        end
      end
    elsif desc.client_streamer?
      define_method(method_key) do |active_call|
        Gruf::Autoloaders.controllers.with_fresh_controller(controller_name) do |fresh_controller|
          c = fresh_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
      end
    elsif desc.server_streamer?
      define_method(method_key) do |message, active_call, &block|
        Gruf::Autoloaders.controllers.with_fresh_controller(controller_name) do |fresh_controller|
          c = fresh_controller.new(
            method_key: method_key,
            service: service_ref,
            message: message,
            active_call: active_call,
            rpc_desc: desc
          )
          c.call(method_key, &block)
        end
      end
    else # bidi
      define_method(method_key) do |messages, active_call, &block|
        Gruf::Autoloaders.controllers.with_fresh_controller(controller_name) do |fresh_controller|
          c = fresh_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
end