Class: Dependabot::Maven::TokenBucket

Inherits:
T::Struct
  • Object
show all
Extended by:
T::Helpers, T::Sig
Includes:
Comparable
Defined in:
lib/dependabot/maven/token_bucket.rb

Instance Method Summary collapse

Instance Method Details

#<=>(other) ⇒ Object



27
28
29
30
31
32
# File 'lib/dependabot/maven/token_bucket.rb', line 27

def <=>(other)
  cmp = compare_tokens(tokens, other.tokens)
  return cmp unless cmp&.zero?

  compare_additions(addition, other.addition)
end

#compare_additions(first, second) ⇒ Object



87
88
89
90
91
# File 'lib/dependabot/maven/token_bucket.rb', line 87

def compare_additions(first, second)
  return 0 if first.nil? && second.nil?

  (first || empty_addition) <=> (second || empty_addition)
end

#compare_token_pair(first = 0, second = 0) ⇒ Object

rubocop:disable Metrics/PerceivedComplexity,Metrics/CyclomaticComplexity



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/dependabot/maven/token_bucket.rb', line 55

def compare_token_pair(first = 0, second = 0) # rubocop:disable Metrics/PerceivedComplexity,Metrics/CyclomaticComplexity
  first ||= 0
  second ||= 0

  if first.is_a?(Integer) && second.is_a?(String)
    return first <= 0 ? -1 : 1
  end

  if first.is_a?(String) && second.is_a?(Integer)
    return second <= 0 ? 1 : -1
  end

  if first == Dependabot::Maven::VersionParser::SP &&
     second.is_a?(String) && second != Dependabot::Maven::VersionParser::SP
    return -1
  end

  if second == Dependabot::Maven::VersionParser::SP &&
     first.is_a?(String) && first != Dependabot::Maven::VersionParser::SP
    return 1
  end

  if first.is_a?(Integer) && second.is_a?(Integer)
    first <=> second
  elsif first.is_a?(String) && second.is_a?(String)
    first <=> second
  end
end

#compare_tokens(first, second) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/dependabot/maven/token_bucket.rb', line 40

def compare_tokens(first, second)
  max_idx = [first.size, second.size].max - 1
  (0..max_idx).each do |idx|
    cmp = compare_token_pair(first[idx], second[idx])
    return cmp unless T.must(cmp).zero?
  end
  0
end

#empty_additionObject



94
95
96
# File 'lib/dependabot/maven/token_bucket.rb', line 94

def empty_addition
  TokenBucket.new(tokens: [])
end

#to_aObject



20
21
22
23
24
# File 'lib/dependabot/maven/token_bucket.rb', line 20

def to_a
  return tokens if addition.nil?

  tokens.clone.append(addition.to_a)
end