I've got some constants defined like this
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"
The record field can have values like services2
or industrial1
. In my model I've created a mapping method that's supposed to return hash with different set of attributes depending on the record field value like so
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
For instance, if a record contains a value consumer3
, the mapping method should return { abc: abc, vpn: vpn, lbc: lbc }
. As you can see there's a lot of code duplication. I was wondering if there might be more optimal and concise way of tackling this task.