Class: PactBroker::Pacts::Selector

Inherits:
Hash
  • Object
show all
Defined in:
lib/pact_broker/pacts/selector.rb

Overview

rubocop: disable Metrics/ClassLength

Direct Known Subclasses

ResolvedSelector, Versions::Selector

Constant Summary collapse

PROPERTY_NAMES =
[:latest, :tag, :branch, :consumer, :consumer_version, :environment_name, :fallback_tag, :fallback_branch, :main_branch, :matching_branch, :currently_supported, :currently_deployed]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(properties = {}) ⇒ Selector

Returns a new instance of Selector.



11
12
13
14
# File 'lib/pact_broker/pacts/selector.rb', line 11

def initialize(properties = {})
  properties.without(*PROPERTY_NAMES).tap { |it| warn("WARN: Unsupported property for #{self.class.name}: #{it.keys.join(", ")} at #{caller[0..3]}") if it.any? }
  merge!(properties)
end

Class Method Details

.all_for_tag(tag) ⇒ Object



166
167
168
# File 'lib/pact_broker/pacts/selector.rb', line 166

def self.all_for_tag(tag)
  new(tag: tag)
end

.all_for_tag_and_consumer(tag, consumer) ⇒ Object



170
171
172
# File 'lib/pact_broker/pacts/selector.rb', line 170

def self.all_for_tag_and_consumer(tag, consumer)
  new(tag: tag, consumer: consumer)
end

.for_currently_deployed(environment_name = nil) ⇒ Object



186
187
188
# File 'lib/pact_broker/pacts/selector.rb', line 186

def self.for_currently_deployed(environment_name = nil)
  new( { currently_deployed: true, environment_name: environment_name }.compact )
end

.for_currently_deployed_and_consumer(consumer) ⇒ Object



194
195
196
# File 'lib/pact_broker/pacts/selector.rb', line 194

def self.for_currently_deployed_and_consumer(consumer)
  new(currently_deployed: true, consumer: consumer)
end

.for_currently_deployed_and_environment_and_consumer(environment_name, consumer) ⇒ Object



198
199
200
# File 'lib/pact_broker/pacts/selector.rb', line 198

def self.for_currently_deployed_and_environment_and_consumer(environment_name, consumer)
  new(currently_deployed: true, environment_name: environment_name, consumer: consumer)
end

.for_currently_supported(environment_name = nil) ⇒ Object



190
191
192
# File 'lib/pact_broker/pacts/selector.rb', line 190

def self.for_currently_supported(environment_name = nil)
  new( { currently_supported: true, environment_name: environment_name }.compact )
end

.for_currently_supported_and_environment_and_consumer(environment_name, consumer) ⇒ Object



202
203
204
# File 'lib/pact_broker/pacts/selector.rb', line 202

def self.for_currently_supported_and_environment_and_consumer(environment_name, consumer)
  new(currently_supported: true, environment_name: environment_name, consumer: consumer)
end

.for_environment(environment_name) ⇒ Object



206
207
208
# File 'lib/pact_broker/pacts/selector.rb', line 206

def self.for_environment(environment_name)
  new(environment_name: environment_name)
end

.for_environment_and_consumer(environment_name, consumer) ⇒ Object



210
211
212
# File 'lib/pact_broker/pacts/selector.rb', line 210

def self.for_environment_and_consumer(environment_name, consumer)
  new(environment_name: environment_name, consumer: consumer)
end

.for_main_branchObject



146
147
148
# File 'lib/pact_broker/pacts/selector.rb', line 146

def self.for_main_branch
  new(main_branch: true)
end

.from_hash(hash) ⇒ Object



214
215
216
# File 'lib/pact_broker/pacts/selector.rb', line 214

def self.from_hash hash
  new(hash)
end

.latest_for_branch(branch) ⇒ Object



154
155
156
# File 'lib/pact_broker/pacts/selector.rb', line 154

def self.latest_for_branch(branch)
  new(latest: true, branch: branch)
end

.latest_for_branch_and_consumer(branch, consumer) ⇒ Object



178
179
180
# File 'lib/pact_broker/pacts/selector.rb', line 178

def self.latest_for_branch_and_consumer(branch, consumer)
  new(latest: true, branch: branch, consumer: consumer)
end

.latest_for_branch_with_fallback(branch, fallback_branch) ⇒ Object



162
163
164
# File 'lib/pact_broker/pacts/selector.rb', line 162

def self.latest_for_branch_with_fallback(branch, fallback_branch)
  new(latest: true, branch: branch, fallback_branch: fallback_branch)
end

.latest_for_consumer(consumer) ⇒ Object



182
183
184
# File 'lib/pact_broker/pacts/selector.rb', line 182

def self.latest_for_consumer(consumer)
  new(latest: true, consumer: consumer)
end

.latest_for_tag(tag) ⇒ Object



150
151
152
# File 'lib/pact_broker/pacts/selector.rb', line 150

def self.latest_for_tag(tag)
  new(latest: true, tag: tag)
end

.latest_for_tag_and_consumer(tag, consumer) ⇒ Object



174
175
176
# File 'lib/pact_broker/pacts/selector.rb', line 174

def self.latest_for_tag_and_consumer(tag, consumer)
  new(latest: true, tag: tag, consumer: consumer)
end

.latest_for_tag_with_fallback(tag, fallback_tag) ⇒ Object



158
159
160
# File 'lib/pact_broker/pacts/selector.rb', line 158

def self.latest_for_tag_with_fallback(tag, fallback_tag)
  new(latest: true, tag: tag, fallback_tag: fallback_tag)
end

.overall_latestObject



142
143
144
# File 'lib/pact_broker/pacts/selector.rb', line 142

def self.overall_latest
  new(latest: true)
end

Instance Method Details

#<=>(other) ⇒ Object

rubocop: disable Metrics/CyclomaticComplexity



293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# File 'lib/pact_broker/pacts/selector.rb', line 293

def <=> other
  if overall_latest? || other.overall_latest?
    overall_latest_comparison(other)
  elsif latest_for_branch? || other.latest_for_branch?
    branch_comparison(other)
  elsif latest_for_tag? || other.latest_for_tag?
    latest_for_tag_comparison(other)
  elsif tag || other.tag
    tag_comparison(other)
  elsif currently_deployed? || other.currently_deployed?
    currently_deployed_comparison(other)
  elsif currently_supported? || other.currently_supported?
    currently_supported_comparison(other)
  elsif consumer || other.consumer
    consumer_comparison(other)
  else
    0
  end
end

#==(other) ⇒ Object



288
289
290
# File 'lib/pact_broker/pacts/selector.rb', line 288

def == other
  other.class == self.class && super
end

#all_for_tag?Boolean

Returns:

  • (Boolean)


284
285
286
# File 'lib/pact_broker/pacts/selector.rb', line 284

def all_for_tag?
  !!(tag && !latest?)
end

#all_for_tag_and_consumer?Boolean

Returns:

  • (Boolean)


280
281
282
# File 'lib/pact_broker/pacts/selector.rb', line 280

def all_for_tag_and_consumer?
  !!(tag && !latest? && consumer)
end

#branchObject



242
243
244
# File 'lib/pact_broker/pacts/selector.rb', line 242

def branch
  self[:branch]
end

#branch=(branch) ⇒ Object



70
71
72
# File 'lib/pact_broker/pacts/selector.rb', line 70

def branch= branch
  self[:branch] = branch
end

#consumerObject



102
103
104
# File 'lib/pact_broker/pacts/selector.rb', line 102

def consumer
  self[:consumer]
end

#consumer=(consumer) ⇒ Object



98
99
100
# File 'lib/pact_broker/pacts/selector.rb', line 98

def consumer= consumer
  self[:consumer] = consumer
end

#currently_deployedObject



110
111
112
# File 'lib/pact_broker/pacts/selector.rb', line 110

def currently_deployed
  self[:currently_deployed]
end

#currently_deployed=(currently_deployed) ⇒ Object



106
107
108
# File 'lib/pact_broker/pacts/selector.rb', line 106

def currently_deployed= currently_deployed
  self[:currently_deployed] = currently_deployed
end

#currently_deployed?Boolean

Returns:

  • (Boolean)


114
115
116
# File 'lib/pact_broker/pacts/selector.rb', line 114

def currently_deployed?
  !!currently_deployed
end

#currently_supportedObject



122
123
124
# File 'lib/pact_broker/pacts/selector.rb', line 122

def currently_supported
  self[:currently_supported]
end

#currently_supported=(currently_supported) ⇒ Object



118
119
120
# File 'lib/pact_broker/pacts/selector.rb', line 118

def currently_supported= currently_supported
  self[:currently_supported] = currently_supported
end

#currently_supported?Boolean

Returns:

  • (Boolean)


126
127
128
# File 'lib/pact_broker/pacts/selector.rb', line 126

def currently_supported?
  !!currently_supported
end

#environment_nameObject



134
135
136
# File 'lib/pact_broker/pacts/selector.rb', line 134

def environment_name
  self[:environment_name]
end

#environment_name=(environment_name) ⇒ Object



130
131
132
# File 'lib/pact_broker/pacts/selector.rb', line 130

def environment_name= environment_name
  self[:environment_name] = environment_name
end

#fallback_branchObject



94
95
96
# File 'lib/pact_broker/pacts/selector.rb', line 94

def fallback_branch
  self[:fallback_branch]
end

#fallback_branch=(fallback_branch) ⇒ Object



86
87
88
# File 'lib/pact_broker/pacts/selector.rb', line 86

def fallback_branch= fallback_branch
  self[:fallback_branch] = fallback_branch
end

#fallback_branch?Boolean

Returns:

  • (Boolean)


230
231
232
# File 'lib/pact_broker/pacts/selector.rb', line 230

def fallback_branch?
  !!fallback_branch
end

#fallback_tagObject



90
91
92
# File 'lib/pact_broker/pacts/selector.rb', line 90

def fallback_tag
  self[:fallback_tag]
end

#fallback_tag=(fallback_tag) ⇒ Object



82
83
84
# File 'lib/pact_broker/pacts/selector.rb', line 82

def fallback_tag= fallback_tag
  self[:fallback_tag] = fallback_tag
end

#fallback_tag?Boolean

Returns:

  • (Boolean)


226
227
228
# File 'lib/pact_broker/pacts/selector.rb', line 226

def fallback_tag?
  !!fallback_tag
end

#for_consumer(consumer) ⇒ Object



218
219
220
# File 'lib/pact_broker/pacts/selector.rb', line 218

def for_consumer(consumer)
  self.class.new(to_h.merge(consumer: consumer))
end

#in_environment?Boolean

Returns:

  • (Boolean)


138
139
140
# File 'lib/pact_broker/pacts/selector.rb', line 138

def in_environment?
  !!environment_name
end

#latestObject



78
79
80
# File 'lib/pact_broker/pacts/selector.rb', line 78

def latest
  self[:latest]
end

#latest=(latest) ⇒ Object



74
75
76
# File 'lib/pact_broker/pacts/selector.rb', line 74

def latest= latest
  self[:latest] = latest
end

#latest_for_branch?(potential_branch = nil) ⇒ Boolean

Not sure if the fallback_tag logic is needed

Returns:

  • (Boolean)


268
269
270
271
272
273
274
# File 'lib/pact_broker/pacts/selector.rb', line 268

def latest_for_branch? potential_branch = nil
  if potential_branch
    latest == true && branch == potential_branch
  else
    latest == true && branch.is_a?(String)
  end
end

#latest_for_each_branch?Boolean

Returns:

  • (Boolean)


276
277
278
# File 'lib/pact_broker/pacts/selector.rb', line 276

def latest_for_each_branch?
  latest == true && branch == true
end

#latest_for_main_branch?Boolean

Returns:

  • (Boolean)


222
223
224
# File 'lib/pact_broker/pacts/selector.rb', line 222

def latest_for_main_branch?
  !!main_branch
end

#latest_for_tag?(potential_tag = nil) ⇒ Boolean

Not sure if the fallback_tag logic is needed

Returns:

  • (Boolean)


259
260
261
262
263
264
265
# File 'lib/pact_broker/pacts/selector.rb', line 259

def latest_for_tag? potential_tag = nil
  if potential_tag
    !!(latest && tag == potential_tag)
  else
    !!(latest && !!tag)
  end
end

#main_branchObject



234
235
236
# File 'lib/pact_broker/pacts/selector.rb', line 234

def main_branch
  self[:main_branch]
end

#main_branch=(main_branch) ⇒ Object

rubocop: enable Metrics/CyclomaticComplexity



58
59
60
# File 'lib/pact_broker/pacts/selector.rb', line 58

def main_branch= main_branch
  self[:main_branch] = main_branch
end

#matching_branchObject



246
247
248
# File 'lib/pact_broker/pacts/selector.rb', line 246

def matching_branch
  self[:matching_branch]
end

#matching_branch=(matching_branch) ⇒ Object



62
63
64
# File 'lib/pact_broker/pacts/selector.rb', line 62

def matching_branch= matching_branch
  self[:matching_branch] = matching_branch
end

#matching_branch?Boolean

Returns:

  • (Boolean)


250
251
252
# File 'lib/pact_broker/pacts/selector.rb', line 250

def matching_branch?
  !!matching_branch
end

#overall_latest?Boolean

Returns:

  • (Boolean)


254
255
256
# File 'lib/pact_broker/pacts/selector.rb', line 254

def overall_latest?
  !!(latest? && !tag && !branch && !main_branch && !currently_deployed && !currently_supported && !environment_name)
end

#resolve(consumer_version) ⇒ Object



16
17
18
# File 'lib/pact_broker/pacts/selector.rb', line 16

def resolve(consumer_version)
  ResolvedSelector.new(self.to_h.without(:fallback_tag, :fallback_branch), consumer_version)
end

#resolve_for_environment(consumer_version, environment, target = nil) ⇒ Object



24
25
26
# File 'lib/pact_broker/pacts/selector.rb', line 24

def resolve_for_environment(consumer_version, environment, target = nil)
  ResolvedSelector.new(self.to_h.merge({ environment: environment, target: target }.compact), consumer_version)
end

#resolve_for_fallback(consumer_version) ⇒ Object



20
21
22
# File 'lib/pact_broker/pacts/selector.rb', line 20

def resolve_for_fallback(consumer_version)
  ResolvedSelector.new(self.to_h, consumer_version)
end

#tagObject



238
239
240
# File 'lib/pact_broker/pacts/selector.rb', line 238

def tag
  self[:tag]
end

#tag=(tag) ⇒ Object



66
67
68
# File 'lib/pact_broker/pacts/selector.rb', line 66

def tag= tag
  self[:tag] = tag
end

#typeObject

Only currently used to identify the currently_deployed from the others in verifiable_pact_messages, so don’t need the “for_consumer” sub category rubocop: disable Metrics/CyclomaticComplexity



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/pact_broker/pacts/selector.rb', line 31

def type
  if latest_for_main_branch?
    :latest_for_main_branch
  elsif latest_for_each_branch?
    :latest_for_each_branch
  elsif latest_for_branch?
    :latest_for_branch
  elsif matching_branch?
    :matching_branch
  elsif currently_deployed?
    :currently_deployed
  elsif currently_supported?
    :currently_supported
  elsif in_environment?
    :in_environment
  elsif latest_for_tag?
    :latest_for_tag
  elsif all_for_tag?
    :all_for_tag
  elsif overall_latest?
    :overall_latest
  else
    :undefined
  end
end