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.
Constants in Ruby are mostly about information hiding. For example, if the key changes from
consumer1
toconsumer_1
as long as everything accesses the Hash withCONSUMER_1_TYPE
you're ok. Why risk it?Instead, fully hide the Hash. Now that it's hidden, constants are not necessary. Use Symbols.
If all the values are going to be the same, put them into their own methods.
That's about as far as I can say without more context. If there's that much redundancy you may be able to split
product_type
into type and subtype.Consider moving
product_type_mapping
into config/application.rb, plus any other related configurations. This keeps the application configuration in one place, not scattered around in various classes.