Sha256: e7b0bb8456a89cdaf2470ed82adf37828e86a4f00da76ac6d95a1c2efe9d2629

Contents?: true

Size: 1.92 KB

Versions: 1

Compression:

Stored size: 1.92 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] verb
        # @return [String]
        def transaction_for(verb)
          %W(
            transactions.#{ns}.#{verb}
            transactions.#{type}.#{verb}
          ).detect { |key| container.key?(key) }
        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

1 entries across 1 versions & 1 rubygems

Version Path
roda-endpoints-0.1.0 lib/roda/endpoints/endpoint/transactions.rb