Created
June 5, 2023 11:53
-
-
Save Shigawire/4a74d937f1563ec0d2a015bef6b6f6f9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
module Contacts | |
module Services | |
module SlsEvents | |
class Importer | |
module Consumers | |
# | |
# Contacts::Services::SlsEvents::Importer::Consumers::VersionResolver | |
# | |
# Abstracts the cases for resource version management | |
# Returns one of: :stale, :update, :confirm, :conflict, :sync | |
class VersionResolver | |
attr_reader :local_our_version, :local_their_version, :incoming_their_version, :incoming_our_version | |
def initialize(local_our_version:, local_their_version:, incoming_their_version:, incoming_our_version:) | |
@local_our_version = local_our_version | |
@local_their_version = local_their_version | |
@incoming_their_version = incoming_their_version | |
@incoming_our_version = incoming_our_version | |
end | |
def call | |
return :conflict if conflict? | |
return :confirmation if confirmation? | |
return :stale if stale? | |
return :update if update? | |
return :sync if sync? | |
raise "Unexpected versioning case happened: #{[local_our_version, local_their_version, incoming_their_version, incoming_our_version].join(',')}" | |
end | |
private | |
def confirmation? | |
[ | |
incoming_their_version, | |
incoming_our_version, | |
local_our_version, | |
].uniq.length == 1 && local_their_version < incoming_their_version | |
end | |
def update? | |
incoming_their_version > incoming_our_version# && incoming_our_version <= local_our_version | |
end | |
def conflict? | |
update? && incoming_our_version != local_our_version | |
end | |
def stale? | |
incoming_our_version < local_our_version && !update? | |
end | |
def sync? | |
all_fields.uniq.length == 1 | |
end | |
def all_fields | |
[ | |
incoming_their_version, | |
incoming_our_version, | |
local_our_version, | |
local_their_version, | |
] | |
end | |
end | |
end | |
end | |
end | |
end | |
end | |
__END__ | |
vr = Contacts::Services::SlsQueue::Importer::Consumers::VersionResolver.new( | |
local_our_version: 4, | |
local_their_version: 4, | |
incoming_their_version: 5, | |
incoming_our_version: 4, | |
) | |
vr.call |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment