Skip to content

Commit 25e0a48

Browse files
committed
extracting ForFileOutputBuilder for clarity
1 parent ec95ffb commit 25e0a48

File tree

3 files changed

+85
-48
lines changed

3 files changed

+85
-48
lines changed

lib/code_ownership.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
require 'code_ownership/private/file_path_finder'
1212
require 'code_ownership/private/file_path_team_cache'
1313
require 'code_ownership/private/team_finder'
14+
require 'code_ownership/private/for_file_output_builder'
1415
require 'code_ownership/cli'
1516

1617
begin

lib/code_ownership/cli.rb

Lines changed: 1 addition & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -111,54 +111,7 @@ def self.for_file(argv)
111111
raise "Please pass in one file. Use `#{EXECUTABLE} for_file --help` for more info"
112112
end
113113

114-
if options[:verbose]
115-
for_file_verbose(file: files.first, json: options[:json])
116-
else
117-
for_file_terse(file: files.first, json: options[:json])
118-
end
119-
end
120-
121-
def self.for_file_terse(file:, json:)
122-
team = CodeOwnership.for_file(file)
123-
124-
team_name = team&.name || 'Unowned'
125-
team_yml = team&.config_yml || 'Unowned'
126-
127-
if json
128-
json_output = {
129-
team_name: team_name,
130-
team_yml: team_yml
131-
}
132-
133-
puts json_output.to_json
134-
else
135-
puts <<~MSG
136-
Team: #{team_name}
137-
Team YML: #{team_yml}
138-
MSG
139-
end
140-
end
141-
142-
def self.for_file_verbose(file:, json:)
143-
verbose = CodeOwnership.for_file_verbose(file) || {team_name: 'Unowned', team_config_yml: 'Unowned', reasons: []}
144-
145-
if json
146-
json = {
147-
team_name: verbose[:team_name],
148-
team_yml: verbose[:team_config_yml],
149-
reasons: verbose[:reasons]
150-
}
151-
152-
puts json.to_json
153-
else
154-
messages = ["Team: #{verbose[:team_name]}", "Team YML: #{verbose[:team_config_yml]}"]
155-
if verbose[:reasons].any?
156-
messages << "Reasons:\n- #{verbose[:reasons].join("\n-")}"
157-
end
158-
messages.last << "\n"
159-
160-
puts messages.join("\n")
161-
end
114+
puts CodeOwnership::Private::ForFileOutputBuilder.build(file_path: files.first, json: !!options[:json], verbose: !!options[:verbose])
162115
end
163116

164117
def self.for_team(argv)
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# frozen_string_literal: true
2+
3+
# typed: strict
4+
5+
module CodeOwnership
6+
module Private
7+
class ForFileOutputBuilder
8+
extend T::Sig
9+
private_class_method :new
10+
11+
sig { params(file_path: String, json: T::Boolean, verbose: T::Boolean).void }
12+
def initialize(file_path:, json:, verbose:)
13+
@file_path = file_path
14+
@json = json
15+
@verbose = verbose
16+
end
17+
18+
sig { params(file_path: String, json: T::Boolean, verbose: T::Boolean).returns(String) }
19+
def self.build(file_path:, json:, verbose:)
20+
new(file_path: file_path, json: json, verbose: verbose).build
21+
end
22+
23+
UNOWNED_OUTPUT = T.let(
24+
{
25+
team_name: 'Unowned',
26+
team_yml: 'Unowned'
27+
},
28+
T::Hash[Symbol, T.untyped]
29+
)
30+
31+
sig { returns(String) }
32+
def build
33+
result_hash = @verbose ? build_verbose : build_terse
34+
35+
return result_hash.to_json if @json
36+
37+
build_message_for(result_hash)
38+
end
39+
40+
private
41+
42+
sig { returns(T::Hash[Symbol, T.untyped]) }
43+
def build_verbose
44+
result = CodeOwnership.for_file_verbose(@file_path)
45+
return UNOWNED_OUTPUT if result.nil?
46+
47+
{
48+
team_name: result[:team_name],
49+
team_yml: result[:team_config_yml],
50+
reasons: result[:reasons]
51+
}
52+
end
53+
54+
sig { returns(T::Hash[Symbol, T.untyped]) }
55+
def build_terse
56+
team = CodeOwnership.for_file(@file_path)
57+
58+
if team.nil?
59+
UNOWNED_OUTPUT
60+
else
61+
{
62+
team_name: team.name,
63+
team_yml: team.config_yml
64+
}
65+
end
66+
end
67+
68+
sig { params(result_hash: T::Hash[Symbol, T.untyped]).returns(String) }
69+
def build_message_for(result_hash)
70+
messages = ["Team: #{result_hash[:team_name]}", "Team YML: #{result_hash[:team_yml]}"]
71+
reasons_list = T.let(Array(result_hash[:reasons]), T::Array[String])
72+
messages << build_reasons_message(reasons_list) unless reasons_list.empty?
73+
messages.last << "\n"
74+
messages.join("\n")
75+
end
76+
77+
sig { params(reasons: T::Array[String]).returns(String) }
78+
def build_reasons_message(reasons)
79+
"Reasons:\n- #{reasons.join("\n-")}"
80+
end
81+
end
82+
end
83+
end

0 commit comments

Comments
 (0)