Class: Grape::Validations::ParamsScope::AttributesDoc

Inherits:
Object
  • Object
show all
Defined in:
lib/grape/validations/attributes_doc.rb

Overview

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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api, scope) ⇒ AttributesDoc

Returns a new instance of AttributesDoc.


13
14
15
16
17
# File 'lib/grape/validations/attributes_doc.rb', line 13

def initialize(api, scope)
  @api = api
  @scope = scope
  @type = type
end

Instance Attribute Details

#typeObject

Returns the value of attribute type.


9
10
11
# File 'lib/grape/validations/attributes_doc.rb', line 9

def type
  @type
end

#valuesObject

Returns the value of attribute values.


9
10
11
# File 'lib/grape/validations/attributes_doc.rb', line 9

def values
  @values
end

Instance Method Details

#document(attrs) ⇒ Object


33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/grape/validations/attributes_doc.rb', line 33

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

#extract_details(validations) ⇒ Object


19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/grape/validations/attributes_doc.rb', line 19

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

#requiredObject


46
47
48
# File 'lib/grape/validations/attributes_doc.rb', line 46

def required
  details[:required]
end