Refactoring: Splitted ExternalSync and Lookup namespaces.

This commit is contained in:
Thorsten Eckel 2018-01-08 16:26:15 +01:00
parent b1f1dc2e12
commit eaf097c262
7 changed files with 119 additions and 98 deletions

View file

@ -10,7 +10,7 @@ class Sequencer
'Import::Exchange::FolderContact::Mapping',
'Import::Common::Model::Skip::Blank::Mapped',
'Import::Exchange::FolderContact::StaticAttributes',
'Import::Common::Model::ExternalSync::Lookup',
'Import::Common::Model::Lookup::ExternalSync',
'Import::Common::Model::Associations::Extract',
'Import::Common::User::Attributes::Downcase',
'Import::Common::User::Email::CheckValidity',
@ -19,7 +19,7 @@ class Sequencer
'Import::Common::Model::Create',
'Import::Common::Model::Associations::Assign',
'Import::Common::Model::Save',
'Import::Common::Model::ExternalSync::Create',
'Import::Common::Model::ExternalSync::Integrity',
'Import::Exchange::FolderContact::HttpLog',
'Import::Exchange::FolderContact::Statistics::Diff',
'Import::Common::ImportJob::Statistics::Update',

View file

@ -1,29 +0,0 @@
class Sequencer
class Unit
module Import
module Common
module Model
module ExternalSync
class Create < Sequencer::Unit::Base
prepend ::Sequencer::Unit::Import::Common::Model::Mixin::SkipOnSkippedInstance
uses :instance, :instance_action, :remote_id, :dry_run, :external_sync_source, :model_class
def process
return if dry_run
return if instance_action != :created
::ExternalSync.create(
source: external_sync_source,
source_id: remote_id,
object: model_class.name,
o_id: instance.id
)
end
end
end
end
end
end
end
end

View file

@ -0,0 +1,51 @@
class Sequencer
class Unit
module Import
module Common
module Model
module ExternalSync
class Integrity < Sequencer::Unit::Base
uses :instance, :remote_id, :dry_run, :external_sync_source, :model_class
def process
return if dry_run
return if instance.blank?
return if instance.id.blank?
return if up_to_date?
create
end
private
def up_to_date?
return false if entry.blank?
return true if entry.source_id == remote_id
entry.update!(source_id: remote_id)
true
end
def entry
@entry ||= begin
::ExternalSync.find_by(
source: external_sync_source,
object: model_class.name,
o_id: instance.id
)
end
end
def create
::ExternalSync.create(
source: external_sync_source,
source_id: remote_id,
object: model_class.name,
o_id: instance.id
)
end
end
end
end
end
end
end
end

View file

@ -1,63 +0,0 @@
require 'sequencer/unit/import/common/model/mixin/handle_failure'
class Sequencer
class Unit
module Import
module Common
module Model
module ExternalSync
class Local < Sequencer::Unit::Base
include ::Sequencer::Unit::Import::Common::Model::Mixin::HandleFailure
prepend ::Sequencer::Unit::Import::Common::Model::Mixin::SkipOnSkippedInstance
uses :mapped, :remote_id, :model_class, :external_sync_source, :instance_action
provides :instance
def process
return if state.provided?(:instance)
return if value.blank?
return if instance.blank?
create_external_sync
state.provide(:instance, instance)
end
private
def attribute
raise "Missing implementation of '#{__method__}' method for '#{self.class.name}'"
end
def value
mapped[attribute]
end
def instance
@instance ||= begin
model_class.where(attribute => value).find do |local|
!ExternalSync.exists?(
source: external_sync_source,
object: model_class.name,
o_id: local.id
)
end
end
end
def create_external_sync
ExternalSync.create(
source: external_sync_source,
source_id: remote_id,
object: import_class.name,
o_id: instance.id
)
end
end
end
end
end
end
end
end

View file

@ -0,0 +1,60 @@
class Sequencer
class Unit
module Import
module Common
module Model
module Lookup
class Attributes < Sequencer::Unit::Base
include ::Sequencer::Unit::Import::Common::Model::Mixin::HandleFailure
prepend ::Sequencer::Unit::Import::Common::Model::Mixin::Skip::InstanceAction
skip_instance_action :skipped
uses :mapped, :model_class
provides :instance
def process
return if state.provided?(:instance)
return if existing_instance.blank?
state.provide(:instance, existing_instance)
end
private
def attribute
raise "Missing implementation of '#{__method__}' method for '#{self.class.name}'"
end
alias attributes attribute
def existing_instance
@existing_instance ||= begin
Array(attributes).find do |attribute|
value = mapped[attribute]
next if value.blank?
existing_instance = lookup(
attribute: attribute,
value: value
)
next if existing_instance.blank?
# https://stackoverflow.com/a/24901650/7900866
break existing_instance
end
end
end
def lookup(attribute:, value:)
return model_class.identify(value) if model_class.respond_to?(:identify)
model_class.find_by(attribute => value)
end
end
end
end
end
end
end
end

View file

@ -3,10 +3,12 @@ class Sequencer
module Import
module Common
module Model
module ExternalSync
class Lookup < Sequencer::Unit::Base
module Lookup
class ExternalSync < Sequencer::Unit::Base
include ::Sequencer::Unit::Import::Common::Model::Mixin::HandleFailure
prepend ::Sequencer::Unit::Import::Common::Model::Mixin::SkipOnSkippedInstance
prepend ::Sequencer::Unit::Import::Common::Model::Mixin::Skip::InstanceAction
skip_instance_action :skipped
uses :remote_id, :model_class, :external_sync_source
provides :instance

View file

@ -1,6 +1,6 @@
require 'rails_helper'
RSpec.describe Sequencer::Unit::Import::Common::Model::ExternalSync::Lookup, sequencer: :unit do
RSpec.describe Sequencer::Unit::Import::Common::Model::Lookup::ExternalSync, sequencer: :unit do
it 'finds model_class instances by remote_id' do
user = create(:user)