lib/pact_broker/pacts/selector.rb in pact_broker-2.76.2 vs lib/pact_broker/pacts/selector.rb in pact_broker-2.77.0

- old
+ new

@@ -1,20 +1,32 @@ +require 'pact_broker/hash_refinements' + module PactBroker module Pacts class Selector < Hash + using PactBroker::HashRefinements + def initialize(options = {}) merge!(options) end def resolve(consumer_version) + ResolvedSelector.new(self.to_h.without(:fallback_tag, :fallback_branch), consumer_version) + end + + def resolve_for_fallback(consumer_version) ResolvedSelector.new(self.to_h, consumer_version) end def tag= tag self[:tag] = tag end + def branch= branch + self[:branch] = branch + end + def latest= latest self[:latest] = latest end def latest @@ -23,14 +35,22 @@ def fallback_tag= fallback_tag self[:fallback_tag] = fallback_tag end + def fallback_branch= fallback_branch + self[:fallback_branch] = fallback_branch + end + def fallback_tag self[:fallback_tag] end + def fallback_branch + self[:fallback_branch] + end + def consumer= consumer self[:consumer] = consumer end def consumer @@ -43,14 +63,22 @@ def self.latest_for_tag(tag) Selector.new(latest: true, tag: tag) end + def self.latest_for_branch(branch) + Selector.new(latest: true, branch: branch) + end + def self.latest_for_tag_with_fallback(tag, fallback_tag) Selector.new(latest: true, tag: tag, fallback_tag: fallback_tag) end + def self.latest_for_branch_with_fallback(branch, fallback_branch) + Selector.new(latest: true, branch: branch, fallback_branch: fallback_branch) + end + def self.all_for_tag(tag) Selector.new(tag: tag) end def self.all_for_tag_and_consumer(tag, consumer) @@ -59,10 +87,14 @@ def self.latest_for_tag_and_consumer(tag, consumer) Selector.new(latest: true, tag: tag, consumer: consumer) end + def self.latest_for_branch_and_consumer(branch, consumer) + Selector.new(latest: true, branch: branch, consumer: consumer) + end + def self.latest_for_consumer(consumer) Selector.new(latest: true, consumer: consumer) end def self.from_hash hash @@ -71,16 +103,24 @@ def fallback_tag? !!fallback_tag end + def fallback_branch? + !!fallback_branch + end + def tag self[:tag] end + def branch + self[:branch] + end + def overall_latest? - !!(latest? && !tag) + !!(latest? && !tag && !branch) end # Not sure if the fallback_tag logic is needed def latest_for_tag? potential_tag = nil if potential_tag @@ -88,10 +128,19 @@ else !!(latest && !!tag) end end + # Not sure if the fallback_tag logic is needed + def latest_for_branch? potential_branch = nil + if potential_branch + !!(latest && branch == potential_branch) + else + !!(latest && !!branch) + end + end + def all_for_tag_and_consumer? !!(tag && !latest? && consumer) end def all_for_tag? @@ -106,9 +155,15 @@ if overall_latest? || other.overall_latest? if overall_latest? == other.overall_latest? 0 else overall_latest? ? -1 : 1 + end + elsif latest_for_branch? || other.latest_for_branch? + if latest_for_branch? == other.latest_for_branch? + branch <=> other.branch + else + latest_for_branch? ? -1 : 1 end elsif latest_for_tag? || other.latest_for_tag? if latest_for_tag? == other.latest_for_tag? tag <=> other.tag else