class DatoDump::RecordDumper

Attributes

record[R]
repo[R]

Public Class Methods

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

Public Instance Methods

dump() click to toggle source
# File lib/dato_dump/record_dumper.rb, line 14
def dump
  attributes = { id: record.id }
  content_type = record.content_type

  content_type.fields.each do |field|
    attributes[field.api_key] = dump_attribute(
      record.send(field.api_key),
      field
    )
  end

  if content_type.sortable
    attributes["position"] = record.position
  end

  attributes.to_h
end

Private Instance Methods

dump_attribute(value, field) click to toggle source
# File lib/dato_dump/record_dumper.rb, line 34
def dump_attribute(value, field)
  field_type = field.field_type
  type_klass_name = "::DatoDump::FieldTypeDumper::#{field_type.camelize}"
  type_klass = type_klass_name.safe_constantize

  if type_klass
    if field.localized
      (value || {}).reduce({}) do |acc, (locale, locale_value)|
        locale_value && type_klass.dump(locale_value, repo)
      end
    else
      value && type_klass.dump(value, repo)
    end
  else
    raise "Cannot convert field `#{value}` of type `#{field_type}`"
  end
end