Class: Informed::Informant

Inherits:
Object
  • Object
show all
Defined in:
lib/informed.rb

Overview

Informs on method calls!

Defined Under Namespace

Classes: DoneMessage, Message, StartingMessage

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method:, also_log:, level:) ⇒ Informant

Returns a new instance of Informant.

Parameters:



140
141
142
143
144
145
# File 'lib/informed.rb', line 140

def initialize(method:, also_log:, level:)
  self.level = level
  self.method = method
  # Somehow, nils are slipping in here...
  self.also_log = also_log || {}
end

Instance Attribute Details

#also_logHash

What, if any, additional data to log when informing on a method.

If this hash has ‘{ result: true }`, done messages will be logged with the result of the method.

If the hash has ‘{ values: [:a_method_name, :a_keyword_arg_name] }` the values of the method `a_method_name` and the passed in keyword argument `a_keyword_arg_name` will be logged as well.

Examples:

inform_on :a_method, level: :debug, also_log: { result: true,
                                                values: [:some_method, :some_keyword_arg] }
# This will log the result of `a_method` at the debug level, as well as
# the value of `some_method` and the passed in keyword argumented
# `some_keyword_arg`

inform_on :another_method, level: :info
# This will merely log that another_method was called at the info
# level, with no additional context.

Returns:

  • (Hash)


135
136
137
# File 'lib/informed.rb', line 135

def also_log
  @also_log
end

#level:debug, ...

Which level to log informed method calls at

Returns:

  • (:debug, :info, :warn, :error, :fatal, :unknown)


108
109
110
# File 'lib/informed.rb', line 108

def level
  @level
end

#methodSymbol

The name of the method being informed on. This is included in all log messages.

Returns:

  • (Symbol)


113
114
115
# File 'lib/informed.rb', line 113

def method
  @method
end

Instance Method Details

#inform_on(logger:, arguments:, keyword_arguments:, informee:, block: nil) ⇒ Object

Returns the result of the informed upon method.

Parameters:

  • informee (Object)

    The object that had the method called on it.

  • keyword_arguments (Hash)

    The keyword arguments passed into the method being informed on. These may be logged if specified in the :values array in #also_log.

Returns:

  • the result of the informed upon method.



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/informed.rb', line 152

def inform_on(logger:, arguments:, keyword_arguments:, informee:, block: nil)
  method_context = { keyword_arguments: keyword_arguments, method: method, also_log: also_log, informee: informee }
  log(logger: logger, type: StartingMessage, method_context: method_context)
  result = if arguments.empty? && keyword_arguments.empty?
             informee.send(:"unwatched_#{method}", &block)
           elsif arguments.empty? && !keyword_arguments.empty?
             informee.send(:"unwatched_#{method}", **keyword_arguments, &block)
           elsif !arguments.empty? && keyword_arguments.empty?
             informee.send(:"unwatched_#{method}", *arguments, &block)
           elsif !arguments.empty? && !keyword_arguments.empty?
             informee.send(:"unwatched_#{method}", *arguments, **keyword_arguments, &block)
           end
  log(logger: logger, type: DoneMessage, method_context: method_context.merge(result: result))
  result
end