Class: GrapeSwagger::DocMethods::DataType

Inherits:
Object
  • Object
show all
Defined in:
lib/grape-swagger/doc_methods/data_type.rb

Constant Summary collapse

PRIMITIVE_MAPPINGS =
{
  'integer' => %w(integer int32),
  'long' => %w(integer int64),
  'float' => %w(number float),
  'double' => %w(number double),
  'byte' => %w(string byte),
  'date' => %w(string date),
  'dateTime' => %w(string date-time),
  'binary' => %w(string binary),
  'password' => %w(string password),
  'email' => %w(string email)
}.freeze

Class Method Summary collapse

Class Method Details

.call(value) ⇒ Object



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
# File 'lib/grape-swagger/doc_methods/data_type.rb', line 5

def call(value)
  raw_data_type = value.is_a?(Hash) ? value[:type] : value
  raw_data_type ||= 'String'
  raw_data_type = parse_multi_type(raw_data_type)

  case raw_data_type.to_s
  when 'Boolean', 'Date', 'Integer', 'String', 'Float', 'JSON', 'Array'
    raw_data_type.to_s.downcase
  when 'Hash'
    'object'
  when 'Rack::Multipart::UploadedFile', 'File'
    'file'
  when 'Virtus::Attribute::Boolean'
    'boolean'
  when 'BigDecimal'
    'double'
  when 'DateTime', 'Time'
    'dateTime'
  when 'Numeric'
    'long'
  when 'Symbol'
    'string'
  else
    parse_entity_name(raw_data_type)
  end
end

.mapping(value) ⇒ Object



70
71
72
# File 'lib/grape-swagger/doc_methods/data_type.rb', line 70

def mapping(value)
  PRIMITIVE_MAPPINGS[value] || 'string'
end

.parse_entity_name(model) ⇒ Object



43
44
45
46
47
48
49
50
51
52
# File 'lib/grape-swagger/doc_methods/data_type.rb', line 43

def parse_entity_name(model)
  if model.respond_to?(:entity_name)
    model.entity_name
  else
    name = model.to_s
    entity_parts = name.split('::')
    entity_parts.reject! { |p| p == 'Entity' || p == 'Entities' }
    entity_parts.join('::')
  end
end

.parse_multi_type(raw_data_type) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/grape-swagger/doc_methods/data_type.rb', line 32

def parse_multi_type(raw_data_type)
  case raw_data_type
  when /\A\[.*\]\z/
    raw_data_type.gsub(/[(\A\[)(\s+)(\]\z)]/, '').split(',').first
  when Array
    raw_data_type.first
  else
    raw_data_type
  end
end

.primitive?(type) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/grape-swagger/doc_methods/data_type.rb', line 58

def primitive?(type)
  primitives.include?(type.to_s.downcase)
end

.primitivesObject



66
67
68
# File 'lib/grape-swagger/doc_methods/data_type.rb', line 66

def primitives
  PRIMITIVE_MAPPINGS.keys.map(&:downcase)
end

.request_primitive?(type) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/grape-swagger/doc_methods/data_type.rb', line 54

def request_primitive?(type)
  request_primitives.include?(type.to_s.downcase)
end

.request_primitivesObject



62
63
64
# File 'lib/grape-swagger/doc_methods/data_type.rb', line 62

def request_primitives
  primitives + %w(object string boolean file json array)
end