我一直在尝试调整这种设置复杂分配的方法,并且我正在寻找其他选项以使此功能通过警察。
有人会有想法为我指明正确的方向吗?
现在,我正在修补两个内部 .map 调用。
失败的警察
Assignment Branch Condition size for parse_items is too high. [24.08/15]
def parse_items
Avoid multi-line chains of blocks.
end.compact.map do |opt|
问题代码
def parse_items
options = parse_relationships
options = options.select { |opt| opt['type'] == 'product_options' }
options.map do |opt|
parse_included.detect { |x| x['id'] == opt['id'] }
end.compact.map do |opt|
{
group_id: @payload['id'],
originator_id: opt['id'],
price: opt['attributes']['price'],
description: opt['attributes']['name'],
exp_quantity: opt['attributes']['quantity'].to_i,
title: parse_attributes['name'],
image_originator_url: 'image_for_product',
updated_at: timestamp
}
end
end
辅助方法
private
def parse_data
@payload['data']
rescue
[]
end
def parse_included
@payload['included']
rescue
[]
end
def parse_attributes
@payload['data']['attributes']
rescue
[]
end
def parse_relationships
@payload['data']['relationships']['options']['data']
rescue
[]
end
def timestamp
Time.parse(parse_attributes['updated_at'])
end
更新错误
在规范中:错误数量的参数(给定 2,预期 1)Failure/Error: SELECT = ->(opt) { opt['type'] == 'product_options' }
parse_items 的分配分支条件大小太高。[17/15]
更新代码
SELECT = ->(opt) { opt['type'] == 'product_options' }
MAP = ->(opt) { parse_included.detect { |x| x['id'] == opt['id'] } }
def parse_items
parse_relationships.select(&SELECT).map(&MAP).compact.map do |opt|
{
group_id: @payload['id'],
originator_id: opt['id'],
price: opt['attributes']['price'],
description: opt['attributes']['name'],
exp_quantity: opt['attributes']['quantity'].to_i,
title: parse_attributes['name'],
image_originator_url: 'image_for_product',
updated_at: timestamp
}
end
end