object_inspector 0.6.2 → 0.6.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,188 +1,192 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module ObjectInspector
4
- # ObjectInspector::Inspector organizes inspection of the associated {@object}
5
- # via the passed in options and via a {ObjectInspector::BaseFormatter}
6
- # instance.
7
- class Inspector
8
- attr_reader :object
9
-
10
- # Shortcuts the instantiation -> {#to_s} flow that would normally be
11
- # required to use ObjectInspector::Inspector.
12
- #
13
- # @return [String]
14
- def self.inspect(object, **kargs)
15
- new(object, **kargs).to_s
16
- end
3
+ # ObjectInspector::Inspector organizes inspection of the associated {#object}
4
+ # via the passed in options and via an {ObjectInspector::BaseFormatter}
5
+ # instance.
6
+ #
7
+ # :reek:TooManyMethods
8
+ class ObjectInspector::Inspector
9
+ attr_reader :object
10
+
11
+ # Shortcuts the instantiation -> {#to_s} flow that would normally be
12
+ # required to use ObjectInspector::Inspector.
13
+ #
14
+ # @return [String]
15
+ def self.inspect(...)
16
+ new(...).to_s
17
+ end
17
18
 
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
29
- def initialize(
30
- object,
31
- scope: ObjectInspector.configuration.default_scope,
32
- formatter: ObjectInspector.configuration.formatter_class,
33
- **kargs)
34
- @object = object
35
- @scope = Conversions.Scope(scope)
36
- @formatter_klass = formatter
37
- @kargs = kargs
38
- end
19
+ # @param object [Object] the object being inspected
20
+ # @param scope [Symbol] Object inspection type. For example:
21
+ # :self (default) -- Means: Only interrogate self. Don't visit neighbors.
22
+ # <custom> -- Anything else that makes sense for {#object} to key
23
+ # on
24
+ # @param formatter [ObjectInspector::BaseFormatter]
25
+ # (ObjectInspector.configuration.formatter) the formatter object type
26
+ # to use for formatting the inspect String
27
+ # @param kwargs [Hash] options to be sent to {#object} via the
28
+ # {ObjectInspector::ObjectInterrogator} when calling the `inspect_*`
29
+ # methods
30
+ #
31
+ # :reek:DuplicateMethodCall (ObjectInspecto.configuration)
32
+ def initialize(
33
+ object,
34
+ scope: ObjectInspector.configuration.default_scope,
35
+ formatter: ObjectInspector.configuration.formatter_class,
36
+ **kwargs)
37
+ @object = object
38
+ @scope = ObjectInspector::Conversions.Scope(scope)
39
+ @formatter_klass = formatter
40
+ @kwargs = kwargs
41
+ end
39
42
 
40
- # Generate the formatted inspect String.
41
- #
42
- # @return [String]
43
- def to_s
44
- formatter.call
45
- end
43
+ # Generate the formatted inspect String.
44
+ #
45
+ # @return [String]
46
+ def to_s
47
+ formatter.call
48
+ end
46
49
 
47
- # Generate the inspect String for the wrapped object, if present.
48
- #
49
- # @return [String] if {#object_is_a_wrapper}
50
- # @return [NilClass] if not {#object_is_a_wrapper}
51
- def wrapped_object_inspection_result
52
- return unless object_is_a_wrapper?
53
-
54
- self.class.inspect(
55
- extract_wrapped_object,
56
- scope: @scope,
57
- formatter: @formatter_klass,
58
- kargs: @kargs)
59
- end
50
+ # Generate the inspect String for the wrapped object, if present.
51
+ #
52
+ # @return [String] if {#object_is_a_wrapper?}
53
+ # @return [NilClass] if not {#object_is_a_wrapper?}
54
+ def wrapped_object_inspection_result
55
+ return unless object_is_a_wrapper?
56
+
57
+ self.class.inspect(
58
+ extract_wrapped_object,
59
+ scope: @scope,
60
+ formatter: @formatter_klass,
61
+ kwargs: @kwargs)
62
+ end
60
63
 
61
- # Core object identification details, such as the {@object} class name and
62
- # any core-level attributes.
63
- #
64
- # @return [String]
65
- def identification
66
- (value(key: :identification) || @object.class).to_s
67
- end
64
+ # Core object identification details, such as the {#object} class name and
65
+ # any core-level attributes.
66
+ #
67
+ # @return [String]
68
+ def identification
69
+ (value(key: :identification) || @object.class).to_s
70
+ end
68
71
 
69
- # Boolean flags/states applicable to {@object}.
70
- #
71
- # @return [String] if given
72
- # @return [NilClass] if not given
73
- def flags
74
- value(key: :flags)
75
- end
72
+ # Boolean flags/states applicable to {#object}.
73
+ #
74
+ # @return [String] if given
75
+ # @return [NilClass] if not given
76
+ def flags
77
+ value(key: :flags)
78
+ end
76
79
 
77
- # Issues/Warnings applicable to {@object}.
78
- #
79
- # @return [String] if given
80
- # @return [NilClass] if not given
81
- def issues
82
- value(key: :issues)
83
- end
80
+ # Issues/Warnings applicable to {#object}.
81
+ #
82
+ # @return [String] if given
83
+ # @return [NilClass] if not given
84
+ def issues
85
+ value(key: :issues)
86
+ end
84
87
 
85
- # Informational details applicable to {@object}.
86
- #
87
- # @return [String] if given
88
- # @return [NilClass] if not given
89
- def info
90
- value(key: :info)
91
- end
88
+ # Informational details applicable to {#object}.
89
+ #
90
+ # @return [String] if given
91
+ # @return [NilClass] if not given
92
+ def info
93
+ value(key: :info)
94
+ end
92
95
 
93
- # A human-friendly identifier for {@object}.
94
- #
95
- # @return [String] if given
96
- # @return [NilClass] if not given
97
- def name
98
- key = :name
96
+ # A human-friendly identifier for {#object}.
97
+ #
98
+ # @return [String] if given
99
+ # @return [NilClass] if not given
100
+ def name
101
+ key = :name
99
102
 
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
103
+ if @kwargs.key?(key)
104
+ value(key: key)
105
+ else
106
+ interrogate_object_inspect_method(key) ||
107
+ interrogate_object(method_name: :display_name,
108
+ kwargs: object_method_keyword_arguments)
107
109
  end
110
+ end
108
111
 
109
- private
110
-
111
- def formatter
112
- @formatter_klass.new(self)
113
- end
112
+ private
114
113
 
115
- # @return [String] if `key` is found in {#kargs} or if {@object} responds to
116
- # `#{object_inspet_method_name}` (e.g. `inspect_flags`)
117
- # @return [NilClass] if not found in {#kargs} or {@object}
118
- def value(key:)
119
- return_value =
120
- if @kargs.key?(key)
121
- evaluate_passed_in_value(@kargs[key])
122
- else
123
- interrogate_object_inspect_method(key)
124
- end
125
-
126
- return_value&.to_s
127
- end
114
+ def formatter
115
+ @formatter_klass.new(self)
116
+ end
128
117
 
129
- # Call `value` on {@object} if it responds to it and the result is not nil,
130
- # else just return `value`.
131
- #
132
- # @return [#to_s] if {@object} responds to `value` and if the call result
133
- # isn't nil
134
- # @return [#nil] if {@object} doesn't respond to `value` or if the call
135
- # result is nil
136
- def evaluate_passed_in_value(value)
137
- if value.is_a?(Symbol)
138
- interrogate_object(method_name: value) || value
118
+ # @return [String] if `key` is found in {#kwargs} or if {#object} responds to
119
+ # `#{object_inspection_method_name}` (e.g. `inspect_flags`)
120
+ # @return [NilClass] if not found in {#kwargs} or {#object}
121
+ def value(key:)
122
+ return_value =
123
+ if @kwargs.key?(key)
124
+ evaluate_passed_in_value(@kwargs[key])
139
125
  else
140
- value
126
+ interrogate_object_inspect_method(key)
141
127
  end
142
- end
143
128
 
144
- # Attempt to call `inspect_*` on {@object} based on the passed in `name`.
145
- #
146
- # @return [String] if {@object} responds to
147
- # `#{object_inspet_method_name}` (e.g. `inspect_flags`)
148
- # @return [NilClass] if not found on {@object}
149
- def interrogate_object_inspect_method(
150
- name,
151
- prefix: ObjectInspector.configuration.inspect_method_prefix)
152
- interrogate_object(
153
- method_name: object_inspet_method_name(name, prefix: prefix),
154
- kargs: object_method_keyword_arguments)
129
+ return_value&.to_s
130
+ end
131
+
132
+ # Call `value` on {#object} if it responds to it and the result is not nil,
133
+ # else just return `value`.
134
+ #
135
+ # @return [#to_s] if {#object} responds to `value` and if the call result
136
+ # isn't nil
137
+ # @return [#nil] if {#object} doesn't respond to `value` or if the call
138
+ # result is nil
139
+ def evaluate_passed_in_value(value)
140
+ if value.is_a?(Symbol)
141
+ interrogate_object(method_name: value) || value
142
+ else
143
+ value
155
144
  end
145
+ end
156
146
 
157
- def interrogate_object(method_name:, kargs: {})
158
- interrogator =
159
- ObjectInterrogator.new(
160
- object: @object,
161
- method_name: method_name,
162
- kargs: kargs)
147
+ # Attempt to call `inspect_*` on {#object} based on the passed in `name`.
148
+ #
149
+ # @return [String] if {#object} responds to
150
+ # `#{object_inspection_method_name}` (e.g. `inspect_flags`)
151
+ # @return [NilClass] if not found on {#object}
152
+ def interrogate_object_inspect_method(
153
+ name,
154
+ prefix: ObjectInspector.configuration.inspect_method_prefix)
155
+ interrogate_object(
156
+ method_name: object_inspection_method_name(name, prefix: prefix),
157
+ kwargs: object_method_keyword_arguments)
158
+ end
163
159
 
164
- interrogator.call
165
- end
160
+ def interrogate_object(method_name:, kwargs: {})
161
+ interrogator =
162
+ ObjectInspector::ObjectInterrogator.new(
163
+ object: @object,
164
+ method_name: method_name,
165
+ kwargs: kwargs)
166
166
 
167
- def object_inspet_method_name(
168
- name,
169
- prefix: ObjectInspector.configuration.inspect_method_prefix)
170
- "#{prefix}_#{name}"
171
- end
167
+ interrogator.call
168
+ end
172
169
 
173
- def object_method_keyword_arguments
174
- {
175
- scope: @scope
176
- }
177
- end
170
+ # :reek:UtilityFunction
171
+ def object_inspection_method_name(
172
+ name,
173
+ prefix: ObjectInspector.configuration.inspect_method_prefix)
174
+ "#{prefix}_#{name}"
175
+ end
178
176
 
179
- def extract_wrapped_object
180
- @object.to_model
181
- end
177
+ def object_method_keyword_arguments
178
+ {
179
+ scope: @scope,
180
+ }
181
+ end
182
182
 
183
- def object_is_a_wrapper?
184
- @object.respond_to?(:to_model) &&
185
- @object.to_model != @object
186
- end
183
+ def extract_wrapped_object
184
+ @object.to_model
185
+ end
186
+
187
+ # :reek:ManualDispatch
188
+ def object_is_a_wrapper?
189
+ @object.respond_to?(:to_model) &&
190
+ @object.to_model != @object
187
191
  end
188
192
  end
@@ -1,16 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module ObjectInspector
4
- # ObjectInspector::InspectorsHelper can be included into any object to
5
- # simplify the process of instantiating an ObjectInspector::Inspector and
6
- # generating the inspection output.
7
- module InspectorsHelper
8
- # Calls {ObjectInspector::Inspector.inspect} on the passed in `object`,
9
- # passing it the passed in `kargs` (keyword arguments).
10
- #
11
- # @return [String]
12
- def inspect(object = self, **kargs)
13
- Inspector.inspect(object, **kargs)
14
- end
3
+ # ObjectInspector::InspectorsHelper can be included into any object to
4
+ # simplify the process of instantiating an ObjectInspector::Inspector and
5
+ # generating the inspection output.
6
+ module ObjectInspector::InspectorsHelper
7
+ # Calls {ObjectInspector::Inspector.inspect} on the passed in `object`,
8
+ # passing it the passed in `kwargs` (keyword arguments).
9
+ #
10
+ # @return [String]
11
+ def inspect(object = self, **kwargs)
12
+ ObjectInspector::Inspector.inspect(object, **kwargs)
15
13
  end
16
14
  end
@@ -1,44 +1,46 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module ObjectInspector
4
- # ObjectInspector::ObjectInterrogator collaborates with {@object} to return
5
- # Object#{@method_name} if {@object} responds to the method.
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.
9
- class ObjectInterrogator
10
- attr_reader :object
3
+ # ObjectInspector::ObjectInterrogator collaborates with {#object} to return
4
+ # Object#{#method_name} if {#object} responds to the method.
5
+ #
6
+ # If Object#{#method_name} accepts the supplied `kwargs` then they are passed
7
+ # in as well. If not, then any supplied `kwargs` will be ignored.
8
+ class ObjectInspector::ObjectInterrogator
9
+ attr_reader :object,
10
+ :method_name,
11
+ :kwargs
11
12
 
12
- def initialize(object:, method_name:, kargs: {})
13
- @object = object
14
- @method_name = method_name
15
- @kargs = kargs
16
- end
13
+ def initialize(object:, method_name:, kwargs: {})
14
+ @object = object
15
+ @method_name = method_name
16
+ @kwargs = kwargs
17
+ end
17
18
 
18
- # @return [String, ...] whatever type Object#{#method} returns
19
- #
20
- # @raise [ArgumentError] if Object#{#method} has an unexpected method
21
- # signature
22
- def call
23
- return unless object_responds_to_method_name?
19
+ # @return [String, ...] whatever type Object#{#method_name} returns
20
+ #
21
+ # @raise [ArgumentError] if Object#{#method_name} has an unexpected method
22
+ # signature
23
+ def call
24
+ return unless object_responds_to_method_name?
24
25
 
25
- if @object.method(@method_name).arity != 0
26
- call_with_kargs
27
- else
28
- @object.__send__(@method_name)
29
- end
26
+ if object.method(method_name).arity.zero?
27
+ object.__send__(method_name)
28
+ else
29
+ call_with_kwargs
30
30
  end
31
+ end
31
32
 
32
- private
33
+ private
33
34
 
34
- def call_with_kargs
35
- @object.__send__(@method_name, **@kargs)
36
- rescue ArgumentError
37
- @object.__send__(@method_name)
38
- end
35
+ def call_with_kwargs
36
+ object.__send__(method_name, **kwargs)
37
+ rescue ArgumentError
38
+ object.__send__(method_name)
39
+ end
39
40
 
40
- def object_responds_to_method_name?(include_private: true)
41
- @object.respond_to?(@method_name, include_private)
42
- end
41
+ # :reek:ManualDispatch
42
+ # :reek:BooleanParameter
43
+ def object_responds_to_method_name?(include_private: true)
44
+ object.respond_to?(method_name, include_private)
43
45
  end
44
46
  end