Module: Contentful::DatabaseImporter::ResourceBootstrapClassMethods

Defined in:
lib/contentful/database_importer/resource_bootstrap_class_methods.rb

Overview

Bootstrap related Class Methods

Constant Summary collapse

TYPE_MAPPINGS =
{
  symbol: 'Symbol',
  text: 'Text',
  date: 'Date',
  object: 'Object',
  location: 'Location',
  link: 'Link',
  integer: 'Integer',
  number: 'Number',
  boolean: 'Boolean'
}.freeze

Instance Method Summary collapse

Instance Method Details

#array?(field_data) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
64
65
# File 'lib/contentful/database_importer/resource_bootstrap_class_methods.rb', line 61

def array?(field_data)
  field_data[:type] == :array ||
    (resource?(field_data[:type]) &&
     %i[many through].include?(field_data[:relationship]))
end

#array_link?(field_data) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
49
50
# File 'lib/contentful/database_importer/resource_bootstrap_class_methods.rb', line 46

def array_link?(field_data)
  (field_data[:type] == :array && field_data[:item_type] == :asset) ||
    (resource?(field_data[:type]) &&
     %i[many through].include?(field_data[:relationship]))
end


52
53
54
55
# File 'lib/contentful/database_importer/resource_bootstrap_class_methods.rb', line 52

def array_link_type(field_data)
  return 'Asset' if field_data[:item_type] == :asset
  return 'Entry' if resource?(field_data[:type])
end

#basic_field_definition(field_data) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/contentful/database_importer/resource_bootstrap_class_methods.rb', line 76

def basic_field_definition(field_data)
  {
    id: field_data[:maps_to],
    name: field_data[:name],
    type: definition_type(field_data[:type])
  }
end

#content_type_definitionObject



98
99
100
101
102
103
104
105
# File 'lib/contentful/database_importer/resource_bootstrap_class_methods.rb', line 98

def content_type_definition
  {
    id: content_type_id,
    name: content_type_name,
    displayField: display_field,
    fields: fields_definition
  }
end

#definition_type(type) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/contentful/database_importer/resource_bootstrap_class_methods.rb', line 17

def definition_type(type)
  if type == :string
    type = :symbol
  elsif link?(type)
    type = :link
  end

  TYPE_MAPPINGS[type]
end

#field_definition(field_data) ⇒ Object



84
85
86
87
88
89
90
91
92
# File 'lib/contentful/database_importer/resource_bootstrap_class_methods.rb', line 84

def field_definition(field_data)
  definition = basic_field_definition(field_data)

  definition[:type] = 'Array' if array?(field_data)
  definition[:linkType] = link_type(field_data[:type]) if link_type?(field_data)
  definition[:items] = items_type(field_data) if array?(field_data)

  definition
end

#fields_definitionObject



94
95
96
# File 'lib/contentful/database_importer/resource_bootstrap_class_methods.rb', line 94

def fields_definition
  fields.reject { |f| f[:exclude_from_output] }.map { |f| field_definition(f) }
end

#items_type(field_data) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/contentful/database_importer/resource_bootstrap_class_methods.rb', line 34

def items_type(field_data)
  return { type: 'Link', linkType: array_link_type(field_data) } if array_link?(field_data)

  type = definition_type(field_data[:item_type])

  error = 'Array item type could not be mapped for '
  error += field_data[:maps_to].to_s
  raise error if type.nil?

  { type: type }
end

#link?(type) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/contentful/database_importer/resource_bootstrap_class_methods.rb', line 57

def link?(type)
  type == :asset || resource?(type)
end


27
28
29
30
31
32
# File 'lib/contentful/database_importer/resource_bootstrap_class_methods.rb', line 27

def link_type(type)
  return 'Asset' if type == :asset
  return 'Entry' if resource?(type)

  raise 'Type class is not a valid Link'
end

Returns:

  • (Boolean)


72
73
74
# File 'lib/contentful/database_importer/resource_bootstrap_class_methods.rb', line 72

def link_type?(field_data)
  link?(field_data[:type]) && !array_link?(field_data)
end

#resource?(other) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
70
# File 'lib/contentful/database_importer/resource_bootstrap_class_methods.rb', line 67

def resource?(other)
  return false unless other.respond_to?(:ancestors)
  other.ancestors.include?(::Contentful::DatabaseImporter::Resource)
end