class DatoDump::Dumper

Attributes

configuration[R]
repo[R]

Public Class Methods

new(repo, configuration) click to toggle source
# File lib/dato_dump/dumper.rb, line 9
def initialize(repo, configuration)
  @repo = repo
  @configuration = configuration
end

Public Instance Methods

content_type_key(content_type) click to toggle source
# 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_types() click to toggle source
# File lib/dato_dump/dumper.rb, line 52
def content_types
  @content_types ||= repo.find_entities_of_type('content_type')
end
dump() click to toggle source
# 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) click to toggle source
# File lib/dato_dump/dumper.rb, line 39
def dump_entity(record)
  RecordDumper.new(record, repo).dump
end
records() click to toggle source
# File lib/dato_dump/dumper.rb, line 56
def records
  @records ||= repo.find_entities_of_type('record')
end