Module: Contentful::DatabaseImporter::JsonGenerator

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

Overview

Json File Generator

Class Method Summary collapse

Class Method Details

.create_content_type(content_type_definition, result) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/contentful/database_importer/json_generator.rb', line 26

def self.create_content_type(content_type_definition, result)
  prev_ct_definition = result[:contentTypes].find do |ct|
    ct[:id] == content_type_definition[:id]
  end

  if prev_ct_definition
    result[:contentTypes].delete(prev_ct_definition)
    content_type_definition = Support.merge(
      prev_ct_definition, content_type_definition
    )
  end

  result[:contentTypes] << content_type_definition
end

.create_entries(resource, content_type_definition, result) ⇒ Object



41
42
43
44
45
46
# File 'lib/contentful/database_importer/json_generator.rb', line 41

def self.create_entries(resource, content_type_definition, result)
  result[:entries][content_type_definition[:id]] ||= []
  resource.all.each do |entry|
    create_entry(entry, content_type_definition, result)
  end
end

.create_entry(entry, content_type_definition, result) ⇒ Object



67
68
69
70
71
72
73
74
75
# File 'lib/contentful/database_importer/json_generator.rb', line 67

def self.create_entry(entry, content_type_definition, result)
  entry_definition = entry.to_bootstrap

  entry_definition = merge_entries(entry_definition, content_type_definition, result)

  result[:assets].concat(entry.associated_assets) unless entry.associated_assets.empty?

  result[:entries][content_type_definition[:id]] << entry_definition
end

.generate_jsonObject



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/contentful/database_importer/json_generator.rb', line 7

def self.generate_json
  result = { version: 3, contentTypes: [],
             assets: [], entries: {} }

  resources.each do |resource|
    content_type_definition = resource.content_type_definition
    create_content_type(content_type_definition, result)
    create_entries(resource, content_type_definition, result)
  end

  result
end

.generate_json!Object



77
78
79
# File 'lib/contentful/database_importer/json_generator.rb', line 77

def self.generate_json!
  JSON.pretty_generate(generate_json)
end

.merge_entries(entry_definition, content_type_definition, result) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/contentful/database_importer/json_generator.rb', line 54

def self.merge_entries(entry_definition, content_type_definition, result)
  prev_entry = previous_entry(
    entry_definition,
    content_type_definition,
    result
  )

  return entry_definition unless prev_entry

  result[:entries][content_type_definition[:id]].delete(prev_entry)
  Support.merge(prev_entry, entry_definition)
end

.previous_entry(entry_definition, content_type_definition, result) ⇒ Object



48
49
50
51
52
# File 'lib/contentful/database_importer/json_generator.rb', line 48

def self.previous_entry(entry_definition, content_type_definition, result)
  result[:entries][content_type_definition[:id]].find do |e|
    e[:sys][:id] == entry_definition[:sys][:id]
  end
end

.resourcesObject



20
21
22
23
24
# File 'lib/contentful/database_importer/json_generator.rb', line 20

def self.resources
  ObjectSpace.each_object(Class).select do |c|
    c.included_modules.include? Resource
  end
end