Sha256: e8c9a91522f3ee4e8734f579000b50f443060eb9bfd78c103052ae79121ac2f0

Contents?: true

Size: 1.99 KB

Versions: 18

Compression:

Stored size: 1.99 KB

Contents

module Dry
  module Mutations
    module Transactions # :nodoc:
      # http://dry-rb.org/gems/dry-transaction/basic-usage/
      # save_user = Dry.Transaction(container: Container) do
      #   step :process
      #   step :validate
      #   step :persist
      # end

      module DSL # :nodoc:
        include Dry::Monads::Either::Mixin

        # rubocop:disable Style/MultilineIfModifier
        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
        # rubocop:enable Style/MultilineIfModifier

        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
      end
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
dry-mutations-1.6.0 lib/dry/mutations/transactions/dsl.rb
dry-mutations-1.5.7 lib/dry/mutations/transactions/dsl.rb
dry-mutations-1.5.6 lib/dry/mutations/transactions/dsl.rb
dry-mutations-1.5.5 lib/dry/mutations/transactions/dsl.rb
dry-mutations-1.5.4 lib/dry/mutations/transactions/dsl.rb
dry-mutations-1.5.3 lib/dry/mutations/transactions/dsl.rb
dry-mutations-1.5.2 lib/dry/mutations/transactions/dsl.rb
dry-mutations-1.5.1 lib/dry/mutations/transactions/dsl.rb
dry-mutations-1.5.0 lib/dry/mutations/transactions/dsl.rb
dry-mutations-1.3.6 lib/dry/mutations/transactions/dsl.rb
dry-mutations-1.3.5 lib/dry/mutations/transactions/dsl.rb
dry-mutations-1.3.4 lib/dry/mutations/transactions/dsl.rb
dry-mutations-1.3.3 lib/dry/mutations/transactions/dsl.rb
dry-mutations-1.3.2 lib/dry/mutations/transactions/dsl.rb
dry-mutations-1.3.1 lib/dry/mutations/transactions/dsl.rb
dry-mutations-1.1.3 lib/dry/mutations/transactions/dsl.rb
dry-mutations-1.1.1 lib/dry/mutations/transactions/dsl.rb
dry-mutations-1.1.0 lib/dry/mutations/transactions/dsl.rb