object_inspector 0.5.2 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +5 -0
- data/.travis.yml +4 -5
- data/CHANGELOG.md +4 -0
- data/Gemfile.lock +3 -3
- data/README.md +61 -6
- data/bin/console +0 -1
- data/lib/object_inspector/formatters/base_formatter.rb +7 -8
- data/lib/object_inspector/inspector.rb +47 -42
- data/lib/object_inspector/object_interrogator.rb +10 -12
- data/lib/object_inspector/scope.rb +2 -3
- data/lib/object_inspector/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c5bfaac0270b232c17c91495180cf29e5a4538c1010e636cf681832d6668ba8c
|
4
|
+
data.tar.gz: fd5e4add5c7a5c7d971ec22a4d5ba80e78642d4df9e060c6382b6b7a93e67551
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0b31e59f84e625d766309ba1dd760a87bba5b70007214aeec3dd3f47c44e64446db34c640a5a9c35923881178eb61c98080138d542ba1fe1b632a93d1269b4ee
|
7
|
+
data.tar.gz: 777ee932d2e9fa0c0d4c013dd9f0c8713956afafd883ce866fccb93f23b25fa8eaddb729b6035fd74c514bf9865f40bca09cb06fc2153a4f927458b7dbdbad45
|
data/.rubocop.yml
CHANGED
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
object_inspector (0.
|
4
|
+
object_inspector (0.6.0)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: https://rubygems.org/
|
@@ -35,8 +35,8 @@ GEM
|
|
35
35
|
builder
|
36
36
|
minitest (>= 5.0)
|
37
37
|
ruby-progressbar
|
38
|
-
object_identifier (0.
|
39
|
-
parallel (1.
|
38
|
+
object_identifier (0.2.1)
|
39
|
+
parallel (1.14.0)
|
40
40
|
parser (2.6.0.0)
|
41
41
|
ast (~> 2.4.0)
|
42
42
|
powerpack (0.1.2)
|
data/README.md
CHANGED
@@ -24,7 +24,7 @@ And then execute:
|
|
24
24
|
|
25
25
|
$ bundle
|
26
26
|
|
27
|
-
Or install it yourself
|
27
|
+
Or install it yourself:
|
28
28
|
|
29
29
|
$ gem install object_inspector
|
30
30
|
|
@@ -32,11 +32,10 @@ Or install it yourself as:
|
|
32
32
|
## Compatibility
|
33
33
|
|
34
34
|
Tested MRI Ruby Versions:
|
35
|
-
* 2.
|
36
|
-
* 2.
|
37
|
-
* 2.
|
38
|
-
* 2.
|
39
|
-
* 2.6.1
|
35
|
+
* 2.3
|
36
|
+
* 2.4
|
37
|
+
* 2.5
|
38
|
+
* 2.6
|
40
39
|
* edge
|
41
40
|
|
42
41
|
ObjectInspector has no other dependencies.
|
@@ -381,6 +380,62 @@ MyWrapperObject.new.inspect
|
|
381
380
|
This feature is recursive.
|
382
381
|
|
383
382
|
|
383
|
+
### Wrapped Delegators
|
384
|
+
|
385
|
+
If the Object being inspected is wrapped by an object that delegates all unknown methods to the wrapped object, then inspect flags will be doubled up. To get around this, redefine the `inspect` method in the Wrapper object e.g. like:
|
386
|
+
|
387
|
+
```ruby
|
388
|
+
class MyDelegatingWrapperObject
|
389
|
+
include ObjectInspector::InspectorsHelper
|
390
|
+
|
391
|
+
def initialize(my_object)
|
392
|
+
@my_object = my_object
|
393
|
+
end
|
394
|
+
|
395
|
+
def inspect(**kargs)
|
396
|
+
super(identification: self.class.name,
|
397
|
+
name: nil,
|
398
|
+
flags: nil,
|
399
|
+
info: nil,
|
400
|
+
issues: nil,
|
401
|
+
**kargs)
|
402
|
+
end
|
403
|
+
|
404
|
+
def to_model
|
405
|
+
@my_object
|
406
|
+
end
|
407
|
+
|
408
|
+
private
|
409
|
+
|
410
|
+
def method_missing(method_symbol, *args)
|
411
|
+
@my_object.__send__(method_symbol, *args)
|
412
|
+
end
|
413
|
+
|
414
|
+
def respond_to_missing?(*args)
|
415
|
+
@my_object.respond_to?(*args) || super
|
416
|
+
end
|
417
|
+
end
|
418
|
+
|
419
|
+
class MyWrappedObject
|
420
|
+
include ObjectInspector::InspectorsHelper
|
421
|
+
|
422
|
+
def display_name
|
423
|
+
"WRAPPED_OBJECT_NAME"
|
424
|
+
end
|
425
|
+
|
426
|
+
private
|
427
|
+
|
428
|
+
def inspect_flags; "FLAG1" end
|
429
|
+
def inspect_info; "INFO" end
|
430
|
+
def inspect_issues; "ISSUE1" end
|
431
|
+
def inspect_name; "NAME" end
|
432
|
+
end
|
433
|
+
|
434
|
+
MyDelegatingWrapperObject.new(MyWrappedObject.new).inspect
|
435
|
+
# => "<MyDelegatingWrapperObject> ⇨ <MyWrappedObject(FLAG1) !!ISSUE1!! INFO :: NAME>"
|
436
|
+
```
|
437
|
+
|
438
|
+
|
384
439
|
## On-the-fly Inspect Methods
|
385
440
|
|
386
441
|
When passed as an option (as opposed to being called via an Object-defined method) symbols will be called/evaluated on Object on the fly.
|
data/bin/console
CHANGED
@@ -9,7 +9,6 @@ require "ostruct"
|
|
9
9
|
# You can add fixtures and/or initialization code here to make experimenting
|
10
10
|
# with your gem easier. You can also use a different console, if you like.
|
11
11
|
|
12
|
-
# (If you use this, don't forget to add pry to your Gemfile!)
|
13
12
|
require "pry"
|
14
13
|
Pry.start
|
15
14
|
|
@@ -5,11 +5,10 @@ module ObjectInspector
|
|
5
5
|
# with {ObjectInspector::Inspector} objects to combine the supplied
|
6
6
|
# {#identification}, {#flags}, {#info}, and {#name} strings into a friendly
|
7
7
|
# "inspect" String.
|
8
|
-
#
|
9
|
-
# @attr inspector [ObjectInspector::Inspector]
|
10
8
|
class BaseFormatter
|
11
9
|
attr_reader :inspector
|
12
10
|
|
11
|
+
# @param inspector [ObjectInspector::Inspector]
|
13
12
|
def initialize(inspector)
|
14
13
|
@inspector = inspector
|
15
14
|
end
|
@@ -27,14 +26,14 @@ module ObjectInspector
|
|
27
26
|
# @return [NilClass] if not given
|
28
27
|
def wrapped_object_inspection_result
|
29
28
|
@wrapped_object_inspection_result ||=
|
30
|
-
inspector.wrapped_object_inspection_result
|
29
|
+
@inspector.wrapped_object_inspection_result
|
31
30
|
end
|
32
31
|
|
33
32
|
# Delegates to {Inspector#identification}.
|
34
33
|
#
|
35
34
|
# @return [String] if given
|
36
35
|
def identification
|
37
|
-
@identification ||= inspector.identification
|
36
|
+
@identification ||= @inspector.identification
|
38
37
|
end
|
39
38
|
|
40
39
|
# Delegates to {Inspector#flags}.
|
@@ -42,7 +41,7 @@ module ObjectInspector
|
|
42
41
|
# @return [String] if given
|
43
42
|
# @return [NilClass] if not given
|
44
43
|
def flags
|
45
|
-
@flags ||= inspector.flags
|
44
|
+
@flags ||= @inspector.flags
|
46
45
|
end
|
47
46
|
|
48
47
|
# Delegates to {Inspector#issues}.
|
@@ -50,7 +49,7 @@ module ObjectInspector
|
|
50
49
|
# @return [String] if given
|
51
50
|
# @return [NilClass] if not given
|
52
51
|
def issues
|
53
|
-
@issues ||= inspector.issues
|
52
|
+
@issues ||= @inspector.issues
|
54
53
|
end
|
55
54
|
|
56
55
|
# Delegates to {Inspector#info}.
|
@@ -58,7 +57,7 @@ module ObjectInspector
|
|
58
57
|
# @return [String] if given
|
59
58
|
# @return [NilClass] if not given
|
60
59
|
def info
|
61
|
-
@info ||= inspector.info
|
60
|
+
@info ||= @inspector.info
|
62
61
|
end
|
63
62
|
|
64
63
|
# Delegates to {Inspector#name}.
|
@@ -66,7 +65,7 @@ module ObjectInspector
|
|
66
65
|
# @return [String] if given
|
67
66
|
# @return [NilClass] if not given
|
68
67
|
def name
|
69
|
-
@name ||= inspector.name
|
68
|
+
@name ||= @inspector.name
|
70
69
|
end
|
71
70
|
end
|
72
71
|
end
|
@@ -1,24 +1,11 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module ObjectInspector
|
4
|
-
# ObjectInspector::Inspector organizes inspection of the associated {
|
4
|
+
# ObjectInspector::Inspector organizes inspection of the associated {@object}
|
5
5
|
# via the passed in options and via a {ObjectInspector::BaseFormatter}
|
6
6
|
# instance.
|
7
|
-
#
|
8
|
-
# @attr object [Object] the object being inspected
|
9
|
-
# @attr scope [Symbol] Object inspection type. For example:
|
10
|
-
# :self (default) -- Means: Only interrogate self. Don't visit neighbors.
|
11
|
-
# <custom> -- Anything else that makes sense for {#object} to key on
|
12
|
-
# @attr formatter [ObjectInspector::BaseFormatter]
|
13
|
-
# (ObjectInspector.configuration.formatter) the formatter object type
|
14
|
-
# to use for formatting the inspect String
|
15
|
-
# @attr kargs [Hash] options to be sent to {#object} via the
|
16
|
-
# {ObjectInspector::ObjectInterrogator} when calling the `inspect_*` methods
|
17
7
|
class Inspector
|
18
|
-
attr_reader :object
|
19
|
-
:scope,
|
20
|
-
:formatter_klass,
|
21
|
-
:kargs
|
8
|
+
attr_reader :object
|
22
9
|
|
23
10
|
# Shortcuts the instantiation -> {#to_s} flow that would normally be
|
24
11
|
# required to use ObjectInspector::Inspector.
|
@@ -28,6 +15,17 @@ module ObjectInspector
|
|
28
15
|
new(object, **kargs).to_s
|
29
16
|
end
|
30
17
|
|
18
|
+
# @param object [Object] the object being inspected
|
19
|
+
# @param scope [Symbol] Object inspection type. For example:
|
20
|
+
# :self (default) -- Means: Only interrogate self. Don't visit neighbors.
|
21
|
+
# <custom> -- Anything else that makes sense for {@object} to key
|
22
|
+
# on
|
23
|
+
# @param formatter [ObjectInspector::BaseFormatter]
|
24
|
+
# (ObjectInspector.configuration.formatter) the formatter object type
|
25
|
+
# to use for formatting the inspect String
|
26
|
+
# @param kargs [Hash] options to be sent to {@object} via the
|
27
|
+
# {ObjectInspector::ObjectInterrogator} when calling the `inspect_*`
|
28
|
+
# methods
|
31
29
|
def initialize(
|
32
30
|
object,
|
33
31
|
scope: ObjectInspector.configuration.default_scope,
|
@@ -55,19 +53,20 @@ module ObjectInspector
|
|
55
53
|
|
56
54
|
self.class.inspect(
|
57
55
|
extract_wrapped_object,
|
58
|
-
scope: scope,
|
59
|
-
formatter: formatter_klass
|
56
|
+
scope: @scope,
|
57
|
+
formatter: @formatter_klass,
|
58
|
+
kargs: @kargs)
|
60
59
|
end
|
61
60
|
|
62
|
-
# Core object identification details, such as the {
|
61
|
+
# Core object identification details, such as the {@object} class name and
|
63
62
|
# any core-level attributes.
|
64
63
|
#
|
65
64
|
# @return [String]
|
66
65
|
def identification
|
67
|
-
(value(key: :identification) || object.class).to_s
|
66
|
+
(value(key: :identification) || @object.class).to_s
|
68
67
|
end
|
69
68
|
|
70
|
-
# Boolean flags/states applicable to {
|
69
|
+
# Boolean flags/states applicable to {@object}.
|
71
70
|
#
|
72
71
|
# @return [String] if given
|
73
72
|
# @return [NilClass] if not given
|
@@ -75,7 +74,7 @@ module ObjectInspector
|
|
75
74
|
value(key: :flags)
|
76
75
|
end
|
77
76
|
|
78
|
-
# Issues/Warnings applicable to {
|
77
|
+
# Issues/Warnings applicable to {@object}.
|
79
78
|
#
|
80
79
|
# @return [String] if given
|
81
80
|
# @return [NilClass] if not given
|
@@ -83,7 +82,7 @@ module ObjectInspector
|
|
83
82
|
value(key: :issues)
|
84
83
|
end
|
85
84
|
|
86
|
-
# Informational details applicable to {
|
85
|
+
# Informational details applicable to {@object}.
|
87
86
|
#
|
88
87
|
# @return [String] if given
|
89
88
|
# @return [NilClass] if not given
|
@@ -91,29 +90,35 @@ module ObjectInspector
|
|
91
90
|
value(key: :info)
|
92
91
|
end
|
93
92
|
|
94
|
-
#
|
93
|
+
# A human-friendly identifier for {@object}.
|
95
94
|
#
|
96
95
|
# @return [String] if given
|
97
96
|
# @return [NilClass] if not given
|
98
97
|
def name
|
99
|
-
|
100
|
-
|
101
|
-
|
98
|
+
key = :name
|
99
|
+
|
100
|
+
if @kargs.key?(key)
|
101
|
+
value(key: key)
|
102
|
+
else
|
103
|
+
interrogate_object_inspect_method(key) ||
|
104
|
+
interrogate_object(method_name: :display_name,
|
105
|
+
kargs: object_method_keyword_arguments)
|
106
|
+
end
|
102
107
|
end
|
103
108
|
|
104
109
|
private
|
105
110
|
|
106
111
|
def formatter
|
107
|
-
formatter_klass.new(self)
|
112
|
+
@formatter_klass.new(self)
|
108
113
|
end
|
109
114
|
|
110
|
-
# @return [String] if `key` is found in {#kargs} or if {
|
115
|
+
# @return [String] if `key` is found in {#kargs} or if {@object} responds to
|
111
116
|
# `#{object_inspet_method_name}` (e.g. `inspect_flags`)
|
112
|
-
# @return [NilClass] if not found in {#kargs} or {
|
117
|
+
# @return [NilClass] if not found in {#kargs} or {@object}
|
113
118
|
def value(key:)
|
114
119
|
return_value =
|
115
|
-
if
|
116
|
-
evaluate_passed_in_value(
|
120
|
+
if @kargs.key?(key)
|
121
|
+
evaluate_passed_in_value(@kargs[key])
|
117
122
|
else
|
118
123
|
interrogate_object_inspect_method(key)
|
119
124
|
end
|
@@ -121,12 +126,12 @@ module ObjectInspector
|
|
121
126
|
return_value&.to_s
|
122
127
|
end
|
123
128
|
|
124
|
-
# Call `value` on {
|
129
|
+
# Call `value` on {@object} if it responds to it and the result is not nil,
|
125
130
|
# else just return `value`.
|
126
131
|
#
|
127
|
-
# @return [#to_s] if {
|
132
|
+
# @return [#to_s] if {@object} responds to `value` and if the call result
|
128
133
|
# isn't nil
|
129
|
-
# @return [#nil] if {
|
134
|
+
# @return [#nil] if {@object} doesn't respond to `value` or if the call
|
130
135
|
# result is nil
|
131
136
|
def evaluate_passed_in_value(value)
|
132
137
|
if value.is_a?(Symbol)
|
@@ -136,11 +141,11 @@ module ObjectInspector
|
|
136
141
|
end
|
137
142
|
end
|
138
143
|
|
139
|
-
# Attempt to call `inspect_*` on {
|
144
|
+
# Attempt to call `inspect_*` on {@object} based on the passed in `name`.
|
140
145
|
#
|
141
|
-
# @return [String] if {
|
146
|
+
# @return [String] if {@object} responds to
|
142
147
|
# `#{object_inspet_method_name}` (e.g. `inspect_flags`)
|
143
|
-
# @return [NilClass] if not found on {
|
148
|
+
# @return [NilClass] if not found on {@object}
|
144
149
|
def interrogate_object_inspect_method(
|
145
150
|
name,
|
146
151
|
prefix: ObjectInspector.configuration.inspect_method_prefix)
|
@@ -152,7 +157,7 @@ module ObjectInspector
|
|
152
157
|
def interrogate_object(method_name:, kargs: {})
|
153
158
|
interrogator =
|
154
159
|
ObjectInterrogator.new(
|
155
|
-
object: object,
|
160
|
+
object: @object,
|
156
161
|
method_name: method_name,
|
157
162
|
kargs: kargs)
|
158
163
|
|
@@ -167,17 +172,17 @@ module ObjectInspector
|
|
167
172
|
|
168
173
|
def object_method_keyword_arguments
|
169
174
|
{
|
170
|
-
scope: scope
|
175
|
+
scope: @scope
|
171
176
|
}
|
172
177
|
end
|
173
178
|
|
174
179
|
def extract_wrapped_object
|
175
|
-
object.to_model
|
180
|
+
@object.to_model
|
176
181
|
end
|
177
182
|
|
178
183
|
def object_is_a_wrapper?
|
179
|
-
object.respond_to?(:to_model) &&
|
180
|
-
object.to_model != object
|
184
|
+
@object.respond_to?(:to_model) &&
|
185
|
+
@object.to_model != @object
|
181
186
|
end
|
182
187
|
end
|
183
188
|
end
|
@@ -1,15 +1,13 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module ObjectInspector
|
4
|
-
# ObjectInspector::ObjectInterrogator collaborates with {
|
5
|
-
# Object#{
|
4
|
+
# ObjectInspector::ObjectInterrogator collaborates with {@object} to return
|
5
|
+
# Object#{@method_name} if {@object} responds to the method.
|
6
6
|
#
|
7
|
-
# If Object#{
|
8
|
-
# in as well. If not, then any supplied {
|
7
|
+
# If Object#{@method_name} accepts the supplied {@kargs} then they are passed
|
8
|
+
# in as well. If not, then any supplied {@kargs} will be ignored.
|
9
9
|
class ObjectInterrogator
|
10
|
-
attr_reader :object
|
11
|
-
:method_name,
|
12
|
-
:kargs
|
10
|
+
attr_reader :object
|
13
11
|
|
14
12
|
def initialize(object:, method_name:, kargs: {})
|
15
13
|
@object = object
|
@@ -24,23 +22,23 @@ module ObjectInspector
|
|
24
22
|
def call
|
25
23
|
return unless object_responds_to_method_name?
|
26
24
|
|
27
|
-
if object.method(method_name).arity != 0
|
25
|
+
if @object.method(@method_name).arity != 0
|
28
26
|
call_with_kargs
|
29
27
|
else
|
30
|
-
object.
|
28
|
+
@object.__send__(@method_name)
|
31
29
|
end
|
32
30
|
end
|
33
31
|
|
34
32
|
private
|
35
33
|
|
36
34
|
def call_with_kargs
|
37
|
-
object.
|
35
|
+
@object.__send__(@method_name, **@kargs)
|
38
36
|
rescue ArgumentError
|
39
|
-
object.
|
37
|
+
@object.__send__(@method_name)
|
40
38
|
end
|
41
39
|
|
42
40
|
def object_responds_to_method_name?(include_private: true)
|
43
|
-
object.respond_to?(method_name, include_private)
|
41
|
+
@object.respond_to?(@method_name, include_private)
|
44
42
|
end
|
45
43
|
end
|
46
44
|
end
|
@@ -67,8 +67,7 @@ module ObjectInspector
|
|
67
67
|
# @return [FalseClass] if self and `other` resolve to a different set of
|
68
68
|
# objects
|
69
69
|
def ==(other)
|
70
|
-
names.sort ==
|
71
|
-
Array(other).map(&:to_s).sort
|
70
|
+
@names.sort == Array(other).map(&:to_s).sort
|
72
71
|
end
|
73
72
|
alias_method :eql?, :==
|
74
73
|
|
@@ -77,7 +76,7 @@ module ObjectInspector
|
|
77
76
|
end
|
78
77
|
|
79
78
|
def to_a
|
80
|
-
names
|
79
|
+
@names
|
81
80
|
end
|
82
81
|
|
83
82
|
private
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: object_inspector
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Paul Dobbins
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-03-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: benchmark-ips
|
@@ -232,7 +232,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
232
232
|
- !ruby/object:Gem::Version
|
233
233
|
version: '0'
|
234
234
|
requirements: []
|
235
|
-
rubygems_version: 3.0.
|
235
|
+
rubygems_version: 3.0.3
|
236
236
|
signing_key:
|
237
237
|
specification_version: 4
|
238
238
|
summary: ObjectInspector builds uniformly formatted inspect output with customizable
|