Skip to content

Commit ef0abe4

Browse files
authored
Create non-nilable CodeTeams.find! (#22)
1 parent 372d337 commit ef0abe4

File tree

2 files changed

+36
-9
lines changed

2 files changed

+36
-9
lines changed

lib/code_teams.rb

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,27 @@ module CodeTeams
1313
extend T::Sig
1414

1515
class IncorrectPublicApiUsageError < StandardError; end
16+
class TeamNotFoundError < StandardError; end
1617

1718
UNKNOWN_TEAM_STRING = 'Unknown Team'
1819
@plugins_registered = T.let(false, T::Boolean)
1920

2021
sig { returns(T::Array[Team]) }
2122
def self.all
22-
@all = T.let(@all, T.nilable(T::Array[Team]))
23-
@all ||= for_directory('config/teams')
23+
@all ||= T.let(for_directory('config/teams'), T.nilable(T::Array[Team]))
2424
end
2525

2626
sig { params(name: String).returns(T.nilable(Team)) }
2727
def self.find(name)
28-
@index_by_name = T.let(@index_by_name, T.nilable(T::Hash[String, CodeTeams::Team]))
29-
@index_by_name ||= begin
30-
result = {}
31-
all.each { |t| result[t.name] = t }
32-
result
33-
end
34-
28+
@index_by_name ||= T.let(all.to_h { |t| [t.name, t] }, T.nilable(T::Hash[String, CodeTeams::Team]))
3529
@index_by_name[name]
3630
end
3731

32+
sig { params(name: String).returns(Team) }
33+
def self.find!(name)
34+
find(name) || raise(TeamNotFoundError, "No team found with name: #{name}")
35+
end
36+
3837
sig { params(dir: String).returns(T::Array[Team]) }
3938
def self.for_directory(dir)
4039
unless @plugins_registered

spec/lib/code_teams_spec.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,34 @@
3737
end
3838
end
3939

40+
describe '.find' do
41+
it 'returns the team when found' do
42+
team = described_class.find('My Team')
43+
expect(team).to be_a(CodeTeams::Team)
44+
expect(team.name).to eq('My Team')
45+
end
46+
47+
it 'returns nil when the team is not found' do
48+
team = described_class.find('Nonexistent Team')
49+
expect(team).to be_nil
50+
end
51+
end
52+
53+
describe '.find!' do
54+
it 'returns the team when found' do
55+
team = described_class.find!('My Team')
56+
expect(team).to be_a(CodeTeams::Team)
57+
expect(team.name).to eq('My Team')
58+
end
59+
60+
it 'raises TeamNotFoundError when the team is not found' do
61+
expect { described_class.find!('Nonexistent Team') }.to raise_error(
62+
CodeTeams::TeamNotFoundError,
63+
'No team found with name: Nonexistent Team'
64+
)
65+
end
66+
end
67+
4068
describe 'validation_errors' do
4169
subject(:validation_errors) { described_class.validation_errors(described_class.all) }
4270

0 commit comments

Comments
 (0)