call_logger 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 613db64e5b8ff52a92392341f19cc625aa28ced3f27c15a38da75096eadf5d48
4
- data.tar.gz: f459c45b112209952385bf5b5a2b554cb36743d894b8af784a34c128dac501cf
3
+ metadata.gz: 86ff0a5285a0155ffe5c9bf31253c7208ec6d4e22b32e04388bada594abd3726
4
+ data.tar.gz: 97c3aba959d1d3864d667213f4a6709e599669323abb453185e834982856cc60
5
5
  SHA512:
6
- metadata.gz: 335789e5630e506a0b3619714b5b94dbb67d94b8239c3208804cc070d0b1a5caf08178f4085e351110d2c84429fc9534bd929754757b37cedeb1cd0d923afcea
7
- data.tar.gz: f16262c8c53c6e572f7dcb2ca1db6d99a39ccaf781c77fb39d91abe3f66c0a9876376f862f3bebd5e6486b05109adc41aa7e3e187199d1c8be8a853169512634
6
+ metadata.gz: 8781225032feeb50ecef93f2e1e2753b16762fe3ea50f3da1ad01189ab0e7e23932fa8b2abf8c483bf3bcdbf2143709979bdedf66f75284fb247be32f0e184a1
7
+ data.tar.gz: b0cca1e74771fc882fc4645b691bbbbdd8f53e2e6a799ac59e81ebbc02e537214f08269df2782f9b7f1804c62400bf3d7cb6bf10fd38d445ee26b96305fa7791
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- call_logger (0.1.0)
4
+ call_logger (0.2.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # CallLogger
2
2
 
3
- A debugging tool that let you log method usage.
3
+ A debugging tool that lets you log method usage.
4
4
 
5
5
  ```
6
6
  class Calculator
@@ -9,12 +9,21 @@ class Calculator
9
9
  log def times(a, b)
10
10
  a*b
11
11
  end
12
+
13
+ log def div(a, b)
14
+ a/b
15
+ end
12
16
  end
13
17
 
14
18
  Calculator.new.times(3,4)
15
- # times(2, 3)
19
+ # times(3, 4)
16
20
  # times => 6
17
21
  # => 7
22
+
23
+ Calculator.new.div(3,0)
24
+ # div(3, 0)
25
+ # div !! divided by 0
26
+ # ZeroDivisionError: divided by 0
18
27
  ```
19
28
 
20
29
  ## Installation
@@ -74,9 +83,10 @@ end
74
83
  ```
75
84
 
76
85
  * `Logger` should provide a `#call` method accepting a single paramter.
77
- * `Formatter` should provide two methods:
78
- * `#begin_message(method, args)` - accepting method name and it's arguments; called before method execution
79
- * `#end_message(method, result)` - accepting method name and it's result; called after method execution
86
+ * `Formatter` should provide following methods:
87
+ * `#before(method, args)` - accepting method name and it's arguments; called before method execution
88
+ * `#after(method, result)` - accepting method name and it's result; called after method execution
89
+ * `#error(method, exception)` - accepting method name and an exception; called when error is raised
80
90
 
81
91
  ## TODO
82
92
 
@@ -1,5 +1,5 @@
1
1
  module CallLogger
2
- class CallLogger
2
+ class MethodWrapper
3
3
  attr_reader :formatter, :logger
4
4
 
5
5
  def initialize(formatter:, logger: )
@@ -7,11 +7,14 @@ module CallLogger
7
7
  @logger = logger
8
8
  end
9
9
 
10
- def log(method, args)
11
- logger.call(formatter.begin_message(method, args))
10
+ def call(method, args)
11
+ logger.call(formatter.before(method, args))
12
12
  result = yield
13
- logger.call(formatter.end_message(method, result))
13
+ logger.call(formatter.after(method, result))
14
14
  result
15
+ rescue StandardError => e
16
+ logger.call(formatter.error(method, e))
17
+ raise
15
18
  end
16
19
  end
17
20
  end
@@ -1,12 +1,16 @@
1
1
  module CallLogger
2
2
  class Formatter
3
- def begin_message(method, args)
3
+ def before(method, args)
4
4
  "#{method}(#{args.join(', ')})"
5
5
  end
6
6
 
7
7
  # args?
8
- def end_message(method, result)
8
+ def after(method, result)
9
9
  "#{method} => #{result}"
10
10
  end
11
+
12
+ def error(method, exception)
13
+ "#{method} !! #{exception}"
14
+ end
11
15
  end
12
16
  end
@@ -1,3 +1,3 @@
1
1
  module CallLogger
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/call_logger.rb CHANGED
@@ -16,12 +16,17 @@ module CallLogger
16
16
 
17
17
  def self.configure
18
18
  self.configuration ||= Configuration.new
19
- yield(configuration)
19
+ yield(configuration) if block_given?
20
20
  end
21
+ configure # set defaults
21
22
 
22
23
  def log(method, args, &block)
23
- call_logger = ::CallLogger::CallLogger.new(logger: ::CallLogger.configuration.logger, formatter: ::CallLogger.configuration.formatter)
24
- call_logger.log(method, args, &block)
24
+ logger = ::CallLogger.configuration.logger
25
+ formatter = ::CallLogger.configuration.formatter
26
+ method_wrapper = ::CallLogger::MethodWrapper.new(
27
+ logger: logger, formatter: formatter
28
+ )
29
+ method_wrapper.call(method, args, &block)
25
30
  end
26
31
 
27
32
  module ClassMethods
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: call_logger
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Maciej Rzasa