Skip to content

Commit

Permalink
adds new method
Browse files Browse the repository at this point in the history
  • Loading branch information
marachimeno committed Oct 25, 2024
1 parent 720c758 commit adb9c94
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 6 deletions.
13 changes: 13 additions & 0 deletions lib/public/versions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,19 @@ def find!(name)
find(name) || raise("Version '#{name}' not found")
end

sig { params(name: String).returns(T.nilable(Version)) }
def find_next(name)
version = find!(name)
index = T.must(sorted_versions.index(version))

sorted_versions[index + 1]
end

sig { returns(T::Array[Version]) }
def sorted_versions
versions.sort
end

private

sig { returns(T::Array[Version]) }
Expand Down
5 changes: 5 additions & 0 deletions lib/public/versions/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ def to_s
def matches?(str)
[name, *aliases].include?(str)
end

sig { params(other: Version).returns(T.nilable(Integer)) }
def <=>(other)
name <=> other.name
end
end
end
end
48 changes: 42 additions & 6 deletions spec/public/versions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@

RSpec.describe ResourceRegistry::Versions do
subject do
described_class.new(
versions: [
ResourceRegistry::Versions::Version.new('2024-01-01'),
ResourceRegistry::Versions::Version.new('2024-04-01', aliases: 'stable')
]
)
described_class.new(versions: versions)
end

let(:versions) do
[
ResourceRegistry::Versions::Version.new('2024-01-01'),
ResourceRegistry::Versions::Version.new('2024-04-01', aliases: 'stable')
]
end

describe '#find!' do
Expand Down Expand Up @@ -42,4 +44,38 @@
expect(subject.find!('stable').to_s).to eq('2024-04-01')
end
end

describe '#find_next' do
context 'when given an old version' do
it 'returns the following version' do
expect(subject.find_next('2024-01-01').to_s).to eq('2024-04-01')
end
end

context 'when given the last version' do
it 'does not returns a version' do
expect(subject.find_next('2024-04-01')).to be_nil
end
end

context 'when given a random value' do
it 'does not returns a version' do
expect(subject.find_next('2024-04-01')).to be_nil
end
end

context 'when given a list of unsorted versions' do
let(:versions) do
[
ResourceRegistry::Versions::Version.new('2024-03-01', aliases: 'stable'),
ResourceRegistry::Versions::Version.new('2024-04-01', aliases: 'deprecated'),
ResourceRegistry::Versions::Version.new('2024-02-01')
]
end

it 'orders and returns the following version' do
expect(subject.find_next('2024-02-01').to_s).to eq('2024-03-01')
end
end
end
end

0 comments on commit adb9c94

Please sign in to comment.