如何重构方法来降低RuboCop的ABCsize

时间:2016-09-11 21:08:28

标签: ruby-on-rails ruby rubocop

在我学习红宝石的过程中我继续安装Rubocop。到目前为止,以红宝石的方式重构我的代码是一个很大的帮助,但现在我觉得我已经遇到了这个无助的案例。鉴于以下创建新实体的方法,我正在寻找一种方法来重构它以使Rubocop不再向我大吼大叫:

  1. 行长
  2. 作业分部条件大小(目前为26.02 / 15)
  3. 我唯一可以想到的,除了禁用那些警察,实际上是将模型分成两个较小的(比如基本信息和财务)并相应地设置它们,但我得到了印象这会将复杂性从创建方法中移除并将其放在其他地方,因为我需要记住创建两个相关实体等。任何提示都非常受欢迎。

    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
    

1 个答案:

答案 0 :(得分:1)

这只是许多可能性中的一个建议。

您可以使用Ruby的元编程功能来动态发送方法。 meta对象的字段很容易分配user.store_informations,因为字段匹配1表示1。 store对象也可以,但它不会那么简单。

您可以将字段移动到类定义中的数组:

CAST_TO_BOOL = %w(
  tax_included
  ssl_enabled
  permalinks_enabled
  generate_password
).freeze

META_FIELDS = %w(
  timezone
  currency
  currency_format
  currency_position
  thousand_separator
  decimal_separator
  price_num_decimals
  tax_included
  weight_unit
  dimension_unit
  ssl_enabled
  permalinks_enabled
  generate_password
).freeze

然后你可以定义一个私有方法来动态设置meta

user.store_informations字段
private

def set_meta_fields_to_store_information(user)
  META_FIELDS.each do |field|
    if CAST_TO_BOOL.include? field
      user.store_informations.__send__ "#{f}=" { cast_to_bool( meta[field] ) }
      next
    end
    user.store_informations.__send__ "#{f}=" { meta[field] }
  end
end

然后你可以改为调用那个方法:

def create_store_information(store, meta)
  user = @datasource.user
  user.store_informations.new(
    name: store['name'],
    description: store['description'],
    status: 1,
    url: store['URL'].downcase,
    store_version: store['version'],
    api_version: store['wc_version'],
    user: user
  )
  set_meta_fields_to_store_information(user)
  user.save!
end

编辑#2

关于使用不同类的对象填充字段;

一种方法是定义一种方法,根据商店的类别为您分配字段。 但话说回来,如果你有数千个不同的商店,这可能不是最佳的。

class StoreA; end
class StoreB; end
class StoreC; end

然后:

# you could also use dynamic method dispatching here instead:

def set_store_information_to_user(store, user)
  case store
  when StoreA
    assign_store_a_method(store, user)
  when StoreB
    assign_store_b_method(store, user)
  when StoreC
    assign_store_c_method(store, user)
  end
end

private
def assign_store_a_method(store, user); end
def assign_store_b_method(store, user); end
def assign_store_c_method(store, user); end