base_factory.rb 1009 B

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