Class: Pageflow::EntryExportImport::FileMappings

Inherits:
Object
  • Object
show all
Defined in:
lib/pageflow/entry_export_import/file_mappings.rb

Instance Method Summary collapse

Constructor Details

#initializeFileMappings

Returns a new instance of FileMappings.



4
5
6
# File 'lib/pageflow/entry_export_import/file_mappings.rb', line 4

def initialize
  @file_mappings = {}
end

Instance Method Details

#exported_id_for(model_name, imported_id) ⇒ Object



27
28
29
# File 'lib/pageflow/entry_export_import/file_mappings.rb', line 27

def exported_id_for(model_name, imported_id)
  @file_mappings.fetch(model_name).invert[imported_id]
end

#find_or_store(exported_file_id, file_type) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/pageflow/entry_export_import/file_mappings.rb', line 8

def find_or_store(exported_file_id, file_type)
  imported_file_id = @file_mappings.dig(file_type.model.name, exported_file_id)

  if imported_file_id.present?
    imported_file_id
  else
    file = yield

    @file_mappings[file_type.model.name] ||= {}
    @file_mappings[file_type.model.name][exported_file_id] = file.id

    file.id
  end
end

#imported_id_for(model_name, exported_id) ⇒ Object



23
24
25
# File 'lib/pageflow/entry_export_import/file_mappings.rb', line 23

def imported_id_for(model_name, exported_id)
  @file_mappings.fetch(model_name)[exported_id]
end