gpt4 book ai didi

ruby - 如何关闭太高的圈复杂度

转载 作者:行者123 更新时间:2023-12-02 20:31:40 27 4
gpt4 key购买 nike

我在模型中有一个 where 方法抛出 lint 错误。 model中的全部代码目前只是测试代码,后续会进行重构。所以我想暂时关闭这个 lint 错误。

更新:这是我遇到 lint 错误的方法

def self.where(start_date, end_date, customer_id, type, location, is_registered)
filtered_data = if start_date && end_date
customers.select do |e|
e[:startDateTime].to_datetime >= start_date.to_datetime &&
e[:endDateTime].to_datetime <= end_date.to_datetime
end
elsif start_date
customers.select {|e| e[:startDateTime].to_datetime >= start_date.to_datetime }
elsif end_date
customers.select {|e| e[:endDateTime].to_datetime <= end_date.to_datetime }
else
customers
end

if !is_registered.nil? # is_registered is true or false
filtered_data = customers.select { |e| e[:isRegistered].to_s == is_registered }
end
# Check if hash presents and check if the keys have valid values.
if customer_id || type || location
hash = { customerId: customer_id.to_i, type: type, location: location }
# delete if type, location or customer_id is nil.
hash = hash.delete_if { |_k, v| v.nil? || v == 0 }
keys = hash.keys
filtered_data = filtered_data.select { |h| h.select { |k| keys.include?(k) } == hash }
else
filtered_data
end

filtered_data.map do |slot|
mock_customer(slot[:id], slot[:customerId], slot[:name], slot[:startDateTime],
slot[:endDateTime], slot[:location], slot[:status])
end
end

我尝试在模型中添加 # rubocop:disable Metrics/AbcSize 但没有帮助。

最佳答案

试试这个:

# rubocop:disable Metrics/CyclomaticComplexity
.... your method here
# rubocop:enable Metrics/CyclomaticComplexity

此外,如果您想为所有这些测试文件关闭 Rubocop(因为您要重构它们),您可以尝试 this answer .

关于ruby - 如何关闭太高的圈复杂度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48511081/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com