2018-01-08 15:26:15 +00:00
|
|
|
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
|
2017-12-20 14:45:39 +00:00
|
|
|
|
|
|
|
def attributes
|
|
|
|
# alias or alias_method won't work if attribute method
|
|
|
|
# is overwritten in inheriting sub-class
|
|
|
|
attribute
|
|
|
|
end
|
2018-01-08 15:26:15 +00:00
|
|
|
|
|
|
|
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
|