base_factory.rb 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. module Import
  2. module BaseFactory
  3. # rubocop:disable Style/ModuleFunction
  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, &import_block)
  29. records.each(&import_block)
  30. end
  31. def module_name
  32. name.to_s.sub(/Import::/, '').sub(/Factory/, '')
  33. end
  34. end
  35. end