object_identifier 0.9.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/LICENSE.txt +1 -1
- data/README.md +59 -25
- data/lib/core_ext/object.rb +8 -5
- 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/object_identifier.rb +0 -1
- data/lib/object_identifier/parameters.rb +22 -10
- data/lib/object_identifier/version.rb +1 -1
- metadata +4 -7
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/LICENSE.txt
CHANGED
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,36 +29,46 @@ 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
|
|
38
44
|
Tested MRI Ruby Versions:
|
39
45
|
|
40
|
-
- 3.1
|
41
46
|
- 3.2
|
42
47
|
- 3.3
|
48
|
+
- 3.4
|
43
49
|
|
44
|
-
|
50
|
+
For Ruby 2.7 support, install object_identifier gem version 0.7.0.
|
45
51
|
|
46
52
|
```ruby
|
47
53
|
gem "object_identifier", "0.7.0"
|
48
54
|
```
|
49
55
|
|
56
|
+
For Ruby 3.1 support, install object_identifier gem version 0.9.0.
|
57
|
+
|
58
|
+
```ruby
|
59
|
+
gem "object_identifier", "0.9.0"
|
60
|
+
```
|
61
|
+
|
50
62
|
Object Identifier has no other dependencies.
|
51
63
|
|
52
64
|
## Configuration
|
53
65
|
|
54
66
|
Global/default values for Object Identifier can be configured via the ObjectIdentifier::Configuration object.
|
55
67
|
|
56
|
-
_Note: In a Rails app, the following would go in e.g. `config/initializers/object_identifier.rb`_
|
57
|
-
|
58
68
|
```ruby
|
59
|
-
#
|
69
|
+
# config/initializers/object_identifier.rb
|
70
|
+
|
71
|
+
# Default values are shown. Customize to your liking.
|
60
72
|
ObjectIdentifier.configure do |config|
|
61
73
|
config.formatter_class = ObjectIdentifier::StringFormatter
|
62
74
|
config.default_attributes = %i[id]
|
@@ -70,21 +82,35 @@ end
|
|
70
82
|
`identify` outputs the `id` of the receiving object by default, if it exists and no other attributes/methods are specified.
|
71
83
|
|
72
84
|
```ruby
|
73
|
-
|
85
|
+
Movie = Data.define(:id, :name, :rating)
|
86
|
+
|
87
|
+
my_movie = Movie[1, "Identifier Gadget", "7/10"]
|
88
|
+
|
89
|
+
my_movie.identify # => "Movie[1]"
|
74
90
|
```
|
75
91
|
|
76
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.
|
77
93
|
|
78
94
|
```ruby
|
79
|
-
my_movie.identify(:id)
|
80
|
-
my_movie.identify(:
|
81
|
-
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"]'
|
82
101
|
```
|
83
102
|
|
84
103
|
Private methods can be identified just the same as public methods.
|
85
104
|
|
86
105
|
```ruby
|
87
|
-
|
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"]'
|
88
114
|
```
|
89
115
|
|
90
116
|
### Unknown Attributes/Methods
|
@@ -92,21 +118,21 @@ my_movie.identify(:my_private_method) # => Movie["Shh"]
|
|
92
118
|
If the object doesn't respond to a specified attribute/method it is simply ignored:
|
93
119
|
|
94
120
|
```ruby
|
95
|
-
my_movie.identify(:id, :rating, :
|
121
|
+
my_movie.identify(:id, :rating, :unknown) # => 'Movie[id:1, rating:"7/10"]'
|
96
122
|
```
|
97
123
|
|
98
124
|
### Overriding Class Names
|
99
125
|
|
100
126
|
```ruby
|
101
|
-
|
102
|
-
my_movie.identify(
|
127
|
+
my_movie.identify(class: "MyBlockbuster") # => "MyBlockbuster[1]"
|
128
|
+
my_movie.identify(class: nil) # => "[1]"
|
103
129
|
```
|
104
130
|
|
105
131
|
### Identifying Nil
|
106
132
|
|
107
133
|
```ruby
|
108
|
-
nil.identify(:id, :name)
|
109
|
-
nil.identify(:id, :name,
|
134
|
+
nil.identify(:id, :name) # => "[no objects]"
|
135
|
+
nil.identify(:id, :name, class: "Nope") # => "[no objects]"
|
110
136
|
```
|
111
137
|
|
112
138
|
### Collections
|
@@ -114,22 +140,25 @@ nil.identify(:id, :name, klass: "Nope") # => [no objects]
|
|
114
140
|
Collections of objects are each identified in turn.
|
115
141
|
|
116
142
|
```ruby
|
143
|
+
User = Data.define(:id, :name)
|
144
|
+
my_user = User[2, "Bob"]
|
145
|
+
|
117
146
|
[my_movie, my_user].identify(:id, :name)
|
118
|
-
# => Movie[id:1, name:"
|
147
|
+
# => 'Movie[id:1, name:"Identifier Gadget"], User[id:2, name:"Bob"]'
|
119
148
|
```
|
120
149
|
|
121
150
|
The number of results that will be identified from a collection can be truncated by specifying the `limit` option.
|
122
151
|
|
123
152
|
```ruby
|
124
153
|
[my_movie, my_user].identify(:id, :name, limit: 1)
|
125
|
-
# => Movie[id:1, name:"
|
154
|
+
# => 'Movie[id:1, name:"Identifier Gadget"], ... (1 more)'
|
126
155
|
```
|
127
156
|
|
128
157
|
### Empty Collections
|
129
158
|
|
130
159
|
```ruby
|
131
|
-
[].identify # => [no objects]
|
132
|
-
{}.identify # => [no objects]
|
160
|
+
[].identify # => "[no objects]"
|
161
|
+
{}.identify # => "[no objects]"
|
133
162
|
```
|
134
163
|
|
135
164
|
## Supporting Gems
|
@@ -140,16 +169,21 @@ Object Identifier works great with the [Object Inspector](https://github.com/pdo
|
|
140
169
|
|
141
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.
|
142
171
|
|
143
|
-
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.
|
144
173
|
|
145
174
|
```ruby
|
146
|
-
|
175
|
+
CUSTOM_FORMATTER_CLASSES = [MyCustomFormatter]
|
147
176
|
|
148
177
|
load "script/benchmarking/formatters.rb"
|
178
|
+
# Reporting for: Ruby v3.4.2
|
179
|
+
|
180
|
+
# == Averaged ============================================================
|
181
|
+
# ...
|
182
|
+
#
|
149
183
|
# ObjectIdentifier::StringFormatter
|
150
|
-
#
|
184
|
+
# 38.646k (± 1.3%) i/s (25.88 μs/i) - 196.758k in 5.092094s
|
151
185
|
# MyCustomFormatter
|
152
|
-
#
|
186
|
+
# ...
|
153
187
|
```
|
154
188
|
|
155
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]"
|
@@ -31,7 +34,7 @@ class Object
|
|
31
34
|
#
|
32
35
|
# (1..10).to_a.identify(:to_f, limit: 2)
|
33
36
|
# # => "Integer[to_f:1.0], Integer[to_f:2.0], ... (8 more)"
|
34
|
-
def identify(
|
35
|
-
ObjectIdentifier.(self,
|
37
|
+
def identify(...)
|
38
|
+
ObjectIdentifier.(self, ...)
|
36
39
|
end
|
37
40
|
end
|
@@ -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,14 +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
|
-
autorequire:
|
9
8
|
bindir: exe
|
10
9
|
cert_chain: []
|
11
|
-
date:
|
10
|
+
date: 2025-03-23 00:00:00.000000000 Z
|
12
11
|
dependencies:
|
13
12
|
- !ruby/object:Gem::Dependency
|
14
13
|
name: benchmark-ips
|
@@ -81,7 +80,6 @@ metadata:
|
|
81
80
|
source_code_uri: https://github.com/pdobb/object_identifier
|
82
81
|
homepage_uri: https://github.com/pdobb/object_identifier
|
83
82
|
rubygems_mfa_required: 'true'
|
84
|
-
post_install_message:
|
85
83
|
rdoc_options: []
|
86
84
|
require_paths:
|
87
85
|
- lib
|
@@ -89,15 +87,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
89
87
|
requirements:
|
90
88
|
- - ">="
|
91
89
|
- !ruby/object:Gem::Version
|
92
|
-
version: '3.
|
90
|
+
version: '3.2'
|
93
91
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
94
92
|
requirements:
|
95
93
|
- - ">="
|
96
94
|
- !ruby/object:Gem::Version
|
97
95
|
version: '0'
|
98
96
|
requirements: []
|
99
|
-
rubygems_version: 3.
|
100
|
-
signing_key:
|
97
|
+
rubygems_version: 3.6.6
|
101
98
|
specification_version: 4
|
102
99
|
summary: Object Identifier identifies an object by its class name and attributes.
|
103
100
|
test_files: []
|