Sha256: 8b12d9b74f4ff1545458117317fc2900476f3b992012b9ee86ece3c31d3dbdb1

Contents?: true

Size: 1.98 KB

Versions: 8

Compression:

Stored size: 1.98 KB

Contents

# frozen_string_literal: true

require 'roda/endpoints/endpoint/namespace'

class Roda
  module Endpoints
    class Endpoint
      # Lookup everything in the container.
      module Lookup
        prepend Namespace

        # @return [Dry::Container::Mixin]
        def container
          @container || parent&.container || Roda::Endpoints.roda_class
        end

        # @param [<String>] paths
        def lookup(*paths, key: nil, scope: nil, default: nil)
          if key && scope
            paths += lookup_keys(key: key, scope: scope, default: default)
          end
          paths.flatten.detect { |full_key| container.key?(full_key) }
        end

        # @param [#to_s] key
        # @param [#to_s] scope
        def lookup_keys(key:, scope: nil, default: nil, lookup: lookup_path)
          scope = "#{scope}." unless scope.to_s.end_with?('.')
          keys = []
          Array(key).flatten.map do |sect|
            lookup.each do |choice|
              [sect, default].compact.each do |value|
                full_key = "#{scope}#{choice}.#{value}"
                yield full_key if block_given?
                keys << full_key
              end
            end
          end
          keys
        end

        def lookup_path
          path = [ns]
          endpoint = self.class
          while endpoint < Roda::Endpoints::Endpoint
            path << endpoint.type
            endpoint = endpoint.superclass
          end
          path
        end

        # @param [Symbol] verb
        # @return [String]
        def operation_for(verb)
          lookup key: verb, scope: 'operations.'
        end

        # @param [Symbol] verb
        # @return [String]
        def validation_for(verb)
          lookup(key: verb, default: 'any', scope: 'validations.') ||
            provide_default_validation!
        end

        # @param [Symbol] verb
        # @return [String]
        def transaction_for(verb)
          lookup key: verb, scope: 'transactions'
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
roda-endpoints-0.3.6 lib/roda/endpoints/endpoint/lookup.rb
roda-endpoints-0.3.5 lib/roda/endpoints/endpoint/lookup.rb
roda-endpoints-0.3.4 lib/roda/endpoints/endpoint/lookup.rb
roda-endpoints-0.3.3 lib/roda/endpoints/endpoint/lookup.rb
roda-endpoints-0.3.2 lib/roda/endpoints/endpoint/lookup.rb
roda-endpoints-0.3.1 lib/roda/endpoints/endpoint/lookup.rb
roda-endpoints-0.3.0 lib/roda/endpoints/endpoint/lookup.rb
roda-endpoints-0.2.0 lib/roda/endpoints/endpoint/lookup.rb