Module: Messaging::Handle::Build

Defined in:
lib/messaging/handle.rb

Constant Summary collapse

Error =
Class.new(RuntimeError)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.configure?(instance, parameter_name) ⇒ Boolean

Returns:

  • (Boolean)

Raises:



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/messaging/handle.rb', line 57

def self.configure?(instance, parameter_name)
  configure_method = instance.method(:configure)

  parameter_type, _ = configure_method.parameters.find do |type, name|
    name == parameter_name
  end

  return false if parameter_type.nil?

  return true if [:key, :keyreq].include?(parameter_type)

  error_message = "Incorrect definition of handler's configure method. The #{parameter_name} parameter must be a keyword argument (Handler: #{instance.class}, Parameter Type: #{parameter_type.inspect})"
  handler_logger.error(tag: :handle) { error_message }
  raise Error, error_message
end

.handler_loggerObject



73
74
75
# File 'lib/messaging/handle.rb', line 73

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

.send_session?(session, instance) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/messaging/handle.rb', line 49

def self.send_session?(session, instance)
  configure?(instance, :session) && !session.nil?
end

.send_settings?(settings, instance) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/messaging/handle.rb', line 53

def self.send_settings?(settings, instance)
  configure?(instance, :settings) && !settings.nil?
end

Instance Method Details

#build(strict: nil, session: nil, settings: nil) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/messaging/handle.rb', line 30

def build(strict: nil, session: nil, settings: nil)
  instance = new
  instance.strict = strict

  arguments = {}

  if Build.send_session?(session, instance)
    arguments[:session] = session
  end

  if Build.send_settings?(settings, instance)
    arguments[:settings] = settings
  end

  instance.configure(**arguments)

  instance
end