-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathattributes_doc.rb
58 lines (43 loc) · 1.52 KB
/
attributes_doc.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# 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