Skip to content

Commit

Permalink
Make chains comparable by #== based on contents
Browse files Browse the repository at this point in the history
This is useful behavior on its own, but is also necessary to ensure callbacks from a base app-level Hanami::Action class propogate all the way through a slice-level base class to an individual action class.
  • Loading branch information
timriley committed Nov 28, 2022
1 parent eea867f commit 7e7283d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
1 change: 1 addition & 0 deletions hanami-utils.gemspec
Expand Up @@ -20,6 +20,7 @@ Gem::Specification.new do |spec|
spec.metadata["rubygems_mfa_required"] = "true"
spec.required_ruby_version = ">= 3.0"

spec.add_dependency "dry-core", "~> 1.0", "< 2"
spec.add_dependency "dry-transformer", "~> 1.0", "< 2"
spec.add_dependency "concurrent-ruby", "~> 1.0"

Expand Down
5 changes: 5 additions & 0 deletions lib/hanami/utils/callbacks.rb
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require "concurrent/array"
require "dry/core"

module Hanami
module Utils
Expand All @@ -14,6 +15,8 @@ module Callbacks
# @since 0.1.0
# @private
class Chain
include Dry.Equalizer(:chain)

# Returns a new chain
#
# @return [Hanami::Utils::Callbacks::Chain]
Expand Down Expand Up @@ -235,6 +238,8 @@ def self.fabricate(callback)
# @since 0.1.0
# @api private
class Callback
include Dry.Equalizer(:callback)

# @api private
attr_reader :callback

Expand Down
11 changes: 11 additions & 0 deletions spec/unit/hanami/utils/callbacks_spec.rb
Expand Up @@ -282,6 +282,17 @@ def set_article(params) # rubocop:disable Naming/AccessorMethodName
expect { @chain.append :authenticate! }.to raise_error RuntimeError
end
end

describe "#==" do
it "is equal to another chain based on its contents" do
@chain.append :callback_a, :callback_b

other = @chain.class.new
other.append :callback_a, :callback_b

expect(@chain).to eq other
end
end
end

RSpec.describe Hanami::Utils::Callbacks::Factory do
Expand Down

0 comments on commit 7e7283d

Please sign in to comment.