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 +4 -4
- data/lib/verbose.rb +35 -21
- data/test/test_verbose.rb +10 -1
- data/verbose.gemspec +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a3c1132a2ab497379701fe1b67eb32e6c320c8e368b6e7fee89cd4e00f62f1f0
|
4
|
+
data.tar.gz: 07dadf0f32ac2c28bc428cca0236fc4e73f0b800dee4bbe56f86c62a4a7f5df0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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:
|
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
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
if
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
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
|
-
|
63
|
+
a
|
56
64
|
end
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
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?('
|
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.
|
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 =
|