Sha256: 965af5006a676323e16321fc0bb0f587d1d98ef7df2f4d512bd15d4b5f579b14

Contents?: true

Size: 1.68 KB

Versions: 2

Compression:

Stored size: 1.68 KB

Contents

# frozen_string_literal: true

require 'roda/endpoints/transactions'

class Roda
  module Endpoints
    # Generic HTTP endpoint abstraction.
    class Endpoint
      # Namespacing operations, validations, etc.
      module Transactions
        # @return [Endpoints::Transactions]
        def transactions
          endpoint = self
          @transactions ||=
            begin
              transactions = Endpoints::Transactions.new(endpoint: self)
              self.class.transactions.each do |(name, block)|
                transactions.define(name) { instance_exec(endpoint, &block) }
              end
              transactions
            end
          yield @transaction if block_given?
          @transactions
        end

        # @param [Symbol, String] name
        # @param [Proc] block
        def step(name, only: [], **kwargs, &block)
          name = "operations.#{ns}.#{name}"
          container.register(name, &block) if block_given?
          verbs = Array(only).flatten
          verbs.each do |verb|
            result = transactions[verb].insert(container: container, **kwargs) do
              step name
            end
            @transaction = result
          end
          transactions
        end

        def before(name, **kwargs, &block)
          step "before_#{name}", before: name, **kwargs, &block
        end

        def after(name, **kwargs, &block)
          step "after_#{name}", after: name, **kwargs, &block
        end

        # @param [Symbol] operation
        # @param [Array] args
        # @return [Dry::Monads::Either]
        def perform(operation, *args, **options)
          transactions[operation].call(*args, **options)
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
roda-endpoints-0.3.0 lib/roda/endpoints/endpoint/transactions.rb
roda-endpoints-0.2.0 lib/roda/endpoints/endpoint/transactions.rb