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),
  'uuid' => %w(string uuid)
}.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

.collectionsObject



80
81
82
# File 'lib/grape-swagger/doc_methods/data_type.rb', line 80

def collections
  %w(csv ssv tsv pipes multi)
end

.mapping(value) ⇒ Object



76
77
78
# File 'lib/grape-swagger/doc_methods/data_type.rb', line 76

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

.parse_entity_name(model) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/grape-swagger/doc_methods/data_type.rb', line 48

def parse_entity_name(model)
  if model.methods(false).include?(:entity_name)
    model.entity_name
  elsif model.to_s.end_with?('::Entity', '::Entities')
    model.to_s.split('::')[-2]
  elsif model.respond_to?(:name)
    model.name.demodulize.camelize
  else
    model.to_s.split('::').last
  end
end

.parse_multi_type(raw_data_type) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# 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/
    type_as_string = raw_data_type.gsub(/[\[\s+\]]/, '').split(',').first
    begin
      Object.const_get(type_as_string)
    rescue NameError
      type_as_string
    end
  when Array
    raw_data_type.first
  else
    raw_data_type
  end
end

.primitive?(type) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/grape-swagger/doc_methods/data_type.rb', line 64

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

.primitivesObject



72
73
74
# File 'lib/grape-swagger/doc_methods/data_type.rb', line 72

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

.request_primitive?(type) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/grape-swagger/doc_methods/data_type.rb', line 60

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

.request_primitivesObject



68
69
70
# File 'lib/grape-swagger/doc_methods/data_type.rb', line 68

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