如何优化包含相似键和值的映射散列?

How to optimize mapping hash that contains similar keys and values?

我有一些这样定义的常量

CONSUMER_TYPE = 'consumer'
CONSUMER_1_TYPE = "#{CONSUMER_TYPE}1"
CONSUMER_2_TYPE = "#{CONSUMER_TYPE}2"
CONSUMER_3_TYPE = "#{CONSUMER_TYPE}3"

INDUSTRIAL_TYPE = 'industrial'
INDUSTRIAL_1_TYPE = "#{INDUSTRIAL_TYPE}1"
INDUSTRIAL_2_TYPE = "#{INDUSTRIAL_TYPE}2"
INDUSTRIAL_3_TYPE = "#{INDUSTRIAL_TYPE}3"

SERVICES_TYPE = 'services'
SERVICES_1_TYPE = "#{SERVICES_TYPE}1"
SERVICES_2_TYPE = "#{SERVICES_TYPE}2"
SERVICES_3_TYPE = "#{SERVICES_TYPE}3"

记录字段可以有类似 services2industrial1 的值。在我的模型中,我创建了一个映射方法,该方法应该 return 根据记录字段值对不同的属性集进行散列,例如

def classification_attributes
  product_type_mapping[product_type]
end

def product_type_mapping
  {
     CONSUMER_1_TYPE => { abc: abc, vpn: vpn, lbc: lbc },
     CONSUMER_2_TYPE => { abc: abc, vpn: vpn, lbc: lbc },
     CONSUMER_3_TYPE => { abc: abc, vpn: vpn, lbc: lbc },
     INDUSTRIAL_1_TYPE => { vpn: vpn, htt: htt, bnn: bnn },
     INDUSTRIAL_2_TYPE => { vpn: vpn, htt: htt, bnn: bnn },
     INDUSTRIAL_3_TYPE => { vpn: vpn, htt: htt, bnn: bnn },
     SERVICES_1_TYPE => { dhy: dhy, rtt: rtt, abc: abc },
     SERVICES_2_TYPE => { dhy: dhy, rtt: rtt, abc: abc },
     SERVICES_3_TYPE => { dhy: dhy, rtt: rtt, abc: abc }
  }
end

例如,如果记录包含值consumer3,则映射方法应该return { abc: abc, vpn: vpn, lbc: lbc }。如您所见,有很多代码重复。我想知道是否有更优化和简洁的方法来处理这个任务。

  1. 使用符号代替常量。
  2. 不要公开映射。

Ruby中的常量主要是关于信息隐藏的。例如,如果密钥从 consumer1 更改为 consumer_1,只要所有内容都访问具有 CONSUMER_1_TYPE 的哈希,您就可以了。为什么要冒险?

相反,完全隐藏哈希。既然它是隐藏的,就不需要常量了。使用符号。

如果所有值都相同,请将它们放入自己的方法中。

def classification_attributes(product_type)
  product_type_mapping[product_type]
end

private def consumer_config
  { abc: abc, vpn: vpn, lbc: lbc }
end

private def industrial_config
  { vpn: vpn, htt: htt, bnn: bnn }
end

private def services_config
  { dhy: dhy, rtt: rtt, abc: abc }
end

private def product_type_mapping
  {
     conumser1: consumer_config,
     consumer2: consumer_config,
     consumer3: consumer_config,
     industrial1: industrial_config,
     industrial2: industrial_config,
     industrial3: industrial_config,
     services1: services_config,
     services2: services_config,
     services3: services_config
  }
end

在没有更多上下文的情况下,我只能这么说。如果有那么多冗余,您可以将 product_type 拆分为类型和子类型。


考虑将 product_type_mapping 移动到 config/application.rb,以及任何其他相关配置。这将应用程序配置保存在一个地方,而不是散落在各种 类.

module YourApp
  class Application < Rails::Application
    config.x.consumer_config = { abc: abc, vpn: vpn, lbc: lbc }.freeze
    config.x.industrial_config = { vpn: vpn, htt: htt, bnn: bnn }.freeze
    config.x.services_config = { dhy: dhy, rtt: rtt, abc: abc }.freeze

    config.x.product_type_mapping = {
      conumser1: config.x.consumer_config,
      consumer2: config.x.consumer_config,
      consumer3: config.x.consumer_config,
      industrial1: config.x.industrial_config,
      industrial2: config.x.industrial_config,
      industrial3: config.x.industrial_config,
      services1: config.x.services_config,
      services2: config.x.services_config,
      services3: config.x.services_config
    }.freeze
  end
end

# in your class...

def classification_attributes(product_type)
  Rails.configuration.x.product_type_mapping[product_type]
end