Module: Messaging::Handle

Defined Under Namespace

Modules: Build, Call, Defaults, HandleMacro, Info, MessageRegistry Classes: Error

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(cls) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/messaging/handle.rb', line 5

def self.included(cls)
  cls.class_exec do
    include Dependency
    include TemplateMethod

    def handler_logger
      @handler_logger ||= Log.get(self)
    end
    attr_writer :handler_logger

    extend Build
    extend Call
    extend Info
    extend HandleMacro
    extend MessageRegistry

    template_method :configure

    attr_writer :strict
  end
end

Instance Method Details

#call(message_or_message_data, strict: nil) ⇒ Object



168
169
170
171
172
173
174
# File 'lib/messaging/handle.rb', line 168

def call(message_or_message_data, strict: nil)
  if message_or_message_data.is_a? Message
    handle_message(message_or_message_data, strict: strict)
  else
    handle_message_data(message_or_message_data, strict: strict)
  end
end

#handle_message(message, strict: nil) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/messaging/handle.rb', line 176

def handle_message(message, strict: nil)
  strict ||= self.strict?

  handler_logger.trace(tags: [:dispatch, :message]) { "Dispatching message (Message class: #{message.class.name})" }
  handler_logger.trace(tags: [:data, :message]) { message.pretty_inspect }

  handler = self.class.handler(message)

  unless handler.nil?
    message_type = message.message_type

    handler_logger.trace(tag: [:handle, :message]) { "Handling Message (Type: #{message_type}, Method: #{handler})" }

    public_send(handler, message)

    handler_logger.info(tags: [:handle, :message]) { "Handled message (Message class: #{message.class.name})" }
    handler_logger.info(tags: [:data, :message]) { message.pretty_inspect }
  else
    if strict
      error_msg = "#{self.class.name} does not implement a handler for #{message.message_type}. Cannot handle the message."
      handler_logger.error(tag: :handle) { error_msg }
      raise Error, error_msg
    end
  end

  message
end

#handle_message_data(message_data, strict: nil) ⇒ Object



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/messaging/handle.rb', line 204

def handle_message_data(message_data, strict: nil)
  strict ||= self.strict?

  handler_logger.trace(tags: [:dispatch, :message_data]) { "Dispatching message data (Type: #{message_data.type})" }
  handler_logger.trace(tags: [:data, :message_data]) { message_data.pretty_inspect }

  message = nil

  handler = self.class.handler(message_data)

  unless handler.nil?
    message_type = message_data.type
    message_name = Messaging::Message::Info.canonize_name(message_type)
    message_class = self.class.message_registry.get(message_name)

    if message_class == nil
      error_msg = "No message class is registered (Message Type: #{message_type}, Handler: #{self.class.name})"
      handler_logger.error(tag: :handle) { error_msg }
      raise Error, error_msg
    end

    message = Message::Import.(message_data, message_class)

    message_type = message.message_type

    handler_logger.trace(tag: [:handle, :message_data]) { "Handling Message Data (Type: #{message_type}, Method: #{handler})" }

    public_send(handler, message)

    handler_logger.info(tags: [:handle, :message_data]) { "Handled message data (Type: #{message_data.type})" }
    handler_logger.info(tags: [:data, :message_data]) { message_data.pretty_inspect }
  else
    if respond_to?(:handle)
      message_type = message_data.type
      handler_logger.trace(tag: [:handle, :message_data]) { "Handling Message Data (Type: #{message_type}, Method: handle" }

      handle(message_data)

      handler_logger.info(tags: [:handle, :message_data]) { "Handled message data (Type: #{message_data.type})" }
      handler_logger.info(tags: [:data, :message_data]) { message_data.pretty_inspect }

      message = message_data
    else
      if strict
        error_msg = "#{self.class.name} does not implement `handle'. Cannot handle message data."
        handler_logger.error(tag: :handle) { error_msg }
        raise Error, error_msg
      end
    end
  end

  message
end

#strictObject Also known as: strict?

Can’t null-coalesce here. It’s a boolean. Use expanded form of ‘if` block - Scott, Mon Oct 16 2023



163
164
165
# File 'lib/messaging/handle.rb', line 163

def strict
  @strict ||= Defaults.strict
end