Ruby 圈和感知复杂性



我有一个服务对象,它解析一个 excel 文件并将数据保存在其中。Rubocop将其标记为:

app/services/csv_upload.rb:17:3: C: Metrics/AbcSize: Assignment Branch Condition size for parse_spreadsheet is too high. [<14, 34, 11> 38.38/35]
def parse_spreadsheet ...
^^^^^^^^^^^^^^^^^^^^^
app/services/csv_upload.rb:17:3: C: Metrics/CyclomaticComplexity: Cyclomatic complexity for parse_spreadsheet is too high. [10/6]
def parse_spreadsheet ...
^^^^^^^^^^^^^^^^^^^^^
app/services/csv_upload.rb:17:3: C: Metrics/PerceivedComplexity: Perceived complexity for parse_spreadsheet is too high. [11/7]

我该如何解决这个问题?我应该如何重构这段代码?parse_spreadsheet方法是标记它的位置。

require "spreadsheet"
class CsvUpload
attr_accessor :review
def initialize(file)
Spreadsheet.client_encoding = "UTF-8"
tempfile = File.open(file.tempfile)
@spreadsheet = Spreadsheet.open tempfile
@exception_sheet = @spreadsheet.worksheet 3
@review = []
parse_spreadsheet
end
def parse_spreadsheet
@exception_sheet.each_with_index 4 do |row, index|
employee_no = row[1]
department = row[2]
date = row[3].to_datetime
time_in = row[4].to_datetime unless row[4].nil?
time_out = row[5].to_datetime unless row[5].nil?
if department == "Night"
next_row = @exception_sheet.to_a[index + 5]
next_date = next_row[3].to_datetime
next_time_in = next_row[4].to_datetime unless next_row[4].nil?
start_time = set_date_time(date, time_out) unless time_out.nil?
end_time = set_date_time(next_date, next_time_in) unless next_time_in.nil?
else
start_time = set_date_time(date, time_in) unless time_in.nil?
end_time = set_date_time(date, time_out) unless time_out.nil?
end
@employee = Employee.find_by(employee_no: employee_no)
next if @employee.nil?
@attendance_item = AttendanceItem.create(
start_time: start_time, end_time: end_time, employee_id: @employee.id
)
end
end
def to_review
@review
end
private
def set_date_time(date, time)
DateTime.new(
date.year,
date.month,
date.day,
time.hour,
time.min,
time.sec,
"+0800"
).in_time_zone
end
end

我会首先为您的每个项目提供一个单独的方法@exception_sheet:

def parse_spreadsheet
@exception_sheet.each_with_index 4 do |row, index|
handle_exception_row(row, index)
end
end
def handle_exception_row(row, index)
employee_no = row[1]
department = row[2]
date = row[3].to_datetime
time_in = row[4].to_datetime unless row[4].nil?
time_out = row[5].to_datetime unless row[5].nil?
if department == "Night"
next_row = @exception_sheet.to_a[index + 5]
next_date = next_row[3].to_datetime
next_time_in = next_row[4].to_datetime unless next_row[4].nil?
start_time = set_date_time(date, time_out) unless time_out.nil?
end_time = set_date_time(next_date, next_time_in) unless next_time_in.nil?
else
start_time = set_date_time(date, time_in) unless time_in.nil?
end_time = set_date_time(date, time_out) unless time_out.nil?
end
@employee = Employee.find_by(employee_no: employee_no)
next if @employee.nil?
@attendance_item = AttendanceItem.create(
start_time: start_time, end_time: end_time, employee_id: @employee.id
)
end
end

这修复了parse_spreadsheet方法,但会handle_exception_row产生新的错误。然后剥离它:

def handle_exception_row(row, index)
employee_no = row[1]
department = row[2]
date = row[3].to_datetime
time_in = row[4].to_datetime unless row[4].nil?
time_out = row[5].to_datetime unless row[5].nil?
handle_attendance_creation(employee_no: employee_no, department: department, date: date, time_in: time_in, time_out: time_out)
end
def handle_attendance_creation(employee_no:, department:, date:, time_in:, time_out:)
if department == "Night"
next_row = @exception_sheet.to_a[index + 5]
next_date = next_row[3].to_datetime
next_time_in = next_row[4].to_datetime unless next_row[4].nil?
start_time = set_date_time(date, time_out) unless time_out.nil?
end_time = set_date_time(next_date, next_time_in) unless next_time_in.nil?
else
start_time = set_date_time(date, time_in) unless time_in.nil?
end_time = set_date_time(date, time_out) unless time_out.nil?
end
@employee = Employee.find_by(employee_no: employee_no)
next if @employee.nil?
@attendance_item = AttendanceItem.create(
start_time: start_time, end_time: end_time, employee_id: @employee.id
)
end
end

您可能仍然会在handle_attendance_creation上收到错误,但希望您能看到这是怎么回事。去除更多采用所需参数的较小方法,您将到达那里

最新更新