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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0ce8aec7e092dd069c72397d26c799c103b45af8c784e92355dc8f3f267f8f2b
4
- data.tar.gz: a81699623cc8cb8bc532df00b7b9cf6b1d60d6f3620d9a2887bedf217be19ee7
3
+ metadata.gz: c5bfaac0270b232c17c91495180cf29e5a4538c1010e636cf681832d6668ba8c
4
+ data.tar.gz: fd5e4add5c7a5c7d971ec22a4d5ba80e78642d4df9e060c6382b6b7a93e67551
5
5
  SHA512:
6
- metadata.gz: 7b1b091814bb2188953da2d9dbc9ee513c5092f84e7c21339daac97ee1accb0afa5dd1259424fdf9d0ac9770b63a3219bbc196976e670167e679a827dd61e8e5
7
- data.tar.gz: f7a2eac925594a69a2e4ab7faec46ccd0f719a5773a8870c60868e9556b077fa7ae0c00f1943b1ba82e4bb4b7f4d59c0bf9093e2da4c895434f8c3e276749251
6
+ metadata.gz: 0b31e59f84e625d766309ba1dd760a87bba5b70007214aeec3dd3f47c44e64446db34c640a5a9c35923881178eb61c98080138d542ba1fe1b632a93d1269b4ee
7
+ data.tar.gz: 777ee932d2e9fa0c0d4c013dd9f0c8713956afafd883ce866fccb93f23b25fa8eaddb729b6035fd74c514bf9865f40bca09cb06fc2153a4f927458b7dbdbad45
data/.rubocop.yml CHANGED
@@ -98,6 +98,11 @@ Style/Alias:
98
98
  Style/BlockDelimiters:
99
99
  Enabled: false # Reconsider later.
100
100
 
101
+ Style/ClassAndModuleChildren:
102
+ AutoCorrect: true
103
+ Exclude:
104
+ - "test/**/*"
105
+
101
106
  Style/CollectionMethods:
102
107
  Enabled: true
103
108
  PreferredMethods:
data/.travis.yml CHANGED
@@ -4,11 +4,10 @@ env:
4
4
  sudo: false
5
5
  language: ruby
6
6
  rvm:
7
- - 2.2.10
8
- - 2.3.7
9
- - 2.4.4
10
- - 2.5.3
11
- - 2.6.1
7
+ - 2.3
8
+ - 2.4
9
+ - 2.5
10
+ - 2.6
12
11
  - ruby-head
13
12
  notifications:
14
13
  email: false
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ ### 0.6.0 - 2019-03-13
2
+ - Fix inspection of delegating wrapper objects
3
+
4
+
1
5
  ### 0.5.2 - 2019-02-24
2
6
  - Automatically compact nils in join_* methods
3
7
 
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- object_inspector (0.5.2)
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.1.1)
39
- parallel (1.13.0)
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 as:
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.2.10
36
- * 2.3.7
37
- * 2.4.4
38
- * 2.5.3
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 {#object}
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 {#object} class name and
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 {#object}.
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 {#object}.
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 {#object}.
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
- # The generally human-friendly unique identifier for {#object}.
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
- value(key: :name) ||
100
- interrogate_object(method_name: :display_name,
101
- kargs: object_method_keyword_arguments)
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 {#object} responds to
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 {#object}
117
+ # @return [NilClass] if not found in {#kargs} or {@object}
113
118
  def value(key:)
114
119
  return_value =
115
- if (passed_in_value = kargs[key])
116
- evaluate_passed_in_value(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 {#object} if it responds to it and the result is not nil,
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 {#object} responds to `value` and if the call result
132
+ # @return [#to_s] if {@object} responds to `value` and if the call result
128
133
  # isn't nil
129
- # @return [#nil] if {#object} doesn't respond to `value` or if the call
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 {#object} based on the passed in `name`.
144
+ # Attempt to call `inspect_*` on {@object} based on the passed in `name`.
140
145
  #
141
- # @return [String] if {#object} responds to
146
+ # @return [String] if {@object} responds to
142
147
  # `#{object_inspet_method_name}` (e.g. `inspect_flags`)
143
- # @return [NilClass] if not found on {#object}
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 {#object} to return
5
- # Object#{#method_name} if {#object} responds to the method.
4
+ # ObjectInspector::ObjectInterrogator collaborates with {@object} to return
5
+ # Object#{@method_name} if {@object} responds to the method.
6
6
  #
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.
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.send(method_name)
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.send(method_name, **kargs)
35
+ @object.__send__(@method_name, **@kargs)
38
36
  rescue ArgumentError
39
- object.send(method_name)
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ObjectInspector
4
- VERSION = "0.5.2"
4
+ VERSION = "0.6.0"
5
5
  end
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.5.2
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-02-24 00:00:00.000000000 Z
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.2
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