Sha256: 599f4e52100400454e44df16393067f52d3c25fedd0f52942a306b8e12a2fd9e

Contents?: true

Size: 1.68 KB

Versions: 1

Compression:

Stored size: 1.68 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} used inside of
    # The {Platform}.
    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.1.0 lib/roda/endpoints/transactions.rb