Class: Informed::Informant
- Inherits:
-
Object
- Object
- Informed::Informant
- Defined in:
- lib/informed.rb
Overview
Informs on method calls!
Defined Under Namespace
Classes: DoneMessage, Message, StartingMessage
Instance Attribute Summary collapse
-
#also_log ⇒ Hash
What, if any, additional data to log when informing on a method.
-
#level ⇒ :debug, ...
Which level to log informed method calls at.
-
#method ⇒ Symbol
The name of the method being informed on.
Instance Method Summary collapse
-
#inform_on(logger:, arguments:, keyword_arguments:, informee:, block: nil) ⇒ Object
The result of the informed upon method.
-
#initialize(method:, also_log:, level:) ⇒ Informant
constructor
A new instance of Informant.
Constructor Details
#initialize(method:, also_log:, level:) ⇒ Informant
Returns a new instance of Informant.
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_log ⇒ Hash
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.
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
108 109 110 |
# File 'lib/informed.rb', line 108 def level @level end |
#method ⇒ Symbol
The name of the method being informed on. This is included in all log messages.
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.
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 |