base_factory.rb 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. module Import
  3. module BaseFactory
  4. extend self
  5. def import_action(records, *args)
  6. pre_import_hook(records, *args)
  7. import_loop(records, *args) do |record|
  8. next if skip?(record, *args)
  9. backend_instance = create_instance(record, *args)
  10. post_import_hook(record, backend_instance, *args)
  11. end
  12. end
  13. def import(_records, *_args)
  14. raise 'Missing import method implementation for this factory'
  15. end
  16. def pre_import_hook(_records, *args); end
  17. def post_import_hook(_record, _backend_instance, *args); end
  18. def backend_class(_record, *_args)
  19. "Import::#{module_name}".constantize
  20. end
  21. def skip?(_record, *_args)
  22. false
  23. end
  24. private
  25. def create_instance(record, *args)
  26. backend_class(record, *args).new(record, *args)
  27. end
  28. def import_loop(records, *_args, &)
  29. records.each(&)
  30. end
  31. def module_name
  32. name.to_s.sub(%r{Import::}, '').sub(%r{Factory}, '')
  33. end
  34. end
  35. end