Module: JSONAPI::TypeConversion

Defined in:
lib/json_api/support/type_conversion.rb

Class Method Summary collapse

Class Method Details

.format_type_name(full_name, format) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/json_api/support/type_conversion.rb', line 36

def format_type_name(full_name, format)
  case format
  when :prefixed
    full_name
  when :underscore
    full_name.tr("/", "_")
  else # :flat
    full_name.split("/").last
  end
end

.model_type_name(model_class, format: nil) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/json_api/support/type_conversion.rb', line 20

def model_type_name(model_class, format: nil)
  format ||= JSONAPI.configuration.namespace_type_format

  full_name = model_class.name.underscore.pluralize

  format_type_name(full_name, format)
end

.prefixed_type_to_class(type_str) ⇒ Object



15
16
17
18
# File 'lib/json_api/support/type_conversion.rb', line 15

def prefixed_type_to_class(type_str)
  parts = type_str.split("/")
  "#{parts[0..-2].join("/").camelize}::#{parts.last.singularize.classify}"
end

.resolve_type_format(definition_class) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/json_api/support/type_conversion.rb', line 47

def resolve_type_format(definition_class)
  if definition_class.respond_to?(:type_format) && definition_class.type_format
    definition_class.type_format
  else
    JSONAPI.configuration.namespace_type_format
  end
end

.resource_type_name(definition_class, format: nil) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/json_api/support/type_conversion.rb', line 28

def resource_type_name(definition_class, format: nil)
  format ||= resolve_type_format(definition_class)

  full_name = definition_class.name.sub(/Resource$/, "").underscore.pluralize

  format_type_name(full_name, format)
end

.type_to_class_name(type, namespace: nil) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/json_api/support/type_conversion.rb', line 7

def type_to_class_name(type, namespace: nil)
  type_str = type.to_s
  return prefixed_type_to_class(type_str) if type_str.include?("/")
  return "#{namespace.to_s.camelize}::#{type_str.singularize.classify}" if namespace.present?

  type_str.singularize.classify
end