Sha256: ba1923e80f4ddf977e8f50de018db3bfaba916dd6e544f72c3c5fb82a1bf68d1

Contents?: true

Size: 1.52 KB

Versions: 4

Compression:

Stored size: 1.52 KB

Contents

# frozen_string_literal: true

module Grape
  module Validations
    class ParamsScope
      # Documents parameters of an endpoint. If documentation isn't needed (for instance, it is an
      # internal API), the class only cleans up attributes to avoid junk in RAM.
      class AttributesDoc
        attr_accessor :type, :values

        # @param api [Grape::API::Instance]
        # @param scope [Validations::ParamsScope]
        def initialize(api, scope)
          @api = api
          @scope = scope
          @type = type
        end

        def extract_details(validations)
          details[:required] = validations.key?(:presence)

          desc = validations.delete(:desc) || validations.delete(:description)

          details[:desc] = desc if desc

          documentation = validations.delete(:documentation)

          details[:documentation] = documentation if documentation

          details[:default] = validations[:default] if validations.key?(:default)
        end

        def document(attrs)
          return if @api.namespace_inheritable(:do_not_document)

          details[:type] = type.to_s if type
          details[:values] = values if values

          documented_attrs = attrs.each_with_object({}) do |name, memo|
            memo[@scope.full_name(name)] = details
          end

          @api.namespace_stackable(:params, documented_attrs)
        end

        def required
          details[:required]
        end

        protected

        def details
          @details ||= {}
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
grape-2.0.0 lib/grape/validations/attributes_doc.rb
grape-1.8.0 lib/grape/validations/attributes_doc.rb
grape-1.7.1 lib/grape/validations/attributes_doc.rb
grape-1.7.0 lib/grape/validations/attributes_doc.rb