Skip to content

Commit

Permalink
No longer define/use #inspect_lit
Browse files Browse the repository at this point in the history
Just use #inspect instead of #inspect_lit. Remove #inspect_lit from core
extensions on: BigDecimal, Symbol, String, and Object.

Because:
- My String#inspect_lit appeared to be identical to String#inspect. At
  least now it is? I'm unsure if it was ever different.
- My Symbol#inspect_lit produced a less-obvious version of a Symbol, to
  my eyes--at this point.
- BigDecimal#inspect_lit was.. helpful-ish. But the default #inspect
  output will be more familiar to those that use it more often, so it's
  not really worth enforcing our own.
- Given that all of these are gone, we no longer need to override Object
  to define #inspect_lit either.

It's better to do less overriding of core classes.
So. 👋
  • Loading branch information
pdobb committed Nov 23, 2024
1 parent 329df6e commit 3f8cc17
Show file tree
Hide file tree
Showing 13 changed files with 11 additions and 156 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## [Unreleased]

- Just use #inspect instead of #inspect_lit. Remove #inspect_lit from core extensions on: BigDecimal, Symbol, String, and Object.

### 0.8.0 - 2024-11-21

- Update minimum Ruby version from 2.7 -> 3.1
Expand Down
30 changes: 0 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,36 +132,6 @@ The number of results that will be identified from a collection can be truncated
{}.identify # => [no objects]
```

## Custom Object Identifiers

Internally, Object Identifier calls `inspect_lit` to return a "literally-inspected" string representation of an object. This works because Object, itself, is monkey-patched to define `inspect_lit` which just returns `inspect`. This is sufficient for most objects, but some objects will benefit from defining special output from `inspect_lit`.

Object Identifier defines `inspect_lit` on three other core objects: String, Symbol, and BigDecimal.

```ruby
"a_string".inspect_lit # => "\"a_string\""
:a_symbol.inspect_lit # => ":\"a_symbol\""
BigDecimal(1.99, 3).inspect_lit # => "<BD:1.99>"
```

To identify an object in a special way, just define `inspect_lit` to return a custom String.

```ruby
class MyValueObject
def initialize(val)
@val = val
end

def inspect_lit
"#{@val} Meters"
end
end

my_value_object = MyValueObject.new(42)
OpenStruct.new(my_value: my_value_object).identify(:my_value)
# => "OpenStruct[my_value:42 Meters]"
```

## Supporting Gems

Object Identifier works great with the [Object Inspector](https://github.com/pdobb/object_inspector) gem.
Expand Down
17 changes: 0 additions & 17 deletions lib/core_ext/big_decimal.rb

This file was deleted.

8 changes: 0 additions & 8 deletions lib/core_ext/object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,6 @@

# Reopen the core Object class to add {#identify} to all objects.
class Object
# Standard #inspect for any object that doesn't override this method. This
# method is meant to make an object's type inherently obvious inspected.
#
# @return [String] a String-literal representation of this object
def inspect_lit
inspect
end

# Instance method for constructing a self-identifying string for any given
# object or list of objects.
#
Expand Down
17 changes: 0 additions & 17 deletions lib/core_ext/string.rb

This file was deleted.

16 changes: 0 additions & 16 deletions lib/core_ext/symbol.rb

This file was deleted.

3 changes: 0 additions & 3 deletions lib/object_identifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,3 @@ module ObjectIdentifier
# CORE EXTENSIONS

require "core_ext/object"
require "core_ext/string"
require "core_ext/symbol"
require "core_ext/big_decimal"
12 changes: 6 additions & 6 deletions lib/object_identifier/formatters/string_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ def call

private

def format_collection
Collection.new(objects, parameters).call
end

def format_item(object = objects.first)
Item.new(object, parameters).call
end

def format_collection
Collection.new(objects, parameters).call
end

# ObjectIdentifier::StringFormatter::Collection formats a collection-specific
# identification String, which will also necessarily be composed of
# {ObjectIdentifier::StringFormatter::Item} identification Strings.
Expand Down Expand Up @@ -122,9 +122,9 @@ def formatted_attributes
def attributes_formatter
@attributes_formatter ||=
if attributes_hash.one?
->(_key, value) { value.inspect_lit }
->(_key, value) { value.inspect }
else # attributes_hash.size > 1
->(key, value) { "#{key}:#{value.inspect_lit}" }
->(key, value) { "#{key}:#{value.inspect}" }
end
end

Expand Down
16 changes: 0 additions & 16 deletions test/core_ext/big_decimal_test.rb

This file was deleted.

14 changes: 0 additions & 14 deletions test/core_ext/object_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,6 @@ class ObjectTest < Minitest::Spec
describe Object do
subject { Object.new }

describe "#inspect_lit" do
before do
MuchStub.on_call(subject, :inspect) { |call|
@object_inspect_call = call
}
end

it "calls inspect on self" do
subject.inspect_lit

_(@object_inspect_call.args).must_equal([])
end
end

describe "#identify" do
before do
MuchStub.on_call(ObjectIdentifier, :call) { |call|
Expand Down
13 changes: 0 additions & 13 deletions test/core_ext/string_test.rb

This file was deleted.

13 changes: 0 additions & 13 deletions test/core_ext/symbol_test.rb

This file was deleted.

6 changes: 3 additions & 3 deletions test/object_identifier/formatters/string_formatter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ def parameterize(attributes = [], **formatter_options)
it "quotes symbols in attributes" do
object = OpenStruct.new(name: "TestName", attr1: :value1)
_(subject.call(object, **parameterize(:attr1))).must_equal(
%(OpenStruct[:"value1"]))
%(OpenStruct[:value1]))
end

it "ignores attributes that don't exist" do
object = OpenStruct.new(name: "TestName", attr1: :value1)
_(subject.call(object, **parameterize(%i[attr1 unknown_attr1]))).
must_equal(%(OpenStruct[:"value1"]))
must_equal(%(OpenStruct[:value1]))
end

it "returns the value of instance variables" do
Expand Down Expand Up @@ -75,7 +75,7 @@ def parameterize(attributes = [], **formatter_options)
object, **parameterize(%i[name attr1 @var1]))

expected_attributes_string =
%(name:"TestName", attr1::"value1", @var1:1)
%(name:"TestName", attr1::value1, @var1:1)
_(result).must_equal("OpenStruct[#{expected_attributes_string}]")
end
end
Expand Down

0 comments on commit 3f8cc17

Please sign in to comment.