Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion lib/dry/transaction/instance_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,40 @@ module InstanceMethods
attr_reader :listeners
attr_reader :stack

def initialize(steps: (self.class.steps), listeners: nil, **operations)
def initialize(*args, steps: (self.class.steps), listeners: nil, **operations)
@steps = steps.map { |step|
operation = resolve_operation(step, operations)
step.with(operation: operation)
}
@operations = operations
@stack = Stack.new(@steps)
subscribe(listeners) unless listeners.nil?

next_super_method = method(__method__).super_method
super_initialize = nil

loop do
if next_super_method.owner.name =~ /Dry::Transaction/
next_super_method = next_super_method.super_method
else
super_initialize = next_super_method
break
end
end

if super_initialize && super_initialize.parameters.empty?
super()
elsif super_initialize
super_kwarg_names = super_initialize.parameters.each_with_object([]) { |(type, name), names|
names << name if [:key, :keyreq].include?(type)
}

super_kwargs = operations.each_with_object({}) { |(key, val), kwargs|
kwargs[key] = val if super_kwarg_names.include?(key)
}

super(*args, **super_kwargs)
end
end

def call(input = nil, &block)
Expand Down
25 changes: 25 additions & 0 deletions spec/integration/user_class_hierarchy_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
RSpec.describe "dry transaction used with inheriting user classes" do
let(:base_class) do
Class.new do
attr_reader :base_called

def initialize(*_args)
@base_called = true
end
end
end

let(:child_class) do
Class.new(base_class) do
include Dry::Transaction
end
end

context "when user class inherits from another class" do
subject { child_class.new }

it "calls base class initializer" do
expect(subject.base_called).to be true
end
end
end