在我学习ruby & rails的旅程中,我继续安装Rubocop。到目前为止,我的代码以ruby方式重构是非常有帮助的,但现在我认为我已经在这个无助的案例中遇到了困难。考虑下面的方法来创建一个新的实体,我正在寻找一种方式来重构它使Rubocop停在我大声喊叫:如何重构方法来降低RuboCop的ABCsize
- 线长
- 指派分支条件的大小(目前26.02/15)
,我能想到的就目前而言,除了禁止那些警察OFC的唯一的事,其实是分手了的模型分成两个较小的(说的基本信息和财务),并相应设置它们,但我会产生这样的印象,即这会将复杂性从创建方法中移出并放到其他地方,因为我需要记住创建这两者相关实体等。任何提示都是值得欢迎的。
def create_store_information(store, meta)
user = @datasource.user
user.store_informations.create!(
name: store['name'],
description: store['description'],
status: 1,
url: store['URL'].downcase,
store_version: store['version'],
api_version: store['wc_version'],
timezone: meta['timezone'],
currency: meta['currency'],
currency_format: meta['currency_format'],
currency_position: meta['currency_position'],
thousand_separator: meta['thousand_separator'],
decimal_separator: meta['decimal_separator'],
price_num_decimals: meta['price_num_decimals'],
tax_included: cast_to_bool(meta['tax_included']),
weight_unit: meta['weight_unit'],
dimension_unit: meta['dimension_unit'],
ssl_enabled: cast_to_bool(meta['ssl_enabled']),
permalinks_enabled: cast_to_bool(meta['permalinks_enabled']),
generate_password: cast_to_bool(meta['generate_password']),
user: user
)
end
编辑: 根据要求,我附加来自不同类创建store_information的第二样品。
def create_store_information(store, meta)
user = @datasource.user
user.store_informations.create!(
name: store['id'],
description: store['name'],
status: 1,
url: store['domain'].downcase,
store_version: '1.0',
api_version: '1.0',
timezone: meta['timezone'],
currency: meta['currency'],
currency_format: meta['money_format'],
currency_position: '', # not applicable
thousand_separator: '', # not applicable, take from user's locale
decimal_separator: '', # not applicable, take from user's locale
price_num_decimals: '', # not applicable, take from user's locale
tax_included: cast_to_bool(meta['taxes_included']),
weight_unit: nil, # not applicable
dimension_unit: nil, # not applicable
ssl_enabled: cast_to_bool(meta['force_ssl']),
permalinks_enabled: true,
generate_password: false,
user: user
)
end
也许会喜欢的东西'store.slice(*%W [名称描述...])合并(meta.slice(*%重量快乐[。时区货币...]))'。然后将数据清理('store ['URL'],downcase'等)推送到'user.store_informations'模型中。 –
也许,但它会提高整体代码的可读性?猜不是:) – Rafal
可能不是,但我倾向于忽略这些愚蠢的工具和他们的教条shenanigans :) –