Class: Protocol::GRPC::Interface
- Inherits:
-
Object
- Object
- Protocol::GRPC::Interface
- Defined in:
- lib/protocol/grpc/interface.rb
Overview
Represents an interface definition for gRPC methods. Can be used by both client stubs and server implementations.
Defined Under Namespace
Classes: RPC
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
- #The service name (e.g., "hello.Greeter").(servicename(e.g., "hello.Greeter")) ⇒ Object readonly
Class Method Summary collapse
-
.inherited(subclass) ⇒ Object
Hook called when a subclass is created.
-
.lookup_rpc(name) ⇒ Object
Look up RPC definition for a method.
-
.pascal_case_to_snake_case(pascal_case) ⇒ Object
Convert PascalCase to snake_case.
-
.rpc(name, request_class = nil, response_class = nil, **options) ⇒ Object
Define an RPC method.
-
.rpcs ⇒ Object
Get all RPC definitions from this class and all parent classes.
-
.stream(message_class) ⇒ Object
Helper method to mark a message type as streamed in RPC definitions.
Instance Method Summary collapse
-
#initialize(name) ⇒ Interface
constructor
Initialize a new interface instance.
-
#path(method_name) ⇒ Object
Build gRPC path for a method.
Constructor Details
#initialize(name) ⇒ Interface
Initialize a new interface instance.
151 152 153 |
# File 'lib/protocol/grpc/interface.rb', line 151 def initialize(name) @name = name end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
156 157 158 |
# File 'lib/protocol/grpc/interface.rb', line 156 def name @name end |
#The service name (e.g., "hello.Greeter").(servicename(e.g., "hello.Greeter")) ⇒ Object (readonly)
156 |
# File 'lib/protocol/grpc/interface.rb', line 156 attr :name |
Class Method Details
.inherited(subclass) ⇒ Object
Hook called when a subclass is created. Initializes the RPC hash for the subclass.
64 65 66 67 68 |
# File 'lib/protocol/grpc/interface.rb', line 64 def self.inherited(subclass) super subclass.instance_variable_set(:@rpcs, {}) end |
.lookup_rpc(name) ⇒ Object
Look up RPC definition for a method. Looks up the inheritance chain to find the RPC definition.
117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/protocol/grpc/interface.rb', line 117 def self.lookup_rpc(name) klass = self while klass && klass != Interface if klass.instance_variable_defined?(:@rpcs) if rpc = klass.instance_variable_get(:@rpcs)[name] return rpc end end klass = klass.superclass end # Not found: return nil end |
.pascal_case_to_snake_case(pascal_case) ⇒ Object
Convert PascalCase to snake_case.
170 171 172 173 174 175 |
# File 'lib/protocol/grpc/interface.rb', line 170 def self.pascal_case_to_snake_case(pascal_case) pascal_case .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2') # Insert underscore before capital letters followed by lowercase .gsub(/([a-z\d])([A-Z])/, '\1_\2') # Insert underscore between lowercase/digit and uppercase .downcase end |
.rpc(name, request_class = nil, response_class = nil, **options) ⇒ Object
Define an RPC method.
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/protocol/grpc/interface.rb', line 83 def self.rpc(name, request_class = nil, response_class = nil, **) [:name] = name # Check if request or response are wrapped with stream() request_streaming = request_class.is_a?(Streaming) response_streaming = response_class.is_a?(Streaming) # Unwrap StreamWrapper if present [:request_class] ||= request_streaming ? request_class. : request_class [:response_class] ||= response_streaming ? response_class. : response_class # Auto-detect streaming type from stream() decorators if not explicitly set if !.key?(:streaming) if request_streaming && response_streaming [:streaming] = :bidirectional elsif request_streaming [:streaming] = :client_streaming elsif response_streaming [:streaming] = :server_streaming else [:streaming] = :unary end end # Ensure snake_case method name is always available [:method] ||= pascal_case_to_snake_case(name.to_s).to_sym @rpcs[name] = RPC.new(**) end |
.rpcs ⇒ Object
Get all RPC definitions from this class and all parent classes.
134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/protocol/grpc/interface.rb', line 134 def self.rpcs all_rpcs = {} klass = self # Walk up the inheritance chain: while klass && klass != Interface if klass.instance_variable_defined?(:@rpcs) all_rpcs.merge!(klass.instance_variable_get(:@rpcs)) end klass = klass.superclass end all_rpcs end |
Instance Method Details
#path(method_name) ⇒ Object
Build gRPC path for a method.
161 162 163 |
# File 'lib/protocol/grpc/interface.rb', line 161 def path(method_name) Methods.build_path(@name, method_name.to_s) end |