Skip to content

Commit

Permalink
refactor: clean up provider_or_provider_version methods in QueryBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Dec 31, 2019
1 parent df3e624 commit f9c2434
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 28 deletions.
43 changes: 24 additions & 19 deletions lib/pact_broker/matrix/query_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,29 @@ module PactBroker
module Matrix
class QueryBuilder
def self.provider_or_provider_version_matches(selectors, qualifier = nil)
Sequel.|(*provider_or_provider_version_criteria(selectors, qualifier, false))
Sequel.|(*provider_or_provider_version_criteria(selectors, qualifier))
end

def self.provider_or_provider_version_matches_or_pact_unverified(selectors, qualifier = nil)
Sequel.|(*provider_or_provider_version_criteria(selectors, qualifier, true))
end

def self.provider_or_provider_version_criteria(selectors, qualifier = nil, allow_null_provider_version = false)
most_specific_criteria = selectors.collect(&:most_specific_provider_criterion)
provider_version_ids = collect_ids(most_specific_criteria, :pacticipant_version_id)
provider_ids = collect_ids(most_specific_criteria, :pacticipant_id)
ors = provider_or_provider_version_criteria(selectors, qualifier)
all_provider_ids = selectors.collect{ |s| s[:pacticipant_id] }

ors = []
ors << { qualify(qualifier, :provider_version_id) => provider_version_ids } if provider_version_ids.any?
ors << { qualify(qualifier, :provider_id) => provider_ids } if provider_ids.any?
ors << {
qualify(:lp, :provider_id) => all_provider_ids,
qualify(qualifier, :provider_version_id) => nil
} if allow_null_provider_version
}
Sequel.|(*ors)
end

def self.provider_or_provider_version_criteria(selectors, qualifier = nil)
most_specific_criteria = selectors.collect(&:most_specific_criterion)
# the pacticipant version ids for selectors where pacticipant version id was the most specific criterion
pacticipant_version_ids = collect_ids(most_specific_criteria, :pacticipant_version_id)
# the pacticipant ids for the selectors where the pacticipant id was most specific criterion
pacticipant_ids = collect_ids(most_specific_criteria, :pacticipant_id)

ors = []
ors << { qualify(qualifier, :provider_version_id) => pacticipant_version_ids } if pacticipant_version_ids.any?
ors << { qualify(qualifier, :provider_id) => pacticipant_ids } if pacticipant_ids.any?
ors
end

Expand All @@ -30,13 +33,15 @@ def self.consumer_in_pacticipant_ids(selectors)
end

def self.consumer_or_consumer_version_matches(selectors, qualifier)
most_specific_criteria = selectors.collect(&:most_specific_consumer_criterion)
consumer_version_ids = collect_ids(most_specific_criteria, :pacticipant_version_id)
consumer_ids = collect_ids(most_specific_criteria, :pacticipant_id)
most_specific_criteria = selectors.collect(&:most_specific_criterion)
# the pacticipant version ids for selectors where pacticipant version id was the most specific criterion
pacticipant_version_ids = collect_ids(most_specific_criteria, :pacticipant_version_id)
# the pacticipant ids for the selectors where the pacticipant id was most specific criterion
pacticipant_ids = collect_ids(most_specific_criteria, :pacticipant_id)

ors = []
ors << { qualify(qualifier, :consumer_version_id) => consumer_version_ids } if consumer_version_ids.any?
ors << { qualify(qualifier, :consumer_id) => consumer_ids } if consumer_ids.any?
ors << { qualify(qualifier, :consumer_version_id) => pacticipant_version_ids } if pacticipant_version_ids.any?
ors << { qualify(qualifier, :consumer_id) => pacticipant_ids } if pacticipant_ids.any?

Sequel.|(*ors)
end
Expand Down Expand Up @@ -72,7 +77,7 @@ def self.collect_ids(hashes, key)
end

def self.collect_the_ids selectors
most_specific_criteria = selectors.collect(&:most_specific_consumer_criterion)
most_specific_criteria = selectors.collect(&:most_specific_criterion)
pacticipant_version_ids = collect_ids(most_specific_criteria, :pacticipant_version_id)
pacticipant_ids = collect_ids(most_specific_criteria, :pacticipant_id)
all_pacticipant_ids = selectors.collect(&:pacticipant_id)
Expand Down
10 changes: 1 addition & 9 deletions lib/pact_broker/matrix/resolved_selector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,7 @@ def tag
self[:tag]
end

def most_specific_provider_criterion
if pacticipant_version_id
{ pacticipant_version_id: pacticipant_version_id }
else
{ pacticipant_id: pacticipant_id }
end
end

def most_specific_consumer_criterion
def most_specific_criterion
if pacticipant_version_id
{ pacticipant_version_id: pacticipant_version_id }
else
Expand Down

0 comments on commit f9c2434

Please sign in to comment.