Module: Dry::Mutations::Transactions::DSL

Includes:
Dry::Monads::Either::Mixin
Defined in:
lib/dry/mutations/transactions/dsl.rb

Overview

dry-rb.org/gems/dry-transaction/basic-usage/ save_user = Dry.Transaction(container: Container) do

step :process
step :validate
step :persist

end

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object

rubocop:disable Style/MultilineIfModifier



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/dry/mutations/transactions/dsl.rb', line 15

def self.extended base
  fail Errors::TypeError.new("Extended class [#{base}] should not respond to :call, it is defined by this extension.") if base.respond_to?(:call)
  base.send :define_method, :initialize do |*input|
    @input = Utils.RawInputs(*input)
  end unless base.instance_methods(false).include?(:initialize)
  %i(call run run!).each do |meth|
    base.send :define_method, meth do
      base.public_send(meth, @input)
    end unless base.instance_methods(false).include?(meth)
  end
end

Instance Method Details

#chain(**params) ⇒ Object

rubocop:enable Style/MultilineIfModifier



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/dry/mutations/transactions/dsl.rb', line 28

def chain **params
  return enum_for(:chain) unless block_given? # FIXME: Needed? Works? Remove?

  # rubocop:disable Style/VariableNumber
  λ = Proc.new

  @transaction = ::Dry.Transaction(
    container: ::Dry::Mutations::Transactions::Container,
    step_adapters: StepAdapters
  ) do
    instance_eval(&λ)
  end.tap do |transaction|
    singleton_class.send :define_method, :call do |*input|
      transaction.(Utils.RawInputs(*input))
    end
    singleton_class.send :define_method, :run do |*input|
      ::Dry::Mutations::Extensions::Outcome(transaction.(Utils.RawInputs(*input)))
    end
    singleton_class.send :define_method, :run! do |*input|
      ::Dry::Mutations::Extensions::Outcome!(transaction.(Utils.RawInputs(*input)))
    end
  end
  # rubocop:enable Style/VariableNumber
end