object_inspector 0.3.1 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/Gemfile.lock +1 -1
- data/README.md +51 -37
- data/lib/object_inspector.rb +6 -0
- data/lib/object_inspector/formatters/base_formatter.rb +4 -3
- data/lib/object_inspector/formatters/combining_formatter.rb +2 -2
- data/lib/object_inspector/formatters/templating_formatter.rb +2 -2
- data/lib/object_inspector/inspector.rb +2 -2
- data/lib/object_inspector/scope.rb +25 -1
- data/lib/object_inspector/version.rb +1 -1
- data/object_inspector.gemspec +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: 8f01a078cf1fd2426c6b8db14983ded1ef0be16abd1e0e58c9716945e5fb3f75
|
4
|
+
data.tar.gz: c2d17d5b4cec6f97798c943e600a64136d4bfdb231c6ae1c14b5bd3179a09142
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b96dcc1132c1fe41b7794f25377f9809bf574c93db37a9f4b178f20bf7d563c515cda8400ea774324ac67446db1a8c374601bbef2c541ab2a3bde8dfb2b67993
|
7
|
+
data.tar.gz: bbefc6245702d58e25ea210a53530577f1943f0cf646012c8d27f9e068e5970748e52b1e41ce1878fe19b2caf3992ac0a8af5d7bb2cccb470ec6a354a4c5111d
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,8 @@
|
|
1
|
+
### 0.4.0 - 2018-05-25
|
2
|
+
- Feature: Add ObjectInspector::Configuration#default_scope setting -- can be used to override the default Scope for object inspection.
|
3
|
+
- Implement ObjectInspector::Scope#== for comparing scopes with scopes and/or scopes with (Arrays of) Strings, Symbols, etc.
|
4
|
+
|
5
|
+
|
1
6
|
### 0.3.1 - 2018-04-15
|
2
7
|
- Add ObjectInspector::Configuration#formatter_class setting for overriding the default Formatter.
|
3
8
|
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -7,7 +7,7 @@
|
|
7
7
|
|
8
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 represents objects. Great for the console, logging, etc.
|
9
9
|
|
10
|
-
Because object inspection code should be easy to
|
10
|
+
Because object inspection code should be uniform, easy to build, and its output should be easy to read!
|
11
11
|
|
12
12
|
If you'd like to just jump into an example: [Full Example](#full-example).
|
13
13
|
|
@@ -38,6 +38,8 @@ Tested MRI Ruby Versions:
|
|
38
38
|
* 2.5.1
|
39
39
|
* edge
|
40
40
|
|
41
|
+
ObjectInspector has no other dependencies.
|
42
|
+
|
41
43
|
|
42
44
|
## Configuration
|
43
45
|
|
@@ -50,6 +52,7 @@ _Note: In a Rails app, the following would go in e.g. `config/initializers/objec
|
|
50
52
|
ObjectInspector.configure do |config|
|
51
53
|
config.formatter_class = ObjectInspector::TemplatingFormatter
|
52
54
|
config.inspect_method_prefix = "inspect"
|
55
|
+
config.default_scope = ObjectInspector::Scope.new(:self)
|
53
56
|
config.wild_card_scope = "all"
|
54
57
|
config.out_of_scope_placeholder = "*"
|
55
58
|
config.flags_separator = " / "
|
@@ -77,7 +80,7 @@ See also [Helper Usage](#helper-usage) for an even simpler usage option.
|
|
77
80
|
|
78
81
|
### Output Customization
|
79
82
|
|
80
|
-
Use the `identification`, `flags`, `info`, and `name` options to customize inspect output.
|
83
|
+
Use the `identification`, `flags`, `info`, and/or `name` options to customize inspect output.
|
81
84
|
|
82
85
|
```ruby
|
83
86
|
class MyObject
|
@@ -296,9 +299,6 @@ end
|
|
296
299
|
|
297
300
|
my_object = MyObject.new("Name")
|
298
301
|
|
299
|
-
my_object.inspect
|
300
|
-
# => "<MyObject[a2:2](DEFAULT_FLAG / *) Default Info | * :: Name>"
|
301
|
-
|
302
302
|
my_object.inspect(scope: :complex)
|
303
303
|
# => "<MyObject[a2:2](DEFAULT_FLAG / *) Default Info | Complex Info | * :: Name>"
|
304
304
|
|
@@ -310,6 +310,20 @@ my_object.inspect(scope: %i[self complex verbose])
|
|
310
310
|
|
311
311
|
my_object.inspect(scope: :all)
|
312
312
|
# => "<MyObject[a2:2](DEFAULT_FLAG / AO1_FLAG1 / AO2_FLAG1) Default Info | Complex Info | Verbose Info :: Name>"
|
313
|
+
|
314
|
+
my_object.inspect
|
315
|
+
# => "<MyObject[a2:2](DEFAULT_FLAG / *) Default Info | * :: Name>"
|
316
|
+
|
317
|
+
ObjectInspector.configuration.default_scope = :complex
|
318
|
+
my_object.inspect
|
319
|
+
# => "<MyObject[a2:2](DEFAULT_FLAG / *) Default Info | Complex Info | * :: Name>"
|
320
|
+
|
321
|
+
ObjectInspector.configuration.default_scope = %i[self complex verbose]
|
322
|
+
my_object.inspect
|
323
|
+
|
324
|
+
ObjectInspector.configuration.default_scope = :all
|
325
|
+
my_object.inspect
|
326
|
+
# => "<MyObject[a2:2](DEFAULT_FLAG / AO1_FLAG1 / AO2_FLAG1) Default Info | Complex Info | Verbose Info :: Name>"
|
313
327
|
```
|
314
328
|
|
315
329
|
|
@@ -398,6 +412,38 @@ See examples:
|
|
398
412
|
- [ObjectInspector::CombiningFormatter]
|
399
413
|
|
400
414
|
|
415
|
+
## Supporting Gems
|
416
|
+
|
417
|
+
ObjectInspector works great with the [ObjectIdentifier](https://github.com/pdobb/object_identifier) gem.
|
418
|
+
|
419
|
+
```ruby
|
420
|
+
class MyObject
|
421
|
+
include ObjectInspector::InspectorsHelper
|
422
|
+
|
423
|
+
def my_method1
|
424
|
+
1
|
425
|
+
end
|
426
|
+
|
427
|
+
def my_method2
|
428
|
+
2
|
429
|
+
end
|
430
|
+
|
431
|
+
private
|
432
|
+
|
433
|
+
def inspect_identification
|
434
|
+
identify(:my_method1, :my_method2)
|
435
|
+
end
|
436
|
+
|
437
|
+
def inspect_flags; "FLAG1 / FLAG2" end
|
438
|
+
def inspect_info; "INFO" end
|
439
|
+
def inspect_name; "NAME" end
|
440
|
+
end
|
441
|
+
|
442
|
+
MyObject.new.inspect
|
443
|
+
# => "<MyObject[my_method1:1, my_method2:2](FLAG1 / FLAG2) INFO :: NAME>"
|
444
|
+
```
|
445
|
+
|
446
|
+
|
401
447
|
## Performance
|
402
448
|
|
403
449
|
### Benchmarking ObjectInspector
|
@@ -453,38 +499,6 @@ play scripts/benchmarking/formatters.rb
|
|
453
499
|
```
|
454
500
|
|
455
501
|
|
456
|
-
## Supporting Gems
|
457
|
-
|
458
|
-
ObjectInspector works great with the [ObjectIdentifier](https://github.com/pdobb/object_identifier) gem.
|
459
|
-
|
460
|
-
```ruby
|
461
|
-
class MyObject
|
462
|
-
include ObjectInspector::InspectorsHelper
|
463
|
-
|
464
|
-
def my_method1
|
465
|
-
1
|
466
|
-
end
|
467
|
-
|
468
|
-
def my_method2
|
469
|
-
2
|
470
|
-
end
|
471
|
-
|
472
|
-
private
|
473
|
-
|
474
|
-
def inspect_identification
|
475
|
-
identify(:my_method1, :my_method2)
|
476
|
-
end
|
477
|
-
|
478
|
-
def inspect_flags; "FLAG1 / FLAG2" end
|
479
|
-
def inspect_info; "INFO" end
|
480
|
-
def inspect_name; "NAME" end
|
481
|
-
end
|
482
|
-
|
483
|
-
MyObject.new.inspect
|
484
|
-
# => "<MyObject[my_method1:1, my_method2:2](FLAG1 / FLAG2) INFO :: NAME>"
|
485
|
-
```
|
486
|
-
|
487
|
-
|
488
502
|
## Development
|
489
503
|
|
490
504
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
data/lib/object_inspector.rb
CHANGED
@@ -20,6 +20,7 @@ module ObjectInspector
|
|
20
20
|
class Configuration
|
21
21
|
attr_reader :formatter_class,
|
22
22
|
:inspect_method_prefix,
|
23
|
+
:default_scope,
|
23
24
|
:wild_card_scope,
|
24
25
|
:out_of_scope_placeholder,
|
25
26
|
:flags_separator,
|
@@ -28,6 +29,7 @@ module ObjectInspector
|
|
28
29
|
def initialize
|
29
30
|
@formatter_class = TemplatingFormatter
|
30
31
|
@inspect_method_prefix = "inspect".freeze
|
32
|
+
@default_scope = Scope.new(:self)
|
31
33
|
@wild_card_scope = "all".freeze
|
32
34
|
@out_of_scope_placeholder = "*".freeze
|
33
35
|
@flags_separator = " / ".freeze
|
@@ -46,6 +48,10 @@ module ObjectInspector
|
|
46
48
|
@inspect_method_prefix = value.to_s.freeze
|
47
49
|
end
|
48
50
|
|
51
|
+
def default_scope=(value)
|
52
|
+
@default_scope = Conversions.Scope(value)
|
53
|
+
end
|
54
|
+
|
49
55
|
def wild_card_scope=(value)
|
50
56
|
@wild_card_scope = value.to_s.freeze
|
51
57
|
end
|
@@ -21,12 +21,13 @@ module ObjectInspector
|
|
21
21
|
raise NotImplementedError
|
22
22
|
end
|
23
23
|
|
24
|
-
# Delegates to {Inspector#
|
24
|
+
# Delegates to {Inspector#wrapped_object_inspection_result}.
|
25
25
|
#
|
26
26
|
# @return [String] if given
|
27
27
|
# @return [NilClass] if not given
|
28
|
-
def
|
29
|
-
@
|
28
|
+
def wrapped_object_inspection_result
|
29
|
+
@wrapped_object_inspection_result ||=
|
30
|
+
inspector.wrapped_object_inspection_result
|
30
31
|
end
|
31
32
|
|
32
33
|
# Delegates to {Inspector#identification}.
|
@@ -9,7 +9,7 @@ module ObjectInspector
|
|
9
9
|
#
|
10
10
|
# @return [String]
|
11
11
|
def call
|
12
|
-
if
|
12
|
+
if wrapped_object_inspection_result
|
13
13
|
build_wrapped_object_string
|
14
14
|
else
|
15
15
|
build_string
|
@@ -19,7 +19,7 @@ module ObjectInspector
|
|
19
19
|
private
|
20
20
|
|
21
21
|
def build_wrapped_object_string
|
22
|
-
"#{build_string} #{RIGHT_ARROW_ICON} #{
|
22
|
+
"#{build_string} #{RIGHT_ARROW_ICON} #{wrapped_object_inspection_result}"
|
23
23
|
end
|
24
24
|
|
25
25
|
def build_string
|
@@ -41,7 +41,7 @@ module ObjectInspector
|
|
41
41
|
#
|
42
42
|
# @return [String]
|
43
43
|
def call
|
44
|
-
if
|
44
|
+
if wrapped_object_inspection_result
|
45
45
|
build_wrapped_object_string
|
46
46
|
else
|
47
47
|
build_string
|
@@ -51,7 +51,7 @@ module ObjectInspector
|
|
51
51
|
private
|
52
52
|
|
53
53
|
def build_wrapped_object_string
|
54
|
-
"#{build_string} #{RIGHT_ARROW_ICON} #{
|
54
|
+
"#{build_string} #{RIGHT_ARROW_ICON} #{wrapped_object_inspection_result}"
|
55
55
|
end
|
56
56
|
|
57
57
|
def build_string
|
@@ -28,7 +28,7 @@ module ObjectInspector
|
|
28
28
|
|
29
29
|
def initialize(
|
30
30
|
object,
|
31
|
-
scope:
|
31
|
+
scope: ObjectInspector.configuration.default_scope,
|
32
32
|
formatter: ObjectInspector.configuration.formatter_class,
|
33
33
|
**kargs)
|
34
34
|
@object = object
|
@@ -48,7 +48,7 @@ module ObjectInspector
|
|
48
48
|
#
|
49
49
|
# @return [String] if {#object_is_a_wrapper}
|
50
50
|
# @return [NilClass] if not {#object_is_a_wrapper}
|
51
|
-
def
|
51
|
+
def wrapped_object_inspection_result
|
52
52
|
if object_is_a_wrapper?
|
53
53
|
self.class.inspect(
|
54
54
|
extract_wrapped_object,
|
@@ -3,6 +3,11 @@ module ObjectInspector
|
|
3
3
|
# responds with `true`. This is a prettier way to test for a given type of
|
4
4
|
# "scope" within objects.
|
5
5
|
#
|
6
|
+
# It is possible to pass in multiple scope names to match on.
|
7
|
+
# `:all` is a "wild card" scope name, and will match on all scope names.
|
8
|
+
# Passing a block to a scope predicate falls back to the out-of-scope
|
9
|
+
# placeholder (`*` by default) if the scope does not match.
|
10
|
+
#
|
6
11
|
# @see ActiveSupport::StringInquirer
|
7
12
|
# http://api.rubyonrails.org/classes/ActiveSupport/StringInquirer.html
|
8
13
|
#
|
@@ -32,6 +37,25 @@ module ObjectInspector
|
|
32
37
|
Array(items).join(separator)
|
33
38
|
end
|
34
39
|
|
40
|
+
# Compare self with the passed in object.
|
41
|
+
#
|
42
|
+
# @return [TrueClass] if self and `other` resolve to the same set of objects
|
43
|
+
# @return [FalseClass] if self and `other` resolve to a different set of
|
44
|
+
# objects
|
45
|
+
def ==(other)
|
46
|
+
names.sort ==
|
47
|
+
Array(other).map(&:to_s).sort
|
48
|
+
end
|
49
|
+
alias_method :eql?, :==
|
50
|
+
|
51
|
+
def to_s(separator: ", ".freeze)
|
52
|
+
to_a.join(separator)
|
53
|
+
end
|
54
|
+
|
55
|
+
def to_a
|
56
|
+
names
|
57
|
+
end
|
58
|
+
|
35
59
|
private
|
36
60
|
|
37
61
|
def method_missing(method_name, *args, &block)
|
@@ -55,7 +79,7 @@ module ObjectInspector
|
|
55
79
|
|
56
80
|
def evaluate_block_if(condition, &block)
|
57
81
|
if condition
|
58
|
-
block.call
|
82
|
+
block.call(self)
|
59
83
|
else
|
60
84
|
ObjectInspector.configuration.out_of_scope_placeholder
|
61
85
|
end
|
data/object_inspector.gemspec
CHANGED
@@ -9,7 +9,7 @@ Gem::Specification.new do |spec|
|
|
9
9
|
spec.authors = ["Paul Dobbins"]
|
10
10
|
spec.email = ["paul.dobbins@icloud.com"]
|
11
11
|
|
12
|
-
spec.summary = %q{ObjectInspector
|
12
|
+
spec.summary = %q{ObjectInspector builds uniformly formatted inspect output with customizable amounts of detail.}
|
13
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 represents objects. Great for the console, logging, etc.}
|
14
14
|
spec.homepage = "https://github.com/pdobb/object_inspector"
|
15
15
|
spec.license = "MIT"
|
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.4.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-
|
11
|
+
date: 2018-05-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -206,6 +206,6 @@ rubyforge_project:
|
|
206
206
|
rubygems_version: 2.7.6
|
207
207
|
signing_key:
|
208
208
|
specification_version: 4
|
209
|
-
summary: ObjectInspector
|
209
|
+
summary: ObjectInspector builds uniformly formatted inspect output with customizable
|
210
210
|
amounts of detail.
|
211
211
|
test_files: []
|