Class: Grape::Validations::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



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/grape/validations/attributes_doc.rb', line 36

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
32
33
34
# 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)

  details[:min_length] = validations[:length][:min] if validations.key?(:length) && validations[:length].key?(:min)
  details[:max_length] = validations[:length][:max] if validations.key?(:length) && validations[:length].key?(:max)
end

#requiredObject



49
50
51
# File 'lib/grape/validations/attributes_doc.rb', line 49

def required
  details[:required]
end