2016-11-25 16:10:37 +00:00
|
|
|
module Import
|
|
|
|
module BaseFactory
|
|
|
|
|
|
|
|
# rubocop:disable Style/ModuleFunction
|
|
|
|
extend self
|
|
|
|
|
2016-12-19 08:59:54 +00:00
|
|
|
def import_action(records, *args)
|
2016-12-20 15:16:09 +00:00
|
|
|
pre_import_hook(records, *args)
|
|
|
|
import_loop(records, *args) do |record|
|
|
|
|
next if skip?(record, *args)
|
2016-12-19 08:59:54 +00:00
|
|
|
backend_instance = create_instance(record, *args)
|
2016-12-20 15:16:09 +00:00
|
|
|
post_import_hook(record, backend_instance, *args)
|
2016-12-19 08:59:54 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-12-20 15:16:09 +00:00
|
|
|
def import(_records, *_args)
|
2016-12-19 08:59:54 +00:00
|
|
|
raise 'Missing import method implementation for this factory'
|
2016-11-25 16:10:37 +00:00
|
|
|
end
|
|
|
|
|
2017-11-23 08:09:44 +00:00
|
|
|
def pre_import_hook(_records, *args); end
|
2016-11-25 16:10:37 +00:00
|
|
|
|
2017-11-23 08:09:44 +00:00
|
|
|
def post_import_hook(_record, _backend_instance, *args); end
|
2016-12-19 08:59:54 +00:00
|
|
|
|
2016-12-20 15:16:09 +00:00
|
|
|
def backend_class(_record, *_args)
|
2016-11-25 16:10:37 +00:00
|
|
|
"Import::#{module_name}".constantize
|
|
|
|
end
|
|
|
|
|
2016-12-20 15:16:09 +00:00
|
|
|
def skip?(_record, *_args)
|
2016-11-25 16:10:37 +00:00
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2016-12-19 08:59:54 +00:00
|
|
|
def create_instance(record, *args)
|
2016-12-20 15:16:09 +00:00
|
|
|
backend_class(record, *args).new(record, *args)
|
2016-12-19 08:59:54 +00:00
|
|
|
end
|
|
|
|
|
2016-12-20 15:16:09 +00:00
|
|
|
def import_loop(records, *_args, &import_block)
|
2016-12-19 08:59:54 +00:00
|
|
|
records.each(&import_block)
|
|
|
|
end
|
|
|
|
|
2016-11-25 16:10:37 +00:00
|
|
|
def module_name
|
|
|
|
name.to_s.sub(/Import::/, '').sub(/Factory/, '')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|