Skip to content

Commit

Permalink
Fix Dry::Core::Equalizer() usage (#79)
Browse files Browse the repository at this point in the history
## Fix Dry::Core::Equalizer() usage

Provide a `Dry::Core.Equalizer` method that builds the module when called as `include Dry::Core::Equalizer()` as documented. Previously, this method did not exist at all, so _only_ usages of the "legacy" `Dry.Equalizer` method would work. With this change, we can now appropriately expect `Dry::Core.Equalizer` to be the mainstream usage.

To ensure this, update the specs to expect `Dry::Core::Equalizer()` to be used by default, and provide a single spec to ensure that the legacy `Dry::Equalizer()` method continues to work.

Also update specs to remove direct requires of `dry/core/equalizer`, since a require of `dry/core` alone should be sufficient to load Equalizer when it is used, given we use Zeitwerk.

## Remove all cherry-picked requires from specs and docs

While we're here, make the equivalent change of removing cherry-picked requires from all our other modules, both in specs and docs. These are no longer needed since we use Zeitwerk. A simple `require "dry/core"` alone is enough.
  • Loading branch information
timriley authored Aug 6, 2023
1 parent 2aeb72c commit 381c7c2
Show file tree
Hide file tree
Showing 20 changed files with 42 additions and 32 deletions.
2 changes: 1 addition & 1 deletion docsite/source/cache.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ name: dry-core
Allows you to cache call results that are solely determined by arguments.

```ruby
require 'dry/core/cache'
require "dry/core"

class Foo
extend Dry::Core::Cache
Expand Down
2 changes: 1 addition & 1 deletion docsite/source/deprecations.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ name: dry-core
To deprecate ruby methods you need to extend the `Dry::Core::Deprecations` module with a tag that will be displayed in the output. For example:

```ruby
require 'dry/core/deprecations'
require "dry/core"

class Foo
extend Dry::Core::Deprecations[:tag]
Expand Down
2 changes: 1 addition & 1 deletion docsite/source/equalizer.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ A simple mixin that can be used to add instance variable based equality, equival
### Usage

```ruby
require 'dry/core/equalizer'
require "dry/core"

class GeoLocation
include Dry::Core::Equalizer(:latitude, :longitude)
Expand Down
2 changes: 1 addition & 1 deletion docsite/source/extensions.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ name: dry-core
Define extensions that can be later enabled by the user.

```ruby
require 'dry/core/extensions'
require "dry/core"

class Foo
extend Dry::Core::Extensions
Expand Down
11 changes: 11 additions & 0 deletions lib/dry/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ def self.loader
end

loader.setup

# Build an equalizer module for the inclusion in other class
#
# ## Credits
#
# Equalizer has been originally imported from the equalizer gem created by Dan Kubb
#
# @api public
def self.Equalizer(*keys, **options)
Equalizer.new(*keys, **options)
end
end

# See dry/core/equalizer.rb
Expand Down
4 changes: 1 addition & 3 deletions spec/dry/core/basic_object_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
# frozen_string_literal: true

require "dry/core/basic_object"
require "pp"

class ExternalTestClass
end

Expand Down Expand Up @@ -78,6 +75,7 @@ def respond_to?(_method_name, _include_all = false) # rubocop:disable Style/Opti

# See https://github.com/hanami/utils/issues/234
it "outputs the inspection to the given printer" do
require "pp" # rubucop:disable Lint/RedundantRequireStatement
printer = PP.new
subject = TestClass.new
subject.pretty_print(printer)
Expand Down
2 changes: 0 additions & 2 deletions spec/dry/core/cache_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# frozen_string_literal: true

require "dry/core/cache"

RSpec.describe Dry::Core::Cache do
shared_examples_for "class with cache" do
describe "#fetch_or_store" do
Expand Down
1 change: 0 additions & 1 deletion spec/dry/core/class_attributes_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# frozen_string_literal: true

require "dry/core/class_attributes"
require "dry-types"

RSpec.describe "Class Macros" do
Expand Down
2 changes: 0 additions & 2 deletions spec/dry/core/class_builder_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# frozen_string_literal: true

require "dry/core/class_builder"

RSpec.describe Dry::Core::ClassBuilder do
subject(:builder) { described_class.new(**options) }

Expand Down
2 changes: 0 additions & 2 deletions spec/dry/core/constants_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# frozen_string_literal: true

require "dry/core/constants"

RSpec.describe Dry::Core::Constants do
before do
class ClassWithConstants
Expand Down
1 change: 0 additions & 1 deletion spec/dry/core/deprecations_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# frozen_string_literal: true

require "dry/core/deprecations"
require "tempfile"

RSpec.describe Dry::Core::Deprecations do
Expand Down
2 changes: 0 additions & 2 deletions spec/dry/core/descendants_tracker_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# frozen_string_literal: true

require "dry/core/descendants_tracker"

RSpec.describe Dry::Core::DescendantsTracker do
before do
module Test
Expand Down
3 changes: 1 addition & 2 deletions spec/dry/core/equalizer/included_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# frozen_string_literal: true

require "spec_helper"
require "dry/core/equalizer"

RSpec.describe Dry::Core::Equalizer, "#included" do
subject { descendant.instance_exec(object) { |mod| include mod } }
Expand Down Expand Up @@ -33,7 +32,7 @@

superclass.class_eval do
define_method(:included) do |_|
# Only set the flag when an Dry::Equalizer instance is included.
# Only set the flag when an Dry::Core::Equalizer instance is included.
# Otherwise, other module includes (which get triggered internally
# in RSpec when `change` is used for the first time, since it uses
# autoloading for its matchers) will wrongly set this flag.
Expand Down
20 changes: 20 additions & 0 deletions spec/dry/core/equalizer/legacy_name_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

RSpec.describe "legacy Dry::Equalizer name" do
it "behaves the same as when using Dry::Core::Equalizer" do
klass = Class.new {
attr_reader :name

def initialize(name)
@name = name
end
}
allow(klass).to receive_messages(name: nil, inspect: "User") # specify the class #inspect method
klass.include Dry::Equalizer(:name)

instance = klass.new("Jane")
other = instance.dup

expect(instance).to eql(other)
end
end
1 change: 0 additions & 1 deletion spec/dry/core/equalizer/methods/eql_predicate_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# frozen_string_literal: true

require "spec_helper"
require "dry/core/equalizer"

RSpec.describe Dry::Core::Equalizer::Methods, "#eql?" do
subject { object.eql?(other) }
Expand Down
1 change: 0 additions & 1 deletion spec/dry/core/equalizer/methods/equality_operator_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# frozen_string_literal: true

require "spec_helper"
require "dry/core/equalizer"

RSpec.describe Dry::Core::Equalizer::Methods, "#==" do
subject { object == other }
Expand Down
11 changes: 5 additions & 6 deletions spec/dry/core/equalizer/universal_spec.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
# frozen_string_literal: true

require "spec_helper"
require "dry/core/equalizer"

RSpec.describe Dry::Core::Equalizer do
let(:name) { "User" }
let(:klass) { ::Class.new }

context "with no keys" do
subject { Dry::Equalizer() }
subject { Dry::Core::Equalizer() }

before do
# specify the class #name method
Expand Down Expand Up @@ -73,7 +72,7 @@
end

context "with keys" do
subject { Dry::Equalizer(*keys) }
subject { Dry::Core::Equalizer(*keys) }

let(:keys) { %i[firstname lastname].freeze }
let(:firstname) { "John" }
Expand Down Expand Up @@ -158,7 +157,7 @@ def initialize(firstname, lastname)

context "when immutable" do
describe "#hash" do
subject { Dry::Equalizer(*keys, immutable: true) }
subject { Dry::Core::Equalizer(*keys, immutable: true) }

it "returns memoized hash" do
expect { instance.firstname = "Changed" }.not_to(change { instance.hash })
Expand All @@ -177,7 +176,7 @@ def initialize(firstname, lastname)
end

context "with duplicate keys" do
subject { Dry::Equalizer(*keys) }
subject { Dry::Core::Equalizer(*keys) }

let(:keys) { %i[firstname firstname lastname].freeze }
let(:firstname) { "John" }
Expand Down Expand Up @@ -216,7 +215,7 @@ def initialize(firstname, lastname)

context "with options" do
context "w/o inspect" do
subject { Dry::Equalizer(*keys, inspect: false) }
subject { Dry::Core::Equalizer(*keys, inspect: false) }

let(:keys) { %i[firstname lastname].freeze }
let(:firstname) { "John" }
Expand Down
2 changes: 0 additions & 2 deletions spec/dry/core/extensions_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# frozen_string_literal: true

require "dry/core/extensions"

RSpec.describe Dry::Core::Extensions do
subject do
Class.new do
Expand Down
2 changes: 0 additions & 2 deletions spec/dry/core/inflector_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# frozen_string_literal: true

require "dry/core/inflector"

RSpec.describe Dry::Core::Inflector do
shared_examples "an inflector" do
it "singularises" do
Expand Down
1 change: 0 additions & 1 deletion spec/dry/core/memoizable_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# frozen_string_literal: true

require "concurrent/atomic/atomic_fixnum"
require "dry/core/memoizable"
require "tempfile"
require_relative "../../support/memoized"

Expand Down

0 comments on commit 381c7c2

Please sign in to comment.