base_factory.rb 1016 B

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