如何在过高的地方关闭Cyclomatic复杂性

时间:2018-01-29 22:29:12

标签: ruby rubocop

我在模型中有一个where方法,它会抛出lint错误。模型中的整个代码此时只是一个测试代码,稍后将重构。所以我现在要关掉这个lint错误。

更新: 这是我在

得到lint错误的方法
try {
   $sql = $pdo->prepare($req);
   $myArr[] = $start
   $myArr[] = $limit

   $sql->execute($myArr);
}

我尝试在模型中添加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 ,但没有帮助。

1 个答案:

答案 0 :(得分:2)

试试这个:

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

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