object_identifier 0.10.0 → 0.11.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 +52 -24
- data/lib/core_ext/object.rb +6 -3
- data/lib/object_identifier/configuration.rb +1 -1
- data/lib/object_identifier/formatters/base_formatter.rb +1 -1
- data/lib/object_identifier/formatters/string_formatter.rb +1 -1
- data/lib/object_identifier/parameters.rb +22 -10
- data/lib/object_identifier/version.rb +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: 29e798cf195e524f05ad84c6b2d68573055a68a2ab35aa26f3cde652fd34569e
|
4
|
+
data.tar.gz: 552e5b2a70321be36ad0e32429346bc0c69c11526a64256116849739c5e3f894
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1adb6f5553906c1c990214c2b237f994e222c1a260f271ddd88c6a3e95c5fe60f114d7d7f34449fe17b9f19053cc8816e8b2f196ae1cdb2e9a1605982bd9e00e
|
7
|
+
data.tar.gz: 5704c4b611b882b4ab5180bc1f0abb0691102411cb54f80afddcd5e37112e76600c1116b09a968d56a39ea978cc9c8ed98c960500d5eda1a29e0973fb32540c5
|
data/README.md
CHANGED
@@ -5,6 +5,8 @@
|
|
5
5
|
|
6
6
|
Object Identifier allows quick, easy, and uniform identification of an object by inspecting its class name and outputting any desirable attributes/methods. It is great for logging, sending descriptive notification messages, etc.
|
7
7
|
|
8
|
+
Why? Because object identification output should be uniform and easy to build, and its output should be easy to read! Consistency improves readability.
|
9
|
+
|
8
10
|
For example:
|
9
11
|
|
10
12
|
```ruby
|
@@ -27,11 +29,15 @@ gem "object_identifier"
|
|
27
29
|
|
28
30
|
And then execute:
|
29
31
|
|
30
|
-
|
32
|
+
```sh
|
33
|
+
$ bundle
|
34
|
+
```
|
31
35
|
|
32
36
|
Or install it yourself as:
|
33
37
|
|
34
|
-
|
38
|
+
```sh
|
39
|
+
$ gem install object_identifier
|
40
|
+
```
|
35
41
|
|
36
42
|
## Compatibility
|
37
43
|
|
@@ -50,7 +56,7 @@ gem "object_identifier", "0.7.0"
|
|
50
56
|
For Ruby 3.1 support, install object_identifier gem version 0.9.0.
|
51
57
|
|
52
58
|
```ruby
|
53
|
-
gem "object_identifier", "0.
|
59
|
+
gem "object_identifier", "0.9.0"
|
54
60
|
```
|
55
61
|
|
56
62
|
Object Identifier has no other dependencies.
|
@@ -59,10 +65,10 @@ Object Identifier has no other dependencies.
|
|
59
65
|
|
60
66
|
Global/default values for Object Identifier can be configured via the ObjectIdentifier::Configuration object.
|
61
67
|
|
62
|
-
_Note: In a Rails app, the following would go in e.g. `config/initializers/object_identifier.rb`_
|
63
|
-
|
64
68
|
```ruby
|
65
|
-
#
|
69
|
+
# config/initializers/object_identifier.rb
|
70
|
+
|
71
|
+
# Default values are shown. Customize to your liking.
|
66
72
|
ObjectIdentifier.configure do |config|
|
67
73
|
config.formatter_class = ObjectIdentifier::StringFormatter
|
68
74
|
config.default_attributes = %i[id]
|
@@ -76,21 +82,35 @@ end
|
|
76
82
|
`identify` outputs the `id` of the receiving object by default, if it exists and no other attributes/methods are specified.
|
77
83
|
|
78
84
|
```ruby
|
79
|
-
|
85
|
+
Movie = Data.define(:id, :name, :rating)
|
86
|
+
|
87
|
+
my_movie = Movie[1, "Identifier Gadget", "7/10"]
|
88
|
+
|
89
|
+
my_movie.identify # => "Movie[1]"
|
80
90
|
```
|
81
91
|
|
82
92
|
`identify` doesn't output labels if only identifying a single attribute/method. It includes labels when two or more attributes/methods are being identified.
|
83
93
|
|
84
94
|
```ruby
|
85
|
-
my_movie.identify(:id)
|
86
|
-
my_movie.identify(:
|
87
|
-
my_movie.identify(:
|
95
|
+
my_movie.identify(:id) # => "Movie[1]"
|
96
|
+
my_movie.identify(:name) # => 'Movie["Identifier Gadget"]'
|
97
|
+
my_movie.identify(:rating) # => 'Movie["7/10"]'
|
98
|
+
|
99
|
+
my_movie.identify(:id, :name, :rating)
|
100
|
+
# => 'Movie[id:1, name: "Identifier Gadget", rating:"7/10"]'
|
88
101
|
```
|
89
102
|
|
90
103
|
Private methods can be identified just the same as public methods.
|
91
104
|
|
92
105
|
```ruby
|
93
|
-
|
106
|
+
Movie =
|
107
|
+
Data.define(:id, :name, :rating) do
|
108
|
+
private def my_private_method = "Shh"
|
109
|
+
end
|
110
|
+
|
111
|
+
my_movie = Movie[1, "Private Identifier", "7/10"]
|
112
|
+
|
113
|
+
my_movie.identify(:my_private_method) # => 'Movie["Shh"]'
|
94
114
|
```
|
95
115
|
|
96
116
|
### Unknown Attributes/Methods
|
@@ -98,21 +118,21 @@ my_movie.identify(:my_private_method) # => Movie["Shh"]
|
|
98
118
|
If the object doesn't respond to a specified attribute/method it is simply ignored:
|
99
119
|
|
100
120
|
```ruby
|
101
|
-
my_movie.identify(:id, :rating, :
|
121
|
+
my_movie.identify(:id, :rating, :unknown) # => 'Movie[id:1, rating:"7/10"]'
|
102
122
|
```
|
103
123
|
|
104
124
|
### Overriding Class Names
|
105
125
|
|
106
126
|
```ruby
|
107
|
-
|
108
|
-
my_movie.identify(
|
127
|
+
my_movie.identify(class: "MyBlockbuster") # => "MyBlockbuster[1]"
|
128
|
+
my_movie.identify(class: nil) # => "[1]"
|
109
129
|
```
|
110
130
|
|
111
131
|
### Identifying Nil
|
112
132
|
|
113
133
|
```ruby
|
114
|
-
nil.identify(:id, :name)
|
115
|
-
nil.identify(:id, :name,
|
134
|
+
nil.identify(:id, :name) # => "[no objects]"
|
135
|
+
nil.identify(:id, :name, class: "Nope") # => "[no objects]"
|
116
136
|
```
|
117
137
|
|
118
138
|
### Collections
|
@@ -120,22 +140,25 @@ nil.identify(:id, :name, klass: "Nope") # => [no objects]
|
|
120
140
|
Collections of objects are each identified in turn.
|
121
141
|
|
122
142
|
```ruby
|
143
|
+
User = Data.define(:id, :name)
|
144
|
+
my_user = User[2, "Bob"]
|
145
|
+
|
123
146
|
[my_movie, my_user].identify(:id, :name)
|
124
|
-
# => Movie[id:1, name:"
|
147
|
+
# => 'Movie[id:1, name:"Identifier Gadget"], User[id:2, name:"Bob"]'
|
125
148
|
```
|
126
149
|
|
127
150
|
The number of results that will be identified from a collection can be truncated by specifying the `limit` option.
|
128
151
|
|
129
152
|
```ruby
|
130
153
|
[my_movie, my_user].identify(:id, :name, limit: 1)
|
131
|
-
# => Movie[id:1, name:"
|
154
|
+
# => 'Movie[id:1, name:"Identifier Gadget"], ... (1 more)'
|
132
155
|
```
|
133
156
|
|
134
157
|
### Empty Collections
|
135
158
|
|
136
159
|
```ruby
|
137
|
-
[].identify # => [no objects]
|
138
|
-
{}.identify # => [no objects]
|
160
|
+
[].identify # => "[no objects]"
|
161
|
+
{}.identify # => "[no objects]"
|
139
162
|
```
|
140
163
|
|
141
164
|
## Supporting Gems
|
@@ -146,16 +169,21 @@ Object Identifier works great with the [Object Inspector](https://github.com/pdo
|
|
146
169
|
|
147
170
|
Performance of Formatters can be tested by playing the [Formatters Benchmarking Scripts](https://github.com/pdobb/object_identifier/blob/master/script/benchmarking/formatters.rb) in the IRB console for this gem.
|
148
171
|
|
149
|
-
Custom Formatters may be similarly gauged for comparison by
|
172
|
+
Custom Formatters may be similarly gauged for comparison by putting them into constant array `CUSTOM_FORMATTER_CLASSES` before loading the script in the IRB console for this gem.
|
150
173
|
|
151
174
|
```ruby
|
152
|
-
|
175
|
+
CUSTOM_FORMATTER_CLASSES = [MyCustomFormatter]
|
153
176
|
|
154
177
|
load "script/benchmarking/formatters.rb"
|
178
|
+
# Reporting for: Ruby v3.4.2
|
179
|
+
|
180
|
+
# == Averaged ============================================================
|
181
|
+
# ...
|
182
|
+
#
|
155
183
|
# ObjectIdentifier::StringFormatter
|
156
|
-
#
|
184
|
+
# 38.646k (± 1.3%) i/s (25.88 μs/i) - 196.758k in 5.092094s
|
157
185
|
# MyCustomFormatter
|
158
|
-
#
|
186
|
+
# ...
|
159
187
|
```
|
160
188
|
|
161
189
|
## Development
|
data/lib/core_ext/object.rb
CHANGED
@@ -13,15 +13,18 @@ class Object
|
|
13
13
|
# for this object
|
14
14
|
# @param [Hash] options the options for building a customized
|
15
15
|
# self-identifier
|
16
|
-
# @option options [String, nil] :
|
16
|
+
# @option options [String, nil] :class object class name override
|
17
17
|
# @option options [Integer] :limit maximum number of objects to display from
|
18
18
|
# a collection
|
19
19
|
#
|
20
20
|
# @return [String] a self-identifying string
|
21
21
|
#
|
22
22
|
# @example
|
23
|
-
#
|
24
|
-
#
|
23
|
+
# Struct.new(:a, :b, :c)[1, "2", :"3"].identify(:a, :b, :c, class: "Struct")
|
24
|
+
# => "Struct[a:1, b:\"2\", c::\"3\"]"
|
25
|
+
|
26
|
+
# Data.define(:a, :b, :c)[1, "2", :"3"].identify(:a, :b, :c, class: "Data")
|
27
|
+
# # => "Data[a:1, b:\"2\", c::\"3\"]"
|
25
28
|
#
|
26
29
|
# 1.identify(:to_s) # => "Integer[to_s:\"1\"]"
|
27
30
|
# nil.identify # => "[no objects]"
|
@@ -4,8 +4,8 @@
|
|
4
4
|
# formatter options that may be needed for custom formatting during object
|
5
5
|
# identification.
|
6
6
|
class ObjectIdentifier::Parameters
|
7
|
-
# This String to display if `formatter_options[:
|
8
|
-
|
7
|
+
# This String to display if `formatter_options[:class]` isn't present.
|
8
|
+
CLASS_NOT_GIVEN = "NOT_GIVEN"
|
9
9
|
|
10
10
|
attr_reader :attributes
|
11
11
|
|
@@ -26,14 +26,26 @@ class ObjectIdentifier::Parameters
|
|
26
26
|
# given object(s) with.
|
27
27
|
# @option formatter_options[:limit] [Integer, nil] A given limit on the number
|
28
28
|
# of objects to interrogate.
|
29
|
-
# @option formatter_options[:
|
29
|
+
# @option formatter_options[:class] [#to_s] A preferred type name for
|
30
30
|
# identifying the given object(s) as.
|
31
|
-
def initialize(
|
31
|
+
def initialize( # rubocop:disable Metrics/MethodLength
|
32
32
|
attributes: [],
|
33
33
|
formatter_options: {})
|
34
34
|
@attributes = attributes
|
35
35
|
@limit = formatter_options.fetch(:limit, nil)
|
36
|
-
@
|
36
|
+
@class =
|
37
|
+
formatter_options.fetch(:class) {
|
38
|
+
# For backwards compatibility with earlier versions of this gem.
|
39
|
+
if formatter_options.key?(:klass)
|
40
|
+
warn(
|
41
|
+
"DEPRECATION WARNING: "\
|
42
|
+
"The `klass` option is deprecated and will be removed in v1.0. "\
|
43
|
+
"Use `class` instead.")
|
44
|
+
formatter_options[:klass]
|
45
|
+
else
|
46
|
+
CLASS_NOT_GIVEN
|
47
|
+
end
|
48
|
+
}
|
37
49
|
end
|
38
50
|
|
39
51
|
# NOTE: Expects a block if a value wasn't supplied on initialization.
|
@@ -42,9 +54,9 @@ class ObjectIdentifier::Parameters
|
|
42
54
|
end
|
43
55
|
|
44
56
|
# NOTE: Expects a block if a value wasn't supplied on initialization.
|
45
|
-
def
|
46
|
-
if
|
47
|
-
@
|
57
|
+
def class
|
58
|
+
if class_given?
|
59
|
+
@class.to_s
|
48
60
|
elsif block_given?
|
49
61
|
yield.to_s
|
50
62
|
else
|
@@ -54,7 +66,7 @@ class ObjectIdentifier::Parameters
|
|
54
66
|
|
55
67
|
private
|
56
68
|
|
57
|
-
def
|
58
|
-
@
|
69
|
+
def class_given?
|
70
|
+
@class != CLASS_NOT_GIVEN
|
59
71
|
end
|
60
72
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: object_identifier
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.11.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Paul DobbinSchmaltz
|
8
8
|
bindir: exe
|
9
9
|
cert_chain: []
|
10
|
-
date: 2025-
|
10
|
+
date: 2025-03-23 00:00:00.000000000 Z
|
11
11
|
dependencies:
|
12
12
|
- !ruby/object:Gem::Dependency
|
13
13
|
name: benchmark-ips
|
@@ -94,7 +94,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
94
94
|
- !ruby/object:Gem::Version
|
95
95
|
version: '0'
|
96
96
|
requirements: []
|
97
|
-
rubygems_version: 3.6.
|
97
|
+
rubygems_version: 3.6.6
|
98
98
|
specification_version: 4
|
99
99
|
summary: Object Identifier identifies an object by its class name and attributes.
|
100
100
|
test_files: []
|