Sha256: 513156680427bed12da1a91d9fa3a7e52579c467e090814cfded3a15d7755b3b

Contents?: true

Size: 1.66 KB

Versions: 1

Compression:

Stored size: 1.66 KB

Contents

# frozen_string_literal: true

require 'roda/endpoints'
require 'dry-configurable'
require 'dry-transaction'
require 'forwardable'

class Roda
  module Endpoints
    # The DSL for defining {Transactions transactions} for endpoints.
    class Transactions
      extend Dry::Configurable
      extend Forwardable

      setting :container
      setting :options, {}

      def self.define
        yield new
      end

      # @return [{Symbol=>Object}]
      def self.options
        { container: config.container }.merge(config.options)
      end

      # @param [Endpoint] endpoint
      # @param [Hash] options
      def initialize(endpoint:, **options)
        @endpoint = endpoint
        @container = endpoint.container
        @options = self.class.options
                       .merge(container: endpoint.container)
                       .merge(options)
      end

      # @return [{Symbol=>Object}]
      attr_reader :options

      # @return [Endpoint]
      attr_reader :endpoint

      # @return [Dry::Container::Mixin]
      attr_reader :container

      def_delegators :endpoint, :operation_for, :validation_for

      # @param [Symbol] shortcut
      # @param [Proc] block
      def define(shortcut, &block)
        container.register(
          key_for(shortcut),
          Dry.Transaction(
            container: container,
            endpoint: endpoint,
            &block
          )
        )
      end

      # @param [Symbol] verb
      # @return [String]
      def key_for(verb)
        "transactions.#{endpoint.ns}.#{verb}"
      end

      # @param [Symbol] shortcut
      def [](shortcut)
        key = key_for(shortcut)
        container[key]
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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