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)
|
|
|
|
pre_import_hook(records)
|
|
|
|
import_loop(records) do |record|
|
|
|
|
next if skip?(record)
|
|
|
|
backend_instance = create_instance(record, *args)
|
|
|
|
post_import_hook(record, backend_instance)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-11-25 16:10:37 +00:00
|
|
|
def import(_records)
|
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
|
|
|
|
|
|
|
|
def pre_import_hook(_records)
|
|
|
|
end
|
|
|
|
|
2016-12-19 08:59:54 +00:00
|
|
|
def post_import_hook(_record, _backend_instance)
|
|
|
|
end
|
|
|
|
|
2016-11-25 16:10:37 +00:00
|
|
|
def backend_class(_record)
|
|
|
|
"Import::#{module_name}".constantize
|
|
|
|
end
|
|
|
|
|
|
|
|
def skip?(_record)
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2016-12-19 08:59:54 +00:00
|
|
|
def create_instance(record, *args)
|
|
|
|
backend_class(record).new(record, *args)
|
|
|
|
end
|
|
|
|
|
|
|
|
def import_loop(records, &import_block)
|
|
|
|
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
|