Class: DatoDump::Dumper

Inherits:
Object
  • Object
show all
Defined in:
lib/dato_dump/dumper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repo, configuration) ⇒ Dumper

Returns a new instance of Dumper.



9
10
11
12
# File 'lib/dato_dump/dumper.rb', line 9

def initialize(repo, configuration)
  @repo = repo
  @configuration = configuration
end

Instance Attribute Details

#configurationObject (readonly)

Returns the value of attribute configuration.



7
8
9
# File 'lib/dato_dump/dumper.rb', line 7

def configuration
  @configuration
end

#repoObject (readonly)

Returns the value of attribute repo.



7
8
9
# File 'lib/dato_dump/dumper.rb', line 7

def repo
  @repo
end

Instance Method Details

#content_type_key(content_type) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/dato_dump/dumper.rb', line 43

def content_type_key(content_type)
  api_key = content_type.api_key
  if content_type.singleton
    [api_key, true]
  else
    [api_key.pluralize, false]
  end
end

#content_typesObject



52
53
54
# File 'lib/dato_dump/dumper.rb', line 52

def content_types
  @content_types ||= repo.find_entities_of_type('content_type')
end

#dumpObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/dato_dump/dumper.rb', line 14

def dump
  collections_by_type = {}

  content_types.each do |content_type|
    key, singleton = content_type_key(content_type)
    collections_by_type[key] = singleton ? nil : []
  end

  records.each do |record|
    key, singleton = content_type_key(record.content_type)
    if singleton
      collections_by_type[key] = dump_entity(record)
    else
      collections_by_type[key].push dump_entity(record)
    end
  end

  collections_by_type.each do |key, value|
    path = File.join(configuration.destination_path, key + ".json")
    File.open(path, 'w') do |file|
      file.write JSON.pretty_generate(value)
    end
  end
end

#dump_entity(record) ⇒ Object



39
40
41
# File 'lib/dato_dump/dumper.rb', line 39

def dump_entity(record)
  RecordDumper.new(record, repo).dump
end

#recordsObject



56
57
58
# File 'lib/dato_dump/dumper.rb', line 56

def records
  @records ||= repo.find_entities_of_type('record')
end