object_inspector 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +2 -0
- data/CHANGELOG.md +15 -0
- data/Gemfile.lock +11 -0
- data/README.md +150 -114
- data/lib/object_inspector/{base_formatter.rb → formatters/base_formatter.rb} +14 -36
- data/lib/object_inspector/formatters/combining_formatter.rb +59 -0
- data/lib/object_inspector/formatters/templating_formatter.rb +143 -0
- data/lib/object_inspector/inspector.rb +77 -18
- data/lib/object_inspector/version.rb +1 -1
- data/lib/object_inspector.rb +17 -7
- data/object_inspector.gemspec +4 -1
- data/scripts/benchmarking/formatters.rb +118 -0
- data/scripts/benchmarking/object_inspector.rb +73 -0
- metadata +37 -6
- data/lib/object_inspector/default_formatter.rb +0 -33
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8f80de3b28fe43b95e5fb12538a371807689aa29da9bf7d8a91eb9c4b8b4086c
|
4
|
+
data.tar.gz: 775cecd797055357a8b4fb1c97d7c02a2821b8fbfbb0482f5a646f68da6a3710
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3a0a0c78c42099cc7ea4b03276f6d06182ccad7e4ff657a4875cbe6690f7e277c3861fe7e5f00ebd8f7059516abb064f50ef78bf73ce26fd929f22a23143fddb
|
7
|
+
data.tar.gz: 136d08a468531350650e48585f4471948cfdbd877ea4d462d07c11acdb381b276855c9520367dd1fc00ed8a21924b96a561e32b06d7dd51292a8705127221378
|
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,18 @@
|
|
1
|
+
#### TODO
|
2
|
+
* Add "placeholder" symbol (*) for scope exclusions.
|
3
|
+
* Add gem defaults configuration.
|
4
|
+
|
5
|
+
|
6
|
+
### 0.2.0 - 2018-04-12
|
7
|
+
|
8
|
+
* Automatically inspect wrapped Objects, if applicable.
|
9
|
+
* Use `display_name` if defined on object, in place of `inspect_name`.
|
10
|
+
* Add on-the-fly inspect methods when Symbols are passed in to #inspect.
|
11
|
+
* Update the `flags` and `info` demarcation symbols.
|
12
|
+
* Add ObjectInspector::TemplatingFormatter, and use it as the new default since it's faster.
|
13
|
+
* Rename ObjectInspector::DefaultFormatter to ObjectInspector::CombiningFormatter.
|
14
|
+
|
15
|
+
|
1
16
|
### 0.1.0 - 2018-04-09
|
2
17
|
|
3
18
|
* Initial release!
|
data/Gemfile.lock
CHANGED
@@ -6,12 +6,20 @@ PATH
|
|
6
6
|
GEM
|
7
7
|
remote: https://rubygems.org/
|
8
8
|
specs:
|
9
|
+
ansi (1.5.0)
|
10
|
+
benchmark-ips (2.7.2)
|
11
|
+
builder (3.2.3)
|
9
12
|
byebug (10.0.2)
|
10
13
|
coderay (1.1.2)
|
11
14
|
docile (1.3.0)
|
12
15
|
json (2.1.0)
|
13
16
|
method_source (0.9.0)
|
14
17
|
minitest (5.11.3)
|
18
|
+
minitest-reporters (1.2.0)
|
19
|
+
ansi
|
20
|
+
builder
|
21
|
+
minitest (>= 5.0)
|
22
|
+
ruby-progressbar
|
15
23
|
pry (0.11.3)
|
16
24
|
coderay (~> 1.1.0)
|
17
25
|
method_source (~> 0.9.0)
|
@@ -19,6 +27,7 @@ GEM
|
|
19
27
|
byebug (~> 10.0)
|
20
28
|
pry (~> 0.10)
|
21
29
|
rake (10.4.2)
|
30
|
+
ruby-progressbar (1.9.0)
|
22
31
|
simplecov (0.16.1)
|
23
32
|
docile (~> 1.1)
|
24
33
|
json (>= 1.8, < 3)
|
@@ -29,9 +38,11 @@ PLATFORMS
|
|
29
38
|
ruby
|
30
39
|
|
31
40
|
DEPENDENCIES
|
41
|
+
benchmark-ips (~> 2.7)
|
32
42
|
bundler (~> 1.16)
|
33
43
|
byebug (~> 10.0)
|
34
44
|
minitest (~> 5.0)
|
45
|
+
minitest-reporters (~> 1.2)
|
35
46
|
object_inspector!
|
36
47
|
pry (~> 0.11)
|
37
48
|
pry-byebug (~> 3.6)
|
data/README.md
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
# ObjectInspector
|
2
2
|
|
3
|
-
[![Gem Version](https://badge.fury.io/rb/object_inspector.
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/object_inspector.svg)](https://badge.fury.io/rb/object_inspector)
|
4
4
|
[![Build Status](https://travis-ci.org/objects-on-rails/display-case.svg?branch=master)](https://travis-ci.org/objects-on-rails/display-case)
|
5
5
|
[![Test Coverage](https://api.codeclimate.com/v1/badges/34e821263d9e0c33d536/test_coverage)](https://codeclimate.com/github/pdobb/object_inspector/test_coverage)
|
6
6
|
[![Maintainability](https://api.codeclimate.com/v1/badges/34e821263d9e0c33d536/maintainability)](https://codeclimate.com/github/pdobb/object_inspector/maintainability)
|
7
7
|
|
8
|
-
ObjectInspector takes Object#inspect to the next level. Specify any combination of identification attributes, flags, info, and/or a name along with
|
8
|
+
ObjectInspector takes Object#inspect to the next level. Specify any combination of identification attributes, flags, info, and/or a name along with an optional self-definable scope option to represent an object in the console, in logging, etc.
|
9
9
|
|
10
10
|
|
11
11
|
## Installation
|
@@ -36,19 +36,7 @@ Tested MRI Ruby Versions:
|
|
36
36
|
|
37
37
|
## Usage
|
38
38
|
|
39
|
-
Given, an object of any type, call ObjectInspector::
|
40
|
-
|
41
|
-
```ruby
|
42
|
-
class MyObject
|
43
|
-
def inspect
|
44
|
-
ObjectInspector::Inspector.new(self).to_s
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
MyObject.new.inspect # => "<MyObject>"
|
49
|
-
```
|
50
|
-
|
51
|
-
Or, just use the ObjectInspector::Inspector.inspect method.
|
39
|
+
Given, an object of any type, call ObjectInspector::Inspector.inspect.
|
52
40
|
|
53
41
|
```ruby
|
54
42
|
class MyObject
|
@@ -65,24 +53,23 @@ See also [Helper Usage](#helper-usage) for an even simpler usage option.
|
|
65
53
|
|
66
54
|
### Output Customization
|
67
55
|
|
68
|
-
Use
|
56
|
+
Use the `identification`, `flags`, `info`, and `name` options to customize inspect output.
|
69
57
|
|
70
58
|
```ruby
|
71
59
|
class MyObject
|
72
60
|
def inspect
|
73
|
-
ObjectInspector::Inspector.inspect(
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
name: "NAME")
|
61
|
+
ObjectInspector::Inspector.inspect(self,
|
62
|
+
identification: "My Object",
|
63
|
+
flags: "FLAG1",
|
64
|
+
info: "INFO",
|
65
|
+
name: "NAME")
|
79
66
|
end
|
80
67
|
end
|
81
68
|
|
82
|
-
MyObject.new.inspect # => "<My Object
|
69
|
+
MyObject.new.inspect # => "<My Object(FLAG1) INFO :: NAME>"
|
83
70
|
```
|
84
71
|
|
85
|
-
Or, define `inspect_identification`, `inspect_flags`, `inspect_info`, and `inspect_name`
|
72
|
+
Or, define `inspect_identification`, `inspect_flags`, `inspect_info`, and `inspect_name` as either public or private methods on Object.
|
86
73
|
|
87
74
|
```ruby
|
88
75
|
class MyObject
|
@@ -92,30 +79,19 @@ class MyObject
|
|
92
79
|
|
93
80
|
private
|
94
81
|
|
95
|
-
def inspect_identification
|
96
|
-
|
97
|
-
end
|
98
|
-
|
99
|
-
def inspect_flags
|
100
|
-
"FLAG1"
|
101
|
-
end
|
102
|
-
|
103
|
-
def inspect_info
|
104
|
-
"INFO"
|
105
|
-
end
|
106
|
-
|
107
|
-
def inspect_name
|
108
|
-
"NAME"
|
109
|
-
end
|
82
|
+
def inspect_identification; "My Object" end
|
83
|
+
def inspect_flags; "FLAG1" end
|
84
|
+
def inspect_info; "INFO" end
|
85
|
+
def inspect_name; "NAME" end
|
110
86
|
end
|
111
87
|
|
112
|
-
MyObject.new.inspect # => "<My Object
|
88
|
+
MyObject.new.inspect # => "<My Object(FLAG1) INFO :: NAME>"
|
113
89
|
```
|
114
90
|
|
115
91
|
|
116
92
|
## Helper Usage
|
117
93
|
|
118
|
-
To save
|
94
|
+
To save some typing, include ObjectInspector::InspectHelper into an object and ObjectInspector::Inspector.inspect will be called on `self` automatically.
|
119
95
|
|
120
96
|
```ruby
|
121
97
|
class MyObject
|
@@ -139,7 +115,7 @@ class MyObject
|
|
139
115
|
end
|
140
116
|
end
|
141
117
|
|
142
|
-
MyObject.new.inspect # => "<My Object
|
118
|
+
MyObject.new.inspect # => "<My Object(FLAG1) INFO :: NAME>"
|
143
119
|
```
|
144
120
|
|
145
121
|
Or, define `inspect_identification`, `inspect_flags`, `inspect_info`, and `inspect_name` in Object.
|
@@ -150,39 +126,46 @@ class MyObject
|
|
150
126
|
|
151
127
|
private
|
152
128
|
|
153
|
-
def inspect_identification
|
154
|
-
|
155
|
-
end
|
129
|
+
def inspect_identification; "My Object" end
|
130
|
+
def inspect_flags; "FLAG1" end
|
131
|
+
def inspect_info; "INFO" end
|
132
|
+
def inspect_name; "NAME" end
|
133
|
+
end
|
156
134
|
|
157
|
-
|
158
|
-
|
159
|
-
end
|
135
|
+
MyObject.new.inspect # => "<My Object(FLAG1) INFO :: NAME>"
|
136
|
+
```
|
160
137
|
|
161
|
-
def inspect_info
|
162
|
-
"INFO"
|
163
|
-
end
|
164
138
|
|
165
|
-
|
166
|
-
|
167
|
-
|
139
|
+
## On-the-fly Inspect Methods
|
140
|
+
|
141
|
+
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.
|
142
|
+
|
143
|
+
```ruby
|
144
|
+
class MyObject
|
145
|
+
include ObjectInspector::InspectorsHelper
|
146
|
+
|
147
|
+
def my_method1; "Result1" end
|
148
|
+
def my_method2; "Result2" end
|
149
|
+
|
150
|
+
def inspect_info; :my_method2 end
|
168
151
|
end
|
169
152
|
|
170
|
-
MyObject.new.inspect # => "<
|
153
|
+
MyObject.new.inspect(info: "my_method1") # => "<MyObject my_method1>"
|
154
|
+
MyObject.new.inspect(info: :my_method2) # => "<MyObject Result2>"
|
155
|
+
MyObject.new.inspect # => "<MyObject my_method2>"
|
171
156
|
```
|
172
157
|
|
173
158
|
|
174
159
|
#### Scope
|
175
160
|
|
176
|
-
Use the `scope` option to define the scope of the `inspect_
|
161
|
+
Use the `scope` option to define the scope of the `inspect_*` methods.
|
177
162
|
|
178
163
|
If ActiveSupport::StringInquirer is defined then the default `scope` is `"self".inquiry`.
|
179
164
|
The default value is `:self` if ActiveSupport::StringInquirer is not defined.
|
180
165
|
|
181
166
|
```ruby
|
182
167
|
class MyObject
|
183
|
-
|
184
|
-
ObjectInspector::Inspector.inspect(self, scope: scope)
|
185
|
-
end
|
168
|
+
include ObjectInspector::InspectorsHelper
|
186
169
|
|
187
170
|
def inspect_flags(scope:, separator: " / ".freeze)
|
188
171
|
flags = ["FLAG1"]
|
@@ -197,37 +180,52 @@ class MyObject
|
|
197
180
|
end
|
198
181
|
end
|
199
182
|
|
200
|
-
MyObject.new.inspect
|
201
|
-
MyObject.new.inspect(scope: :all) # => "<MyObject
|
183
|
+
MyObject.new.inspect # => "<MyObject(FLAG1)>"
|
184
|
+
MyObject.new.inspect(scope: :all) # => "<MyObject(FLAG1 / FLAG2)>"
|
202
185
|
```
|
203
186
|
|
204
187
|
|
205
|
-
##
|
188
|
+
## Wrapped Objects
|
206
189
|
|
207
|
-
|
190
|
+
If the Object being inspected wraps another object -- i.e. defines #to_model and #to_model returns an object other than self -- the inspect output will re-inspect the wrapped object. The wrapper points to the wrapped object with an arrow (⇨).
|
208
191
|
|
209
192
|
```ruby
|
210
|
-
class
|
211
|
-
|
212
|
-
|
193
|
+
class MyWrapperObject
|
194
|
+
include ObjectInspector::InspectorsHelper
|
195
|
+
|
196
|
+
def to_model
|
197
|
+
@to_model ||= MyWrappedObject.new
|
213
198
|
end
|
214
199
|
|
215
200
|
private
|
216
201
|
|
217
|
-
def
|
218
|
-
|
219
|
-
end
|
202
|
+
def inspect_flags; "WRAPPER_FLAG1" end
|
203
|
+
end
|
220
204
|
|
221
|
-
|
222
|
-
|
223
|
-
end
|
205
|
+
class MyWrappedObject
|
206
|
+
include ObjectInspector::InspectorsHelper
|
224
207
|
|
225
|
-
|
226
|
-
|
227
|
-
end
|
208
|
+
private
|
209
|
+
|
210
|
+
def inspect_flags; "FLAG1" end
|
211
|
+
def inspect_info; "INFO" end
|
212
|
+
end
|
213
|
+
|
214
|
+
MyWrapperObject.new.inspect
|
215
|
+
# => "<MyWrapperObject(WRAPPER_FLAG1)> ⇨ <MyWrappedObject(FLAG1) INFO>"
|
216
|
+
```
|
217
|
+
|
218
|
+
This feature is recursive.
|
219
|
+
|
220
|
+
|
221
|
+
## Custom Formatters
|
222
|
+
|
223
|
+
A custom inspect formatter can be defined by implementing the interface defined by [ObjectInspector::BaseFormatter](https://github.com/pdobb/object_inspector/blob/master/lib/object_inspector/formatters/base_formatter.rb) and then passing that into ObjectInspector::Inspector.new.
|
228
224
|
|
229
|
-
|
230
|
-
|
225
|
+
```ruby
|
226
|
+
class MyCustomFormatter < ObjectInspector::BaseFormatter
|
227
|
+
def call
|
228
|
+
"[#{identification} Flags: #{flags} -- Info: #{info} -- Name: #{name}]"
|
231
229
|
end
|
232
230
|
end
|
233
231
|
|
@@ -235,35 +233,77 @@ class MyObject
|
|
235
233
|
include ObjectInspector::InspectorsHelper
|
236
234
|
|
237
235
|
def inspect
|
238
|
-
super(formatter: MyCustomFormatter
|
236
|
+
super(formatter: MyCustomFormatter,
|
237
|
+
identification: "IDENTIFICATION",
|
238
|
+
flags: "FLAG1 | FLAG2",
|
239
|
+
info: "INFO",
|
240
|
+
name: "NAME")
|
239
241
|
end
|
242
|
+
end
|
240
243
|
|
241
|
-
|
244
|
+
MyObject.new.inspect
|
245
|
+
# => "[IDENTIFICATION Flags: FLAG1 | FLAG2 -- Info: INFO -- Name: NAME]"
|
246
|
+
```
|
242
247
|
|
243
|
-
|
244
|
-
|
245
|
-
end
|
248
|
+
See also: [ObjectInspector::TemplatingFormatter].
|
249
|
+
See also: [ObjectInspector::CombiningFormatter].
|
246
250
|
|
247
|
-
def inspect_flags
|
248
|
-
"FLAG1"
|
249
|
-
end
|
250
251
|
|
251
|
-
|
252
|
-
"INFO"
|
253
|
-
end
|
252
|
+
## Performance
|
254
253
|
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
254
|
+
### Benchmarking ObjectInspector
|
255
|
+
|
256
|
+
ObjectInspetor is ~4x slower than Ruby's default inspect.
|
257
|
+
|
258
|
+
Performance of ObjectInspect can be tested by playing the [ObjectInspector Benchmarking Scripts] in the pry console for this gem.
|
259
259
|
|
260
|
-
|
260
|
+
```ruby
|
261
|
+
play scripts/benchmarking/object_inspector.rb
|
262
|
+
# Comparison:
|
263
|
+
# Ruby: 30382.2 i/s
|
264
|
+
# ObjectInspector::Inspector: 7712.2 i/s - 3.94x slower
|
261
265
|
```
|
262
266
|
|
263
|
-
|
267
|
+
|
268
|
+
### Benchmarking Formatters
|
269
|
+
|
270
|
+
[ObjectInspector::TemplatingFormatter] -- which is the default Formatter -- outperforms [ObjectInspector::CombiningFormatter] by about 30% on average.
|
271
|
+
|
272
|
+
Performance of Formatters can be tested by playing the [Formatters Benchmarking Scripts] in the pry console for this gem.
|
273
|
+
|
274
|
+
```ruby
|
275
|
+
play scripts/benchmarking/formatters.rb
|
276
|
+
# == Averaged =============================================================
|
277
|
+
# ...
|
278
|
+
#
|
279
|
+
# Comparison:
|
280
|
+
# ObjectInspector::TemplatingFormatter: 45725.3 i/s
|
281
|
+
# ObjectInspector::CombiningFormatter: 34973.9 i/s - 1.31x slower
|
282
|
+
#
|
283
|
+
# == Done
|
284
|
+
```
|
285
|
+
|
286
|
+
#### Benchmarking Custom Formatters
|
287
|
+
|
288
|
+
Custom Formatters may be similarly gauged for comparison by adding them to the `custom_formatter_klasses` array before playing the script.
|
289
|
+
|
290
|
+
```ruby
|
291
|
+
custom_formatter_klasses = [MyCustomFormatter]
|
292
|
+
|
293
|
+
play scripts/benchmarking/formatters.rb
|
294
|
+
# == Averaged =============================================================
|
295
|
+
# ...
|
296
|
+
#
|
297
|
+
# Comparison:
|
298
|
+
# MyCustomFormatter: 52001.2 i/s
|
299
|
+
# ObjectInspector::TemplatingFormatter: 49854.2 i/s - same-ish: difference falls within error
|
300
|
+
# ObjectInspector::CombiningFormatter: 38963.5 i/s - 1.33x slower
|
301
|
+
#
|
302
|
+
# == Done
|
303
|
+
```
|
264
304
|
|
265
305
|
|
266
|
-
## Supporting
|
306
|
+
## Supporting Gems
|
267
307
|
|
268
308
|
ObjectInspector works great with the [ObjectIdentifier](https://github.com/pdobb/object_identifier) gem.
|
269
309
|
|
@@ -272,33 +312,23 @@ class MyObject
|
|
272
312
|
include ObjectInspector::InspectorsHelper
|
273
313
|
|
274
314
|
def my_method1
|
275
|
-
"
|
315
|
+
"Result1"
|
276
316
|
end
|
277
317
|
|
278
|
-
def
|
279
|
-
"
|
318
|
+
def my_method2
|
319
|
+
"Result2"
|
280
320
|
end
|
281
321
|
|
282
322
|
private
|
283
323
|
|
284
|
-
def inspect_identification
|
285
|
-
|
286
|
-
end
|
287
|
-
|
288
|
-
def inspect_flags
|
289
|
-
"FLAG1"
|
290
|
-
end
|
291
|
-
|
292
|
-
def inspect_info
|
293
|
-
"INFO"
|
294
|
-
end
|
295
|
-
|
296
|
-
def inspect_name
|
297
|
-
"NAME"
|
298
|
-
end
|
324
|
+
def inspect_identification; identify(:my_method1, :my_method2) end
|
325
|
+
def inspect_flags; "FLAG1" end
|
326
|
+
def inspect_info; "INFO" end
|
327
|
+
def inspect_name; "NAME" end
|
299
328
|
end
|
300
329
|
|
301
|
-
MyObject.new.inspect
|
330
|
+
MyObject.new.inspect
|
331
|
+
# => "<MyObject[my_method1:Result1, my_method2:Result2](FLAG1) INFO :: NAME>"
|
302
332
|
```
|
303
333
|
|
304
334
|
|
@@ -317,3 +347,9 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/pdobb/
|
|
317
347
|
## License
|
318
348
|
|
319
349
|
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
350
|
+
|
351
|
+
|
352
|
+
[ObjectInspector::TemplatingFormatter]: https://github.com/pdobb/object_inspector/blob/master/lib/object_inspector/formatters/templating_formatter.rb
|
353
|
+
[ObjectInspector::CombiningFormatter]: https://github.com/pdobb/object_inspector/blob/master/lib/object_inspector/formatters/combining_formatter.rb
|
354
|
+
[ObjectInspector Benchmarking Scripts]: https://github.com/pdobb/object_inspector/blob/master/scripts/benchmarking/object_inspector.rb
|
355
|
+
[Formatters Benchmarking Scripts]: https://github.com/pdobb/object_inspector/blob/master/scripts/benchmarking/formatters.rb
|
@@ -6,6 +6,8 @@ module ObjectInspector
|
|
6
6
|
#
|
7
7
|
# @attr inspector [ObjectInspector::Inspector]
|
8
8
|
class BaseFormatter
|
9
|
+
RIGHT_ARROW_ICON = [0x21E8].pack("U").freeze
|
10
|
+
|
9
11
|
attr_reader :inspector
|
10
12
|
|
11
13
|
def initialize(inspector)
|
@@ -19,11 +21,19 @@ module ObjectInspector
|
|
19
21
|
raise NotImplementedError
|
20
22
|
end
|
21
23
|
|
24
|
+
# Delegates to {Inspector#wrapped_object_inspection}.
|
25
|
+
#
|
26
|
+
# @return [String] if given
|
27
|
+
# @return [NilClass] if not given
|
28
|
+
def wrapped_object_inspection
|
29
|
+
@wrapped_object_inspection ||= inspector.wrapped_object_inspection
|
30
|
+
end
|
31
|
+
|
22
32
|
# Delegates to {Inspector#identification}.
|
23
33
|
#
|
24
34
|
# @return [String] if given
|
25
35
|
def identification
|
26
|
-
@inspector.identification
|
36
|
+
@identification ||= inspector.identification
|
27
37
|
end
|
28
38
|
|
29
39
|
# Delegates to {Inspector#flags}.
|
@@ -31,7 +41,7 @@ module ObjectInspector
|
|
31
41
|
# @return [String] if given
|
32
42
|
# @return [NilClass] if not given
|
33
43
|
def flags
|
34
|
-
@inspector.flags
|
44
|
+
@flags ||= inspector.flags
|
35
45
|
end
|
36
46
|
|
37
47
|
# Delegates to {Inspector#info}.
|
@@ -39,7 +49,7 @@ module ObjectInspector
|
|
39
49
|
# @return [String] if given
|
40
50
|
# @return [NilClass] if not given
|
41
51
|
def info
|
42
|
-
@inspector.info
|
52
|
+
@info ||= inspector.info
|
43
53
|
end
|
44
54
|
|
45
55
|
# Delegates to {Inspector#name}.
|
@@ -47,39 +57,7 @@ module ObjectInspector
|
|
47
57
|
# @return [String] if given
|
48
58
|
# @return [NilClass] if not given
|
49
59
|
def name
|
50
|
-
@inspector.name
|
51
|
-
end
|
52
|
-
|
53
|
-
private
|
54
|
-
|
55
|
-
def combine_strings
|
56
|
-
strings.join
|
57
|
-
end
|
58
|
-
|
59
|
-
# Override in subclasses as needed.
|
60
|
-
def strings
|
61
|
-
[
|
62
|
-
build_identification_string,
|
63
|
-
build_flags_string,
|
64
|
-
build_info_string,
|
65
|
-
build_name_string,
|
66
|
-
].compact
|
67
|
-
end
|
68
|
-
|
69
|
-
def build_identification_string(*)
|
70
|
-
raise NotImplementedError
|
71
|
-
end
|
72
|
-
|
73
|
-
def build_flags_string(*)
|
74
|
-
raise NotImplementedError
|
75
|
-
end
|
76
|
-
|
77
|
-
def build_info_string(*)
|
78
|
-
raise NotImplementedError
|
79
|
-
end
|
80
|
-
|
81
|
-
def build_name_string(*)
|
82
|
-
raise NotImplementedError
|
60
|
+
@name ||= inspector.name
|
83
61
|
end
|
84
62
|
end
|
85
63
|
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
module ObjectInspector
|
2
|
+
# ObjectInspector::CombiningFormatter implements
|
3
|
+
# {ObjectInspector::BaseFormatter} to return the standard/default inspect
|
4
|
+
# output format by combining Strings.
|
5
|
+
#
|
6
|
+
# @attr (see BaseFormatter)
|
7
|
+
class CombiningFormatter < BaseFormatter
|
8
|
+
# Perform the formatting routine.
|
9
|
+
#
|
10
|
+
# @return [String]
|
11
|
+
def call
|
12
|
+
if wrapped_object_inspection
|
13
|
+
build_wrapped_object_string
|
14
|
+
else
|
15
|
+
build_string
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def build_wrapped_object_string
|
22
|
+
"#{build_string} #{RIGHT_ARROW_ICON} #{wrapped_object_inspection}"
|
23
|
+
end
|
24
|
+
|
25
|
+
def build_string
|
26
|
+
"<#{combine_strings}>"
|
27
|
+
end
|
28
|
+
|
29
|
+
def combine_strings
|
30
|
+
strings.join
|
31
|
+
end
|
32
|
+
|
33
|
+
# Override in subclasses as needed.
|
34
|
+
def strings
|
35
|
+
[
|
36
|
+
build_identification_string,
|
37
|
+
build_flags_string,
|
38
|
+
build_info_string,
|
39
|
+
build_name_string,
|
40
|
+
].compact
|
41
|
+
end
|
42
|
+
|
43
|
+
def build_identification_string
|
44
|
+
identification.to_s
|
45
|
+
end
|
46
|
+
|
47
|
+
def build_flags_string
|
48
|
+
"(#{flags.to_s.upcase})" if flags
|
49
|
+
end
|
50
|
+
|
51
|
+
def build_info_string
|
52
|
+
" #{info}" if info
|
53
|
+
end
|
54
|
+
|
55
|
+
def build_name_string
|
56
|
+
" :: #{name}" if name
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,143 @@
|
|
1
|
+
module ObjectInspector
|
2
|
+
# ObjectInspector::TemplatingFormatter implements
|
3
|
+
# {ObjectInspector::BaseFormatter} to return the standard/default inspect
|
4
|
+
# output format via String templates.
|
5
|
+
#
|
6
|
+
# @attr (see BaseFormatter)
|
7
|
+
class TemplatingFormatter < BaseFormatter
|
8
|
+
def self.base_template
|
9
|
+
@base_template ||= "<%s>".freeze
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.name_template
|
13
|
+
@name_template ||= "<%s :: %s>".freeze
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.flags_and_name_template
|
17
|
+
@flags_and_name_template ||= "<%s(%s) :: %s>".freeze
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.info_and_name_template
|
21
|
+
@info_and_name_template ||= "<%s %s :: %s>".freeze
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.flags_and_info_template
|
25
|
+
@flags_and_info_template ||= "<%s(%s) %s>".freeze
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.flags_and_info_and_name_template
|
29
|
+
@flags_and_info_and_name_template ||= "<%s(%s) %s :: %s>".freeze
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.flags_template
|
33
|
+
@flags_template ||= "<%s(%s)>".freeze
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.info_template
|
37
|
+
@info_template ||= "<%s %s>".freeze
|
38
|
+
end
|
39
|
+
|
40
|
+
# Perform the formatting routine.
|
41
|
+
#
|
42
|
+
# @return [String]
|
43
|
+
def call
|
44
|
+
if wrapped_object_inspection
|
45
|
+
build_wrapped_object_string
|
46
|
+
else
|
47
|
+
build_string
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
private
|
52
|
+
|
53
|
+
def build_wrapped_object_string
|
54
|
+
"#{build_string} #{RIGHT_ARROW_ICON} #{wrapped_object_inspection}"
|
55
|
+
end
|
56
|
+
|
57
|
+
def build_string
|
58
|
+
if flags
|
59
|
+
if info
|
60
|
+
if name
|
61
|
+
build_flags_and_info_and_name_string
|
62
|
+
else
|
63
|
+
build_flags_and_info_string
|
64
|
+
end
|
65
|
+
elsif name
|
66
|
+
build_flags_and_name_string
|
67
|
+
else
|
68
|
+
build_flags_string
|
69
|
+
end
|
70
|
+
elsif info
|
71
|
+
if name
|
72
|
+
build_info_and_name_string
|
73
|
+
else
|
74
|
+
build_info_string
|
75
|
+
end
|
76
|
+
elsif name
|
77
|
+
build_name_string
|
78
|
+
else
|
79
|
+
build_base_string
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
def build_flags_and_info_and_name_string
|
84
|
+
self.class.flags_and_info_and_name_template % [
|
85
|
+
identification,
|
86
|
+
flags,
|
87
|
+
info,
|
88
|
+
name
|
89
|
+
]
|
90
|
+
end
|
91
|
+
|
92
|
+
def build_flags_and_info_string
|
93
|
+
self.class.flags_and_info_template % [
|
94
|
+
identification,
|
95
|
+
flags,
|
96
|
+
info
|
97
|
+
]
|
98
|
+
end
|
99
|
+
|
100
|
+
def build_flags_and_name_string
|
101
|
+
self.class.flags_and_name_template % [
|
102
|
+
identification,
|
103
|
+
flags,
|
104
|
+
name
|
105
|
+
]
|
106
|
+
end
|
107
|
+
|
108
|
+
def build_info_and_name_string
|
109
|
+
self.class.info_and_name_template % [
|
110
|
+
identification,
|
111
|
+
info,
|
112
|
+
name
|
113
|
+
]
|
114
|
+
end
|
115
|
+
|
116
|
+
def build_name_string
|
117
|
+
self.class.name_template % [
|
118
|
+
identification,
|
119
|
+
name
|
120
|
+
]
|
121
|
+
end
|
122
|
+
|
123
|
+
def build_flags_string
|
124
|
+
self.class.flags_template % [
|
125
|
+
identification,
|
126
|
+
flags
|
127
|
+
]
|
128
|
+
end
|
129
|
+
|
130
|
+
def build_info_string
|
131
|
+
self.class.info_template % [
|
132
|
+
identification,
|
133
|
+
info
|
134
|
+
]
|
135
|
+
end
|
136
|
+
|
137
|
+
def build_base_string
|
138
|
+
self.class.base_template % [
|
139
|
+
identification
|
140
|
+
]
|
141
|
+
end
|
142
|
+
end
|
143
|
+
end
|
@@ -1,6 +1,7 @@
|
|
1
1
|
module ObjectInspector
|
2
|
-
# ObjectInspector organizes inspection of the associated {#object}
|
3
|
-
# passed in options and via a {ObjectInspector::BaseFormatter}
|
2
|
+
# ObjectInspector::Inspector organizes inspection of the associated {#object}
|
3
|
+
# via the passed in options and via a {ObjectInspector::BaseFormatter}
|
4
|
+
# instance.
|
4
5
|
#
|
5
6
|
# @attr object [Object] the object being inspected
|
6
7
|
# @attr scope [Symbol] Object inspection type. For example:
|
@@ -20,7 +21,7 @@ module ObjectInspector
|
|
20
21
|
|
21
22
|
# The prefix for all methods called on {#object} for inspect
|
22
23
|
# details/strings.
|
23
|
-
def self.
|
24
|
+
def self.object_inspect_method_prefix
|
24
25
|
"inspect".freeze
|
25
26
|
end
|
26
27
|
|
@@ -35,11 +36,16 @@ module ObjectInspector
|
|
35
36
|
def initialize(
|
36
37
|
object,
|
37
38
|
scope: :self,
|
38
|
-
formatter:
|
39
|
+
formatter: TemplatingFormatter,
|
39
40
|
**kargs)
|
40
41
|
@object = object
|
41
42
|
@formatter_klass = formatter
|
42
|
-
@scope =
|
43
|
+
@scope =
|
44
|
+
if ObjectInspector.use_string_inquirers?
|
45
|
+
scope.to_s.inquiry
|
46
|
+
else
|
47
|
+
scope.to_sym
|
48
|
+
end
|
43
49
|
@kargs = kargs
|
44
50
|
end
|
45
51
|
|
@@ -50,6 +56,19 @@ module ObjectInspector
|
|
50
56
|
formatter.call
|
51
57
|
end
|
52
58
|
|
59
|
+
# Generate the inspect String for a wrapped object, if applicable.
|
60
|
+
#
|
61
|
+
# @return [String] if {#object_is_a_wrapper}
|
62
|
+
# @return [NilClass] if not {#object_is_a_wrapper}
|
63
|
+
def wrapped_object_inspection
|
64
|
+
if object_is_a_wrapper?
|
65
|
+
self.class.inspect(
|
66
|
+
extract_wrapped_object,
|
67
|
+
scope: scope,
|
68
|
+
formatter: formatter_klass)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
53
72
|
# Core object identification details, such as the {#object} class name and
|
54
73
|
# any core-level attributes.
|
55
74
|
#
|
@@ -75,10 +94,13 @@ module ObjectInspector
|
|
75
94
|
end
|
76
95
|
|
77
96
|
# The generally human-friendly unique identifier for {#object}.
|
97
|
+
#
|
78
98
|
# @return [String] if given
|
79
99
|
# @return [NilClass] if not given
|
80
100
|
def name
|
81
|
-
value(key: :name)
|
101
|
+
value(key: :name) ||
|
102
|
+
interrogate_object(method_name: :display_name,
|
103
|
+
kargs: object_method_keyword_arguments)
|
82
104
|
end
|
83
105
|
|
84
106
|
private
|
@@ -88,33 +110,61 @@ module ObjectInspector
|
|
88
110
|
end
|
89
111
|
|
90
112
|
# @return [String] if `key` is found in {#kargs} or if {#object} responds to
|
91
|
-
# `#{
|
113
|
+
# `#{object_inspect_method_prefix}_#{key}` (e.g. `inspect_flags`)
|
92
114
|
# @return [NilClass] if not found in {#kargs} or {#object}
|
93
115
|
def value(key:)
|
94
|
-
|
116
|
+
return_value =
|
117
|
+
if (passed_in_value = kargs[key])
|
118
|
+
evaluate_passed_in_value(passed_in_value)
|
119
|
+
else
|
120
|
+
interrogate_object_inspect_method(key)
|
121
|
+
end
|
122
|
+
|
123
|
+
return_value.to_s if return_value
|
124
|
+
end
|
95
125
|
|
96
|
-
|
126
|
+
# Call `value` on {#object} if it responds to it and the result is not nil,
|
127
|
+
# else just return `value`.
|
128
|
+
#
|
129
|
+
# @return [#to_s] if {#object} responds to `value` and if the call result
|
130
|
+
# isn't nil
|
131
|
+
# @return [#nil] if {#object} doesn't respond to `value` or if the call
|
132
|
+
# result is nil
|
133
|
+
def evaluate_passed_in_value(value)
|
134
|
+
if value.is_a?(Symbol)
|
135
|
+
interrogate_object(method_name: value) || value
|
136
|
+
else
|
137
|
+
value
|
138
|
+
end
|
97
139
|
end
|
98
140
|
|
99
|
-
#
|
100
|
-
#
|
141
|
+
# Attempt to call `inspect_*` on {#object} based on the passed in `name`.
|
142
|
+
#
|
143
|
+
# @return [String] if {#object} responds to
|
144
|
+
# `#{object_inspect_method_prefix}_#{name}` (e.g. `inspect_flags`)
|
101
145
|
# @return [NilClass] if not found on {#object}
|
102
|
-
def
|
146
|
+
def interrogate_object_inspect_method(name)
|
147
|
+
interrogate_object(
|
148
|
+
method_name: build_inspet_method_name(name),
|
149
|
+
kargs: object_method_keyword_arguments)
|
150
|
+
end
|
151
|
+
|
152
|
+
def interrogate_object(method_name:, kargs: {})
|
103
153
|
interrogator =
|
104
154
|
ObjectInterrogator.new(
|
105
155
|
object: object,
|
106
|
-
method_name:
|
107
|
-
kargs:
|
156
|
+
method_name: method_name,
|
157
|
+
kargs: kargs)
|
108
158
|
|
109
159
|
interrogator.call
|
110
160
|
end
|
111
161
|
|
112
|
-
def
|
113
|
-
"#{
|
162
|
+
def build_inspet_method_name(name)
|
163
|
+
"#{object_inspect_method_prefix}_#{name}"
|
114
164
|
end
|
115
165
|
|
116
|
-
def
|
117
|
-
self.class.
|
166
|
+
def object_inspect_method_prefix
|
167
|
+
self.class.object_inspect_method_prefix
|
118
168
|
end
|
119
169
|
|
120
170
|
def object_method_keyword_arguments
|
@@ -122,5 +172,14 @@ module ObjectInspector
|
|
122
172
|
scope: scope,
|
123
173
|
}
|
124
174
|
end
|
175
|
+
|
176
|
+
def extract_wrapped_object
|
177
|
+
object.to_model
|
178
|
+
end
|
179
|
+
|
180
|
+
def object_is_a_wrapper?
|
181
|
+
object.respond_to?(:to_model) &&
|
182
|
+
object.to_model != object
|
183
|
+
end
|
125
184
|
end
|
126
185
|
end
|
data/lib/object_inspector.rb
CHANGED
@@ -1,11 +1,21 @@
|
|
1
|
-
require "object_inspector/version"
|
2
|
-
|
3
1
|
# ObjectInspector is the base namespace for all modules/classes related to the
|
4
2
|
# object_inspector gem.
|
5
3
|
module ObjectInspector
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
4
|
+
@@use_string_inquirers = nil
|
5
|
+
|
6
|
+
def self.use_string_inquirers?
|
7
|
+
if @@use_string_inquirers.nil?
|
8
|
+
@@use_string_inquirers = !!defined?(ActiveSupport::StringInquirer)
|
9
|
+
else
|
10
|
+
@@use_string_inquirers
|
11
|
+
end
|
12
|
+
end
|
11
13
|
end
|
14
|
+
|
15
|
+
require "object_inspector/version"
|
16
|
+
require "object_inspector/inspector"
|
17
|
+
require "object_inspector/inspectors_helper"
|
18
|
+
require "object_inspector/object_interrogator"
|
19
|
+
require "object_inspector/formatters/base_formatter"
|
20
|
+
require "object_inspector/formatters/combining_formatter"
|
21
|
+
require "object_inspector/formatters/templating_formatter"
|
data/object_inspector.gemspec
CHANGED
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
|
|
10
10
|
spec.email = ["paul.dobbins@icloud.com"]
|
11
11
|
|
12
12
|
spec.summary = %q{ObjectInspector generates uniformly formatted inspect output with customizable amounts of detail.}
|
13
|
-
spec.description = %q{ObjectInspector takes Object#inspect to the next level. Specify any combination of identification attributes, flags, info, and/or a name along with
|
13
|
+
spec.description = %q{ObjectInspector takes Object#inspect to the next level. Specify any combination of identification attributes, flags, info, and/or a name along with an optional self-definable scope option to represent an object in the console, in logging, etc.}
|
14
14
|
spec.homepage = "https://github.com/pdobb/object_inspector"
|
15
15
|
spec.license = "MIT"
|
16
16
|
|
@@ -37,4 +37,7 @@ Gem::Specification.new do |spec|
|
|
37
37
|
spec.add_development_dependency "byebug", "~> 10.0"
|
38
38
|
spec.add_development_dependency "pry", "~> 0.11"
|
39
39
|
spec.add_development_dependency "pry-byebug", "~> 3.6"
|
40
|
+
spec.add_development_dependency "benchmark-ips", "~> 2.7"
|
41
|
+
spec.add_development_dependency "minitest-reporters", "~> 1.2"
|
42
|
+
# spec.add_development_dependency "object_identifier", "~> 0.1" # Add when new version is released
|
40
43
|
end
|
@@ -0,0 +1,118 @@
|
|
1
|
+
# Play from the console with:
|
2
|
+
# play scripts/benchmarking/formatters.rb
|
3
|
+
|
4
|
+
require "ostruct"
|
5
|
+
require "benchmark/ips"
|
6
|
+
|
7
|
+
custom_formatter_klasses ||= []
|
8
|
+
|
9
|
+
formatter_klasses = [
|
10
|
+
ObjectInspector::TemplatingFormatter,
|
11
|
+
ObjectInspector::CombiningFormatter,
|
12
|
+
*Array(custom_formatter_klasses),
|
13
|
+
]
|
14
|
+
|
15
|
+
INSPECTOR_WITH_FLAGS_AND_INFO_AND_NAME ||=
|
16
|
+
OpenStruct.new(
|
17
|
+
identification: "IDENTIFICATION",
|
18
|
+
flags: "FLAG1 | FLAG2",
|
19
|
+
info: "INFO",
|
20
|
+
name: "NAME")
|
21
|
+
INSPECTOR_WITH_FLAGS_AND_INFO ||=
|
22
|
+
OpenStruct.new(
|
23
|
+
identification: "IDENTIFICATION",
|
24
|
+
flags: "FLAG1 | FLAG2",
|
25
|
+
info: "INFO")
|
26
|
+
INSPECTOR_WITH_FLAGS_AND_NAME ||=
|
27
|
+
OpenStruct.new(
|
28
|
+
identification: "IDENTIFICATION",
|
29
|
+
flags: "FLAG1 | FLAG2",
|
30
|
+
name: "NAME")
|
31
|
+
INSPECTOR_WITH_INFO_AND_NAME ||=
|
32
|
+
OpenStruct.new(
|
33
|
+
identification: "IDENTIFICATION",
|
34
|
+
info: "INFO",
|
35
|
+
name: "NAME")
|
36
|
+
INSPECTOR_WITH_NAME ||=
|
37
|
+
OpenStruct.new(
|
38
|
+
identification: "IDENTIFICATION",
|
39
|
+
name: "NAME")
|
40
|
+
INSPECTOR_WITH_FLAGS ||=
|
41
|
+
OpenStruct.new(
|
42
|
+
identification: "IDENTIFICATION",
|
43
|
+
flags: "FLAG1 | FLAG2")
|
44
|
+
INSPECTOR_WITH_INFO ||=
|
45
|
+
OpenStruct.new(
|
46
|
+
identification: "IDENTIFICATION",
|
47
|
+
info: "INFO")
|
48
|
+
INSPECTOR_WITH_BASE ||=
|
49
|
+
OpenStruct.new(
|
50
|
+
identification: "IDENTIFICATION")
|
51
|
+
|
52
|
+
|
53
|
+
puts "== Averaged ============================================================="
|
54
|
+
Benchmark.ips { |x|
|
55
|
+
formatter_klasses.each do |formatter_klass|
|
56
|
+
x.report(formatter_klass) {
|
57
|
+
formatter_klass.new(INSPECTOR_WITH_FLAGS_AND_INFO_AND_NAME).call
|
58
|
+
formatter_klass.new(INSPECTOR_WITH_FLAGS_AND_INFO).call
|
59
|
+
formatter_klass.new(INSPECTOR_WITH_FLAGS_AND_NAME).call
|
60
|
+
formatter_klass.new(INSPECTOR_WITH_INFO_AND_NAME).call
|
61
|
+
formatter_klass.new(INSPECTOR_WITH_NAME).call
|
62
|
+
formatter_klass.new(INSPECTOR_WITH_FLAGS).call
|
63
|
+
formatter_klass.new(INSPECTOR_WITH_INFO).call
|
64
|
+
formatter_klass.new(INSPECTOR_WITH_BASE).call
|
65
|
+
}
|
66
|
+
end
|
67
|
+
|
68
|
+
x.compare!
|
69
|
+
};
|
70
|
+
puts "== Done"
|
71
|
+
|
72
|
+
|
73
|
+
puts "== Individualized ======================================================="
|
74
|
+
Benchmark.ips { |x|
|
75
|
+
formatter_klasses.each do |formatter_klass|
|
76
|
+
x.report("#{formatter_klass} - Flags and Info and Name") {
|
77
|
+
formatter_klass.new(INSPECTOR_WITH_FLAGS_AND_INFO_AND_NAME).call
|
78
|
+
}
|
79
|
+
end
|
80
|
+
formatter_klasses.each do |formatter_klass|
|
81
|
+
x.report("#{formatter_klass} - Flags and Info") {
|
82
|
+
formatter_klass.new(INSPECTOR_WITH_FLAGS_AND_INFO).call
|
83
|
+
}
|
84
|
+
end
|
85
|
+
formatter_klasses.each do |formatter_klass|
|
86
|
+
x.report("#{formatter_klass} - Flags and Name") {
|
87
|
+
formatter_klass.new(INSPECTOR_WITH_FLAGS_AND_NAME).call
|
88
|
+
}
|
89
|
+
end
|
90
|
+
formatter_klasses.each do |formatter_klass|
|
91
|
+
x.report("#{formatter_klass} - Info and Name") {
|
92
|
+
formatter_klass.new(INSPECTOR_WITH_INFO_AND_NAME).call
|
93
|
+
}
|
94
|
+
end
|
95
|
+
formatter_klasses.each do |formatter_klass|
|
96
|
+
x.report("#{formatter_klass} - Name") {
|
97
|
+
formatter_klass.new(INSPECTOR_WITH_NAME).call
|
98
|
+
}
|
99
|
+
end
|
100
|
+
formatter_klasses.each do |formatter_klass|
|
101
|
+
x.report("#{formatter_klass} - Flags") {
|
102
|
+
formatter_klass.new(INSPECTOR_WITH_FLAGS).call
|
103
|
+
}
|
104
|
+
end
|
105
|
+
formatter_klasses.each do |formatter_klass|
|
106
|
+
x.report("#{formatter_klass} - Info") {
|
107
|
+
formatter_klass.new(INSPECTOR_WITH_INFO).call
|
108
|
+
}
|
109
|
+
end
|
110
|
+
formatter_klasses.each do |formatter_klass|
|
111
|
+
x.report("#{formatter_klass} - Base") {
|
112
|
+
formatter_klass.new(INSPECTOR_WITH_BASE).call
|
113
|
+
}
|
114
|
+
end
|
115
|
+
|
116
|
+
x.compare!
|
117
|
+
};
|
118
|
+
puts "== Done"
|
@@ -0,0 +1,73 @@
|
|
1
|
+
# Play from the console with:
|
2
|
+
# play scripts/benchmarking/object_inspector.rb
|
3
|
+
|
4
|
+
require "ostruct"
|
5
|
+
require "benchmark/ips"
|
6
|
+
|
7
|
+
inspector_klass = ObjectInspector::Inspector
|
8
|
+
|
9
|
+
OBJECT_WITH_FLAGS_AND_INFO_AND_NAME ||=
|
10
|
+
OpenStruct.new(
|
11
|
+
identification: "IDENTIFICATION",
|
12
|
+
flags: "FLAG1 | FLAG2",
|
13
|
+
info: "INFO",
|
14
|
+
name: "NAME")
|
15
|
+
OBJECT_WITH_FLAGS_AND_INFO ||=
|
16
|
+
OpenStruct.new(
|
17
|
+
identification: "IDENTIFICATION",
|
18
|
+
flags: "FLAG1 | FLAG2",
|
19
|
+
info: "INFO")
|
20
|
+
OBJECT_WITH_FLAGS_AND_NAME ||=
|
21
|
+
OpenStruct.new(
|
22
|
+
identification: "IDENTIFICATION",
|
23
|
+
flags: "FLAG1 | FLAG2",
|
24
|
+
name: "NAME")
|
25
|
+
OBJECT_WITH_INFO_AND_NAME ||=
|
26
|
+
OpenStruct.new(
|
27
|
+
identification: "IDENTIFICATION",
|
28
|
+
info: "INFO",
|
29
|
+
name: "NAME")
|
30
|
+
OBJECT_WITH_NAME ||=
|
31
|
+
OpenStruct.new(
|
32
|
+
identification: "IDENTIFICATION",
|
33
|
+
name: "NAME")
|
34
|
+
OBJECT_WITH_FLAGS ||=
|
35
|
+
OpenStruct.new(
|
36
|
+
identification: "IDENTIFICATION",
|
37
|
+
flags: "FLAG1 | FLAG2")
|
38
|
+
OBJECT_WITH_INFO ||=
|
39
|
+
OpenStruct.new(
|
40
|
+
identification: "IDENTIFICATION",
|
41
|
+
info: "INFO")
|
42
|
+
OBJECT_WITH_BASE ||=
|
43
|
+
OpenStruct.new(
|
44
|
+
identification: "IDENTIFICATION")
|
45
|
+
|
46
|
+
|
47
|
+
puts "== Averaged ============================================================="
|
48
|
+
Benchmark.ips { |x|
|
49
|
+
x.report(inspector_klass) {
|
50
|
+
inspector_klass.inspect(OBJECT_WITH_FLAGS_AND_INFO_AND_NAME)
|
51
|
+
inspector_klass.inspect(OBJECT_WITH_FLAGS_AND_INFO)
|
52
|
+
inspector_klass.inspect(OBJECT_WITH_FLAGS_AND_NAME)
|
53
|
+
inspector_klass.inspect(OBJECT_WITH_INFO_AND_NAME)
|
54
|
+
inspector_klass.inspect(OBJECT_WITH_NAME)
|
55
|
+
inspector_klass.inspect(OBJECT_WITH_FLAGS)
|
56
|
+
inspector_klass.inspect(OBJECT_WITH_INFO)
|
57
|
+
inspector_klass.inspect(OBJECT_WITH_BASE)
|
58
|
+
}
|
59
|
+
|
60
|
+
x.report("Ruby") {
|
61
|
+
OBJECT_WITH_FLAGS_AND_INFO_AND_NAME.inspect
|
62
|
+
OBJECT_WITH_FLAGS_AND_INFO.inspect
|
63
|
+
OBJECT_WITH_FLAGS_AND_NAME.inspect
|
64
|
+
OBJECT_WITH_INFO_AND_NAME.inspect
|
65
|
+
OBJECT_WITH_NAME.inspect
|
66
|
+
OBJECT_WITH_FLAGS.inspect
|
67
|
+
OBJECT_WITH_INFO.inspect
|
68
|
+
OBJECT_WITH_BASE.inspect
|
69
|
+
}
|
70
|
+
|
71
|
+
x.compare!
|
72
|
+
};
|
73
|
+
puts "== Done"
|
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.2.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: 2018-04-
|
11
|
+
date: 2018-04-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -108,9 +108,37 @@ dependencies:
|
|
108
108
|
- - "~>"
|
109
109
|
- !ruby/object:Gem::Version
|
110
110
|
version: '3.6'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: benchmark-ips
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - "~>"
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '2.7'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - "~>"
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '2.7'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: minitest-reporters
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - "~>"
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '1.2'
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - "~>"
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '1.2'
|
111
139
|
description: ObjectInspector takes Object#inspect to the next level. Specify any combination
|
112
|
-
of identification attributes, flags, info, and/or a name along with
|
113
|
-
scope option to represent an object in the console, in logging,
|
140
|
+
of identification attributes, flags, info, and/or a name along with an optional
|
141
|
+
self-definable scope option to represent an object in the console, in logging, etc.
|
114
142
|
email:
|
115
143
|
- paul.dobbins@icloud.com
|
116
144
|
executables: []
|
@@ -128,13 +156,16 @@ files:
|
|
128
156
|
- bin/console
|
129
157
|
- bin/setup
|
130
158
|
- lib/object_inspector.rb
|
131
|
-
- lib/object_inspector/base_formatter.rb
|
132
|
-
- lib/object_inspector/
|
159
|
+
- lib/object_inspector/formatters/base_formatter.rb
|
160
|
+
- lib/object_inspector/formatters/combining_formatter.rb
|
161
|
+
- lib/object_inspector/formatters/templating_formatter.rb
|
133
162
|
- lib/object_inspector/inspector.rb
|
134
163
|
- lib/object_inspector/inspectors_helper.rb
|
135
164
|
- lib/object_inspector/object_interrogator.rb
|
136
165
|
- lib/object_inspector/version.rb
|
137
166
|
- object_inspector.gemspec
|
167
|
+
- scripts/benchmarking/formatters.rb
|
168
|
+
- scripts/benchmarking/object_inspector.rb
|
138
169
|
homepage: https://github.com/pdobb/object_inspector
|
139
170
|
licenses:
|
140
171
|
- MIT
|
@@ -1,33 +0,0 @@
|
|
1
|
-
module ObjectInspector
|
2
|
-
# ObjectInspector::DefaultFormatter implements
|
3
|
-
# {ObjectInspector::BaseFormatter} to return a standard/default inspect output
|
4
|
-
# format.
|
5
|
-
#
|
6
|
-
# @attr (see BaseFormatter)
|
7
|
-
class DefaultFormatter < BaseFormatter
|
8
|
-
# Perform the formatting routine.
|
9
|
-
#
|
10
|
-
# @return [String]
|
11
|
-
def call
|
12
|
-
"<#{combine_strings}>"
|
13
|
-
end
|
14
|
-
|
15
|
-
private
|
16
|
-
|
17
|
-
def build_identification_string(identification = self.identification)
|
18
|
-
identification.to_s
|
19
|
-
end
|
20
|
-
|
21
|
-
def build_flags_string(flags = self.flags)
|
22
|
-
"[#{flags.to_s.upcase}]" if flags
|
23
|
-
end
|
24
|
-
|
25
|
-
def build_info_string(info = self.info)
|
26
|
-
" (#{info})" if info
|
27
|
-
end
|
28
|
-
|
29
|
-
def build_name_string(name = self.name)
|
30
|
-
" :: #{name}" if name
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|