I'm currently writing a script and I decided to run cane over it to look for some issues, and the following method was highlighted. I've done my best to cut it back, and at this point I'm inclined to leave it as-is, however cane is still giving it a pretty high rating of 23 (15 is the point it starts complaining) so I wanted to get some more input. Other than perhaps splitting fetching and generating does anyone have any ideas?
def self.generate(template, output_file, company)
puts 'Fetching data...'
@data = Provider::GenericProvider.create_provider(company['type'].to_sym, company).data
valid_keys = @data.keys.sort {|a,b| -(a <=> b)}[0,10]
@data = @data.select {|k, _| valid_keys.include? k}
valid_keys = @data.keys.sort {|a,b| -(a <=> b)}[0,2]
loop do
break if :done == choose {|menu| timesheet_menu valid_keys, menu}
end
puts 'Generating output...'
# Used as a result of the ERB binding
#noinspection RubyUnusedLocalVariable
data = @data.select {|k, _| valid_keys.include? k}
File.write(output_file, ERB.new(File.read(template), nil, '-').result(binding))
end
EDIT:
I just split it, and ended up with the below function, which still scores 19 due to the various lambdas used to control sorting and filtering the data despite being pretty easy to follow still:
def self.fetch_data(company)
data = Provider::GenericProvider.create_provider(company['type'].to_sym, company).data
valid_keys = data.keys.sort {|a,b| -(a <=> b)}[0,10]
data = data.select {|k, _| valid_keys.include? k}
valid_keys = data.keys.sort {|a,b| -(a <=> b)}[0,2]
loop do
break if :done == choose {|menu| timesheet_menu data, valid_keys, menu}
end
data.select {|k, _| valid_keys.include? k}
end