object_inspector 0.10.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +230 -157
- data/lib/object_inspector/conversions.rb +12 -4
- data/lib/object_inspector/formatters/base_formatter.rb +20 -21
- data/lib/object_inspector/formatters/combining_formatter.rb +4 -5
- data/lib/object_inspector/formatters/templating_formatter.rb +5 -8
- data/lib/object_inspector/inspect_behaviors.rb +39 -0
- data/lib/object_inspector/inspector.rb +77 -64
- data/lib/object_inspector/{object_interrogator.rb → interrogate_object.rb} +13 -8
- data/lib/object_inspector/scope.rb +45 -16
- data/lib/object_inspector/version.rb +4 -1
- data/lib/object_inspector.rb +60 -16
- metadata +6 -6
- data/lib/object_inspector/inspectors_helper.rb +0 -24
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
#
|
|
4
|
-
#
|
|
5
|
-
# {#
|
|
6
|
-
# "inspect" String.
|
|
3
|
+
# An abstract base class that interfaces with {ObjectInspector::Inspector}
|
|
4
|
+
# objects to combine the supplied {#identification}, {#flags}, {#issues},
|
|
5
|
+
# {#info}, and {#name} Strings into a friendly "inspect" String.
|
|
7
6
|
class ObjectInspector::BaseFormatter
|
|
8
7
|
attr_reader :inspector
|
|
9
8
|
|
|
@@ -21,49 +20,49 @@ class ObjectInspector::BaseFormatter
|
|
|
21
20
|
|
|
22
21
|
# Delegates to {Inspector#wrapped_object_inspection_result}.
|
|
23
22
|
#
|
|
24
|
-
# @return [String]
|
|
25
|
-
# @return [
|
|
23
|
+
# @return [String] If given.
|
|
24
|
+
# @return [nil] If not given.
|
|
26
25
|
def wrapped_object_inspection_result
|
|
27
26
|
@wrapped_object_inspection_result ||=
|
|
28
|
-
|
|
27
|
+
inspector.wrapped_object_inspection_result
|
|
29
28
|
end
|
|
30
29
|
|
|
31
30
|
# Delegates to {Inspector#identification}.
|
|
32
31
|
#
|
|
33
|
-
# @return [String]
|
|
32
|
+
# @return [String] If given.
|
|
34
33
|
def identification
|
|
35
|
-
@identification ||=
|
|
34
|
+
@identification ||= inspector.identification
|
|
36
35
|
end
|
|
37
36
|
|
|
38
37
|
# Delegates to {Inspector#flags}.
|
|
39
38
|
#
|
|
40
|
-
# @return [String]
|
|
41
|
-
# @return [
|
|
39
|
+
# @return [String] If given.
|
|
40
|
+
# @return [nil] If not given.
|
|
42
41
|
def flags
|
|
43
|
-
@flags ||=
|
|
42
|
+
@flags ||= inspector.flags
|
|
44
43
|
end
|
|
45
44
|
|
|
46
45
|
# Delegates to {Inspector#issues}.
|
|
47
46
|
#
|
|
48
|
-
# @return [String]
|
|
49
|
-
# @return [
|
|
47
|
+
# @return [String] If given.
|
|
48
|
+
# @return [nil] If not given.
|
|
50
49
|
def issues
|
|
51
|
-
@issues ||=
|
|
50
|
+
@issues ||= inspector.issues
|
|
52
51
|
end
|
|
53
52
|
|
|
54
53
|
# Delegates to {Inspector#info}.
|
|
55
54
|
#
|
|
56
|
-
# @return [String]
|
|
57
|
-
# @return [
|
|
55
|
+
# @return [String] If given.
|
|
56
|
+
# @return [nil] If not given.
|
|
58
57
|
def info
|
|
59
|
-
@info ||=
|
|
58
|
+
@info ||= inspector.info
|
|
60
59
|
end
|
|
61
60
|
|
|
62
61
|
# Delegates to {Inspector#name}.
|
|
63
62
|
#
|
|
64
|
-
# @return [String]
|
|
65
|
-
# @return [
|
|
63
|
+
# @return [String] If given.
|
|
64
|
+
# @return [nil] If not given.
|
|
66
65
|
def name
|
|
67
|
-
@name ||=
|
|
66
|
+
@name ||= inspector.name
|
|
68
67
|
end
|
|
69
68
|
end
|
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
# ObjectInspector::
|
|
4
|
-
#
|
|
5
|
-
# output format by combining Strings.
|
|
3
|
+
# Specializes on {ObjectInspector::BaseFormatter} to build inspect output by
|
|
4
|
+
# combining Strings (as opposed to {ObjectInspector::TemplatingFormatter}).
|
|
6
5
|
#
|
|
7
6
|
# @attr (see BaseFormatter)
|
|
8
7
|
class ObjectInspector::CombiningFormatter < ObjectInspector::BaseFormatter
|
|
@@ -20,8 +19,8 @@ class ObjectInspector::CombiningFormatter < ObjectInspector::BaseFormatter
|
|
|
20
19
|
private
|
|
21
20
|
|
|
22
21
|
def build_wrapped_object_string
|
|
23
|
-
"#{build_string}
|
|
24
|
-
"#{ObjectInspector.configuration.presented_object_separator}
|
|
22
|
+
"#{build_string}"\
|
|
23
|
+
"#{ObjectInspector.configuration.presented_object_separator}"\
|
|
25
24
|
"#{wrapped_object_inspection_result}"
|
|
26
25
|
end
|
|
27
26
|
|
|
@@ -4,9 +4,8 @@
|
|
|
4
4
|
# :reek:TooManyMethods
|
|
5
5
|
# rubocop:disable Metrics/ClassLength
|
|
6
6
|
|
|
7
|
-
# ObjectInspector::
|
|
8
|
-
#
|
|
9
|
-
# output format via String templates.
|
|
7
|
+
# Specializes on {ObjectInspector::BaseFormatter} to build the default inspect
|
|
8
|
+
# output format using String templates.
|
|
10
9
|
#
|
|
11
10
|
# @attr (see BaseFormatter)
|
|
12
11
|
class ObjectInspector::TemplatingFormatter < ObjectInspector::BaseFormatter
|
|
@@ -47,13 +46,12 @@ class ObjectInspector::TemplatingFormatter < ObjectInspector::BaseFormatter
|
|
|
47
46
|
private
|
|
48
47
|
|
|
49
48
|
def build_wrapped_object_string
|
|
50
|
-
"#{build_string}
|
|
51
|
-
"#{ObjectInspector.configuration.presented_object_separator}
|
|
49
|
+
"#{build_string}"\
|
|
50
|
+
"#{ObjectInspector.configuration.presented_object_separator}"\
|
|
52
51
|
"#{wrapped_object_inspection_result}"
|
|
53
52
|
end
|
|
54
53
|
|
|
55
|
-
# rubocop:disable Metrics/MethodLength
|
|
56
|
-
def build_string
|
|
54
|
+
def build_string # rubocop:disable Metrics/MethodLength
|
|
57
55
|
if flags
|
|
58
56
|
build_string_with_flags_and_maybe_issues_and_info_and_name
|
|
59
57
|
elsif issues
|
|
@@ -66,7 +64,6 @@ class ObjectInspector::TemplatingFormatter < ObjectInspector::BaseFormatter
|
|
|
66
64
|
build_base_string
|
|
67
65
|
end
|
|
68
66
|
end
|
|
69
|
-
# rubocop:enable Metrics/MethodLength
|
|
70
67
|
|
|
71
68
|
def build_string_with_flags_and_maybe_issues_and_info_and_name
|
|
72
69
|
if issues
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# ObjectInspector::InspectBehaviors can be included into any object to override
|
|
4
|
+
# the default `#inspect` method for that object to instead call
|
|
5
|
+
# {ObjectInspector::Inspector.inspect}.
|
|
6
|
+
module ObjectInspector::InspectBehaviors
|
|
7
|
+
# Calls {ObjectInspector::Inspector.inspect} on `self`, passing through any
|
|
8
|
+
# keyword arguments.
|
|
9
|
+
#
|
|
10
|
+
# @return [String]
|
|
11
|
+
def inspect(**)
|
|
12
|
+
return super() if ObjectInspector.configuration.disabled?
|
|
13
|
+
|
|
14
|
+
ObjectInspector::Inspector.inspect(self, **)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
# Like {#inspect} but:
|
|
18
|
+
# - Ignores the enabled/disabled state of ObjectInspector
|
|
19
|
+
# - Forces scope to `:all`
|
|
20
|
+
#
|
|
21
|
+
# This (the bang (!) version) is considered the "more dangerous" version of
|
|
22
|
+
# {#inspect} since the `:all` scope may result in additional queries or extra
|
|
23
|
+
# processing--depending on how the inspect hooks are setup.
|
|
24
|
+
#
|
|
25
|
+
# @return [String]
|
|
26
|
+
def inspect!(**)
|
|
27
|
+
ObjectInspector::Inspector.inspect(self, **, scope: :all)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
private
|
|
31
|
+
|
|
32
|
+
# :reek:UtilityFunction
|
|
33
|
+
|
|
34
|
+
# Allow ActiveRecord::Core#pretty_print to produce the standard Pretty-printed
|
|
35
|
+
# output (vs just straight #inspect String) when ObjectInspector is disabled.
|
|
36
|
+
def custom_inspect_method_defined?
|
|
37
|
+
ObjectInspector.configuration.enabled?
|
|
38
|
+
end
|
|
39
|
+
end
|
|
@@ -1,10 +1,20 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
# ObjectInspector::Inspector organizes inspection of the associated {#object}
|
|
4
|
-
# via the passed in options and via an {ObjectInspector::BaseFormatter}
|
|
5
|
-
# instance.
|
|
6
|
-
#
|
|
7
3
|
# :reek:TooManyMethods
|
|
4
|
+
|
|
5
|
+
# Organizes inspection of the associated {#object} via the passed in options and
|
|
6
|
+
# via an {ObjectInspector::BaseFormatter} instance.
|
|
7
|
+
#
|
|
8
|
+
# @example
|
|
9
|
+
# ObjectInspector::Inspector.inspect(
|
|
10
|
+
# self,
|
|
11
|
+
# identification: self.class.name,
|
|
12
|
+
# flags: "FLAG1",
|
|
13
|
+
# issues: "ISSUE1",
|
|
14
|
+
# info: "INFO",
|
|
15
|
+
# name: "NAME",
|
|
16
|
+
# )
|
|
17
|
+
# # => "<Object(FLAG1) !!ISSUE1!! INFO :: NAME>"
|
|
8
18
|
class ObjectInspector::Inspector
|
|
9
19
|
attr_reader :object
|
|
10
20
|
|
|
@@ -12,9 +22,9 @@ class ObjectInspector::Inspector
|
|
|
12
22
|
# required to use ObjectInspector::Inspector.
|
|
13
23
|
#
|
|
14
24
|
# @return [String]
|
|
15
|
-
def self.inspect(...)
|
|
16
|
-
|
|
17
|
-
|
|
25
|
+
def self.inspect(...) = new(...).to_s
|
|
26
|
+
|
|
27
|
+
# :reek:DuplicateMethodCall
|
|
18
28
|
|
|
19
29
|
# @param object [Object] the object being inspected
|
|
20
30
|
# @param scope [Symbol] Object inspection type. For example:
|
|
@@ -22,18 +32,17 @@ class ObjectInspector::Inspector
|
|
|
22
32
|
# <custom> -- Anything else that makes sense for {#object} to key
|
|
23
33
|
# on
|
|
24
34
|
# @param formatter [ObjectInspector::BaseFormatter]
|
|
25
|
-
# (ObjectInspector.configuration.
|
|
26
|
-
# to use for formatting the inspect String
|
|
27
|
-
# @param kwargs [Hash]
|
|
28
|
-
# {ObjectInspector::
|
|
29
|
-
# methods
|
|
30
|
-
#
|
|
31
|
-
# :reek:DuplicateMethodCall (ObjectInspecto.configuration)
|
|
35
|
+
# (ObjectInspector.configuration.formatter_class) The formatter object type
|
|
36
|
+
# to use for formatting the inspect String.
|
|
37
|
+
# @param kwargs [Hash] Options to be sent to {#object} via
|
|
38
|
+
# {ObjectInspector::InterrogateObject} when calling the `inspect_*`
|
|
39
|
+
# methods.
|
|
32
40
|
def initialize(
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
41
|
+
object,
|
|
42
|
+
scope: ObjectInspector.configuration.default_scope,
|
|
43
|
+
formatter: ObjectInspector.configuration.formatter_class,
|
|
44
|
+
**kwargs
|
|
45
|
+
)
|
|
37
46
|
@object = object
|
|
38
47
|
@scope = ObjectInspector::Conversions.Scope(scope)
|
|
39
48
|
@formatter_class = formatter
|
|
@@ -50,8 +59,8 @@ class ObjectInspector::Inspector
|
|
|
50
59
|
# Generate the inspect String for the wrapped object, if `self` is a wrapper
|
|
51
60
|
# object.
|
|
52
61
|
#
|
|
53
|
-
# @return [String]
|
|
54
|
-
# @return [
|
|
62
|
+
# @return [String] If {#object_is_a_wrapper?}.
|
|
63
|
+
# @return [nil] If not {#object_is_a_wrapper?}.
|
|
55
64
|
def wrapped_object_inspection_result
|
|
56
65
|
return unless object_is_a_wrapper?
|
|
57
66
|
|
|
@@ -59,7 +68,8 @@ class ObjectInspector::Inspector
|
|
|
59
68
|
extract_wrapped_object,
|
|
60
69
|
scope: @scope,
|
|
61
70
|
formatter: @formatter_class,
|
|
62
|
-
kwargs: @kwargs
|
|
71
|
+
kwargs: @kwargs,
|
|
72
|
+
)
|
|
63
73
|
end
|
|
64
74
|
|
|
65
75
|
# Core object identification details, such as the {#object} class name and
|
|
@@ -67,62 +77,68 @@ class ObjectInspector::Inspector
|
|
|
67
77
|
#
|
|
68
78
|
# @return [String]
|
|
69
79
|
def identification
|
|
70
|
-
(value(key: :identification) ||
|
|
80
|
+
(value(key: :identification) || object.class).to_s
|
|
71
81
|
end
|
|
72
82
|
|
|
73
83
|
# Boolean flags/states applicable to {#object}.
|
|
74
84
|
#
|
|
75
|
-
# @return [String]
|
|
76
|
-
# @return [
|
|
85
|
+
# @return [String] If given.
|
|
86
|
+
# @return [nil] If not given.
|
|
77
87
|
def flags
|
|
78
88
|
value(key: :flags)
|
|
79
89
|
end
|
|
80
90
|
|
|
81
91
|
# Issues/Warnings applicable to {#object}.
|
|
82
92
|
#
|
|
83
|
-
# @return [String]
|
|
84
|
-
# @return [
|
|
93
|
+
# @return [String] If given.
|
|
94
|
+
# @return [nil] If not given.
|
|
85
95
|
def issues
|
|
86
96
|
value(key: :issues)
|
|
87
97
|
end
|
|
88
98
|
|
|
89
99
|
# Informational details applicable to {#object}.
|
|
90
100
|
#
|
|
91
|
-
# @return [String]
|
|
92
|
-
# @return [
|
|
101
|
+
# @return [String] If given.
|
|
102
|
+
# @return [nil] If not given.
|
|
93
103
|
def info
|
|
94
104
|
value(key: :info)
|
|
95
105
|
end
|
|
96
106
|
|
|
97
107
|
# A human-friendly identifier for {#object}.
|
|
98
108
|
#
|
|
99
|
-
# @return [String]
|
|
100
|
-
# @return [
|
|
109
|
+
# @return [String] If given.
|
|
110
|
+
# @return [nil] If not given.
|
|
101
111
|
def name
|
|
102
112
|
key = :name
|
|
103
113
|
|
|
104
114
|
if @kwargs.key?(key)
|
|
105
|
-
value(key:
|
|
115
|
+
value(key:)
|
|
106
116
|
else
|
|
107
117
|
interrogate_object_inspect_method(key) ||
|
|
108
118
|
interrogate_object(
|
|
109
|
-
method_name: :display_name,
|
|
119
|
+
method_name: :display_name,
|
|
120
|
+
kwargs: object_method_keyword_arguments,
|
|
121
|
+
)
|
|
110
122
|
end
|
|
111
123
|
end
|
|
112
124
|
|
|
113
125
|
private
|
|
114
126
|
|
|
127
|
+
attr_reader :scope,
|
|
128
|
+
:formatter_class,
|
|
129
|
+
:kwargs
|
|
130
|
+
|
|
115
131
|
def formatter
|
|
116
|
-
|
|
132
|
+
formatter_class.new(self)
|
|
117
133
|
end
|
|
118
134
|
|
|
119
|
-
# @return [String]
|
|
120
|
-
# `#{object_inspection_method_name}` (e.g. `inspect_flags`)
|
|
121
|
-
# @return [
|
|
135
|
+
# @return [String] If `key` is found in {#kwargs} or if {#object} responds to
|
|
136
|
+
# `#{object_inspection_method_name}` (e.g. `inspect_flags`).
|
|
137
|
+
# @return [nil] If not found in {#kwargs} or {#object}.
|
|
122
138
|
def value(key:)
|
|
123
139
|
return_value =
|
|
124
|
-
if
|
|
125
|
-
evaluate_passed_in_value(
|
|
140
|
+
if kwargs.key?(key)
|
|
141
|
+
evaluate_passed_in_value(kwargs[key])
|
|
126
142
|
else
|
|
127
143
|
interrogate_object_inspect_method(key)
|
|
128
144
|
end
|
|
@@ -133,10 +149,10 @@ class ObjectInspector::Inspector
|
|
|
133
149
|
# Call `value` on {#object} if it responds to it and the result is not nil,
|
|
134
150
|
# else just return `value`.
|
|
135
151
|
#
|
|
136
|
-
# @return [#to_s]
|
|
137
|
-
# isn't nil
|
|
138
|
-
# @return [
|
|
139
|
-
#
|
|
152
|
+
# @return [#to_s] If {#object} responds to `value` and if the call result
|
|
153
|
+
# isn't nil.
|
|
154
|
+
# @return [nil] If {#object} doesn't respond to `value` or if the call result
|
|
155
|
+
# is nil.
|
|
140
156
|
def evaluate_passed_in_value(value)
|
|
141
157
|
if value.is_a?(Symbol)
|
|
142
158
|
interrogate_object(method_name: value) || value
|
|
@@ -147,47 +163,44 @@ class ObjectInspector::Inspector
|
|
|
147
163
|
|
|
148
164
|
# Attempt to call `inspect_*` on {#object} based on the passed in `name`.
|
|
149
165
|
#
|
|
150
|
-
# @return [String]
|
|
151
|
-
# `#{object_inspection_method_name}` (e.g. `inspect_flags`)
|
|
152
|
-
# @return [
|
|
166
|
+
# @return [String] If {#object} responds to
|
|
167
|
+
# `#{object_inspection_method_name}` (e.g. `inspect_flags`).
|
|
168
|
+
# @return [nil] If not found on {#object}.
|
|
153
169
|
def interrogate_object_inspect_method(
|
|
154
|
-
|
|
155
|
-
|
|
170
|
+
name,
|
|
171
|
+
prefix: ObjectInspector.configuration.inspect_method_prefix
|
|
172
|
+
)
|
|
156
173
|
interrogate_object(
|
|
157
|
-
method_name: object_inspection_method_name(name, prefix:
|
|
158
|
-
kwargs: object_method_keyword_arguments
|
|
174
|
+
method_name: object_inspection_method_name(name, prefix:),
|
|
175
|
+
kwargs: object_method_keyword_arguments,
|
|
176
|
+
)
|
|
159
177
|
end
|
|
160
178
|
|
|
161
179
|
def interrogate_object(method_name:, kwargs: {})
|
|
162
|
-
|
|
163
|
-
ObjectInspector::ObjectInterrogator.new(
|
|
164
|
-
object: @object,
|
|
165
|
-
method_name: method_name,
|
|
166
|
-
kwargs: kwargs)
|
|
167
|
-
|
|
168
|
-
interrogator.call
|
|
180
|
+
ObjectInspector::InterrogateObject.(object, method_name:, kwargs:)
|
|
169
181
|
end
|
|
170
182
|
|
|
171
183
|
# :reek:UtilityFunction
|
|
184
|
+
|
|
172
185
|
def object_inspection_method_name(
|
|
173
|
-
|
|
174
|
-
|
|
186
|
+
name,
|
|
187
|
+
prefix: ObjectInspector.configuration.inspect_method_prefix
|
|
188
|
+
)
|
|
175
189
|
"#{prefix}_#{name}"
|
|
176
190
|
end
|
|
177
191
|
|
|
178
192
|
def object_method_keyword_arguments
|
|
179
|
-
{
|
|
180
|
-
scope: @scope,
|
|
181
|
-
}
|
|
193
|
+
{ scope: }
|
|
182
194
|
end
|
|
183
195
|
|
|
184
196
|
def extract_wrapped_object
|
|
185
|
-
|
|
197
|
+
object.to_model
|
|
186
198
|
end
|
|
187
199
|
|
|
188
200
|
# :reek:ManualDispatch
|
|
201
|
+
|
|
189
202
|
def object_is_a_wrapper?
|
|
190
|
-
|
|
191
|
-
|
|
203
|
+
object.respond_to?(:to_model) &&
|
|
204
|
+
object.to_model != object
|
|
192
205
|
end
|
|
193
206
|
end
|
|
@@ -1,25 +1,29 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
#
|
|
4
|
-
#
|
|
3
|
+
# Collaborates with {#object} to return {#object}#{#method_name} if {#object}
|
|
4
|
+
# responds to {#method_name}.
|
|
5
5
|
#
|
|
6
|
-
# If
|
|
6
|
+
# If {#object}#{#method_name} accepts the supplied `kwargs` then they are passed
|
|
7
7
|
# in as well. If not, then any supplied `kwargs` will be ignored.
|
|
8
|
-
class ObjectInspector::
|
|
8
|
+
class ObjectInspector::InterrogateObject
|
|
9
|
+
# @return (see #call)
|
|
10
|
+
def self.call(...) = new(...).call
|
|
11
|
+
|
|
9
12
|
attr_reader :object,
|
|
10
13
|
:method_name,
|
|
11
14
|
:kwargs
|
|
12
15
|
|
|
13
|
-
def initialize(object
|
|
16
|
+
def initialize(object, method_name:, kwargs: {})
|
|
14
17
|
@object = object
|
|
15
18
|
@method_name = method_name
|
|
16
19
|
@kwargs = kwargs
|
|
17
20
|
end
|
|
18
21
|
|
|
19
|
-
# @return [
|
|
22
|
+
# @return [Object, nil] The result of calling {#method_name} on {#object},
|
|
23
|
+
# or `nil` if {#object} does not respond to {#method_name}.
|
|
20
24
|
#
|
|
21
|
-
# @raise [ArgumentError]
|
|
22
|
-
# signature
|
|
25
|
+
# @raise [ArgumentError] If {#object}#{#method_name} has an unexpected method
|
|
26
|
+
# signature.
|
|
23
27
|
def call
|
|
24
28
|
return unless object_responds_to_method_name?
|
|
25
29
|
|
|
@@ -40,6 +44,7 @@ class ObjectInspector::ObjectInterrogator
|
|
|
40
44
|
|
|
41
45
|
# :reek:ManualDispatch
|
|
42
46
|
# :reek:BooleanParameter
|
|
47
|
+
|
|
43
48
|
def object_responds_to_method_name?(include_private: true)
|
|
44
49
|
object.respond_to?(method_name, include_private)
|
|
45
50
|
end
|
|
@@ -2,9 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
# :reek:TooManyMethods
|
|
4
4
|
|
|
5
|
-
#
|
|
6
|
-
#
|
|
7
|
-
# "scope" within objects.
|
|
5
|
+
# Defines a predicate method that matches {#names} and responds with `true`.
|
|
6
|
+
# This is a prettier way to test for a given type of "scope" within objects.
|
|
8
7
|
#
|
|
9
8
|
# It is possible to pass in multiple scope names to match on.
|
|
10
9
|
#
|
|
@@ -13,6 +12,19 @@
|
|
|
13
12
|
# Passing a block to a scope predicate falls back to the out-of-scope
|
|
14
13
|
# placeholder (`*` by default) if the scope does not match.
|
|
15
14
|
#
|
|
15
|
+
# @example
|
|
16
|
+
# ObjectInspector::Scope.new
|
|
17
|
+
# # => <ObjectInspector::Scope :: ["self"]>
|
|
18
|
+
#
|
|
19
|
+
# ObjectInspector::Scope.new(:my_custom_scope)
|
|
20
|
+
# # => <ObjectInspector::Scope :: ["my_custom_scope"]>
|
|
21
|
+
#
|
|
22
|
+
# ObjectInspector::Scope.new(%w[verbose complex])
|
|
23
|
+
# # => <ObjectInspector::Scope :: ["complex", "verbose"]>
|
|
24
|
+
#
|
|
25
|
+
# ObjectInspector::Scope.new(:verbose, :self)
|
|
26
|
+
# # => <ObjectInspector::Scope :: ["self", "verbose"]>
|
|
27
|
+
#
|
|
16
28
|
# @see ActiveSupport::StringInquirer
|
|
17
29
|
# http://api.rubyonrails.org/classes/ActiveSupport/StringInquirer.html
|
|
18
30
|
#
|
|
@@ -20,16 +32,21 @@
|
|
|
20
32
|
class ObjectInspector::Scope
|
|
21
33
|
attr_reader :names
|
|
22
34
|
|
|
23
|
-
|
|
24
|
-
|
|
35
|
+
# :reek:FeatureEnvy
|
|
36
|
+
|
|
37
|
+
def initialize(*names)
|
|
38
|
+
names = names.empty? ? %w[self] : names.flatten
|
|
39
|
+
@names = names.map! { |name| String(name) }.sort!
|
|
25
40
|
end
|
|
26
41
|
|
|
27
42
|
# Join the passed in name parts with the passed in separator.
|
|
28
43
|
#
|
|
29
44
|
# @param parts [Array<#to_s>]
|
|
30
|
-
# @param separator [#to_s] (ObjectInspector.configuration.
|
|
45
|
+
# @param separator [#to_s] (ObjectInspector.configuration.name_separator)
|
|
31
46
|
def join_name(
|
|
32
|
-
|
|
47
|
+
parts,
|
|
48
|
+
separator: ObjectInspector.configuration.name_separator
|
|
49
|
+
)
|
|
33
50
|
_join(parts, separator)
|
|
34
51
|
end
|
|
35
52
|
|
|
@@ -38,7 +55,9 @@ class ObjectInspector::Scope
|
|
|
38
55
|
# @param flags [Array<#to_s>]
|
|
39
56
|
# @param separator [#to_s] (ObjectInspector.configuration.flags_separator)
|
|
40
57
|
def join_flags(
|
|
41
|
-
|
|
58
|
+
flags,
|
|
59
|
+
separator: ObjectInspector.configuration.flags_separator
|
|
60
|
+
)
|
|
42
61
|
_join(flags, separator)
|
|
43
62
|
end
|
|
44
63
|
|
|
@@ -47,7 +66,9 @@ class ObjectInspector::Scope
|
|
|
47
66
|
# @param issues [Array<#to_s>]
|
|
48
67
|
# @param separator [#to_s] (ObjectInspector.configuration.issues_separator)
|
|
49
68
|
def join_issues(
|
|
50
|
-
|
|
69
|
+
issues,
|
|
70
|
+
separator: ObjectInspector.configuration.issues_separator
|
|
71
|
+
)
|
|
51
72
|
_join(issues, separator)
|
|
52
73
|
end
|
|
53
74
|
|
|
@@ -56,19 +77,21 @@ class ObjectInspector::Scope
|
|
|
56
77
|
# @param items [Array<#to_s>]
|
|
57
78
|
# @param separator [#to_s] (ObjectInspector.configuration.info_separator)
|
|
58
79
|
def join_info(
|
|
59
|
-
|
|
80
|
+
items,
|
|
81
|
+
separator: ObjectInspector.configuration.info_separator
|
|
82
|
+
)
|
|
60
83
|
_join(items, separator)
|
|
61
84
|
end
|
|
62
85
|
|
|
63
86
|
# Compare self with the passed in object.
|
|
64
87
|
#
|
|
65
|
-
# @return [TrueClass]
|
|
66
|
-
# @return [FalseClass]
|
|
67
|
-
# objects
|
|
88
|
+
# @return [TrueClass] If self and `other` resolve to the same set of objects.
|
|
89
|
+
# @return [FalseClass] If self and `other` resolve to a different set of
|
|
90
|
+
# objects.
|
|
68
91
|
def ==(other)
|
|
69
|
-
|
|
92
|
+
names == Array(other).map(&:to_s).sort!
|
|
70
93
|
end
|
|
71
|
-
|
|
94
|
+
alias eql? ==
|
|
72
95
|
|
|
73
96
|
# @return [String] the contents of {#names}, joined by `, `.
|
|
74
97
|
def to_s(separator: ", ")
|
|
@@ -80,6 +103,11 @@ class ObjectInspector::Scope
|
|
|
80
103
|
names
|
|
81
104
|
end
|
|
82
105
|
|
|
106
|
+
# @return [String] A pretty representation of the scope and its {#names}.
|
|
107
|
+
def inspect
|
|
108
|
+
"<#{self.class.name} :: #{names.inspect}>"
|
|
109
|
+
end
|
|
110
|
+
|
|
83
111
|
private
|
|
84
112
|
|
|
85
113
|
def method_missing(method_name, *args, &)
|
|
@@ -131,10 +159,11 @@ class ObjectInspector::Scope
|
|
|
131
159
|
end
|
|
132
160
|
|
|
133
161
|
def any_names_match?(other_name)
|
|
134
|
-
|
|
162
|
+
names.any?(other_name)
|
|
135
163
|
end
|
|
136
164
|
|
|
137
165
|
# :reek:BooleanParameter
|
|
166
|
+
|
|
138
167
|
def respond_to_missing?(method_name, include_private = false)
|
|
139
168
|
method_name[-1] == "?" || super
|
|
140
169
|
end
|