verbose 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1e2bf7069e8349328cc1ff15e871233e462e21e6db4e9d8701178c7ff8fdc396
4
- data.tar.gz: 844b9594ec446dfec61416954691e4a21fcd662b5c5e9f96de891595d650087d
3
+ metadata.gz: a3c1132a2ab497379701fe1b67eb32e6c320c8e368b6e7fee89cd4e00f62f1f0
4
+ data.tar.gz: 07dadf0f32ac2c28bc428cca0236fc4e73f0b800dee4bbe56f86c62a4a7f5df0
5
5
  SHA512:
6
- metadata.gz: d0c9462b86d14c86725eae347bb2bd63f6b0a86cac04a3654611bcf12343c14978e509ea07c38f7364909f962eac3745c8d6859e4e7d12c1ee69559900916b05
7
- data.tar.gz: 5794a696441ccea364d26a4626abf5d93b8ec1212661b1d6866ff99a195dc34330771374f9941ffd4a1b76b88dabc55f5fb69430685fffd46a9bd11267c7d157
6
+ metadata.gz: 10f19a5ffc0a370c197b38ad20d8f63dd65c1c4f05a98cc9f898749e2ccb1e7cbafa60a0482ed13bff45b1250844bc90ad19665f343657f54a1643dc467050ee
7
+ data.tar.gz: 17cbd5f2d4251cbf99ccddf02eaac576abf5004d672f56098db04bcab56ff84d650b132b8fd678a206bc36f133c3ca64370b500892186515958a898f72cb2d92
data/lib/verbose.rb CHANGED
@@ -29,7 +29,7 @@ require 'tago'
29
29
  # Copyright:: Copyright (c) 2024 Yegor Bugayenko
30
30
  # License:: MIT
31
31
  class Verbose
32
- def initialize(origin, log: $stdout)
32
+ def initialize(origin, log: nil)
33
33
  @origin = origin
34
34
  @log = log
35
35
  end
@@ -37,28 +37,42 @@ class Verbose
37
37
  def method_missing(*args)
38
38
  start = Time.now
39
39
  mtd = args.shift
40
- begin
41
- result =
42
- if @origin.respond_to?(mtd)
43
- params = @origin.method(mtd).parameters
44
- reqs = params.count { |p| p[0] == :req }
45
- if params.any? { |p| p[0] == :key } && args.size > reqs
46
- @origin.__send__(mtd, *args[0...-1], **args.last) do |*a|
47
- yield(*a) if block_given?
48
- end
49
- else
50
- @origin.__send__(mtd, *args) do |*a|
51
- yield(*a) if block_given?
52
- end
53
- end
40
+ if @origin.respond_to?(mtd)
41
+ params = @origin.method(mtd).parameters
42
+ reqs = params.count { |p| p[0] == :req }
43
+ if params.any? { |p| p[0] == :key } && args.size > reqs
44
+ @origin.__send__(mtd, *args[0...-1], **args.last) do |*a|
45
+ yield(*a) if block_given?
46
+ end
47
+ else
48
+ @origin.__send__(mtd, *args) do |*a|
49
+ yield(*a) if block_given?
50
+ end
51
+ end
52
+ else
53
+ super
54
+ end
55
+ ensure
56
+ params = args.map do |a|
57
+ if a.is_a?(String)
58
+ max = 32
59
+ a = a.inspect
60
+ if a.length > max
61
+ "#{a[0..(max / 2) - 2]}...#{a[(max / 2) + 1..]}"
54
62
  else
55
- super
63
+ a
56
64
  end
57
- @log.debug("#{@origin.class}.#{mtd}(): finished in #{start.ago}")
58
- result
59
- rescue StandardError => e
60
- @log.debug("#{@origin.class}.#{mtd}(): thrown #{e.class} in #{start.ago}")
61
- raise e
65
+ elsif [Integer, Float, TrueClass, FalseClass].include?(a.class)
66
+ a
67
+ else
68
+ a.class
69
+ end
70
+ end
71
+ msg = "#{@origin.class}.#{mtd}(#{params.join(', ')}) in #{start.ago}"
72
+ if @log.respond_to?(:debug)
73
+ @log.debug(msg)
74
+ else
75
+ puts(msg)
62
76
  end
63
77
  end
64
78
 
data/test/test_verbose.rb CHANGED
@@ -38,7 +38,16 @@ class TestVerbose < Minitest::Test
38
38
  obj = Verbose.new(obj, log:)
39
39
  assert_equal(42, obj.read(42))
40
40
  assert_equal(42, obj.read(42))
41
- assert(log.to_s.include?('finished in'), log)
41
+ assert(log.to_s.include?('(42) in'), log)
42
+ end
43
+
44
+ def test_simple_to_stdout
45
+ obj = Object.new
46
+ def obj.read(foo)
47
+ foo
48
+ end
49
+ obj = Verbose.new(obj)
50
+ assert_equal(42, obj.read(42))
42
51
  end
43
52
 
44
53
  def test_works_with_optional_arguments
data/verbose.gemspec CHANGED
@@ -26,7 +26,7 @@ Gem::Specification.new do |s|
26
26
  s.required_rubygems_version = Gem::Requirement.new('>= 0') if s.respond_to? :required_rubygems_version=
27
27
  s.required_ruby_version = '>=3.0'
28
28
  s.name = 'verbose'
29
- s.version = '0.0.1'
29
+ s.version = '0.0.2'
30
30
  s.license = 'MIT'
31
31
  s.summary = 'A Decorator that Logs Every Method Call of a Decoratee'
32
32
  s.description =
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: verbose
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yegor Bugayenko