composed_of
is a feature that is not widely used and its API is somewhat confusing, especially for beginners. It was even deprecated for a while, but 10 years after its deprecation, it’s still here.
The Rails documentation includes these examples including a mapping:
composed_of :temperature, mapping: %w(reading celsius)
composed_of :balance, class_name: "Money", mapping: %w(balance amount)
composed_of :address, mapping: [ %w(address_street street), %w(address_city city) ]
Something I noticed that could make the API more beginner-friendly would be allowing a hash for its mapping
option. So it could look like:
composed_of :temperature, mapping: { reading: :celsius }
composed_of :balance, class_name: "Money", mapping: { balance: :amount }
composed_of :address, mapping: { address_street: :street, address_city: :city }
I can see why it wasn’t considered at the time it was introduced: before Ruby 1.9, looping through a hash didn’t have deterministic order, and the mapping order is important. It hasn’t been an issue for a while, so we could accept a hash, too.
I’ll start working on it right away, but I’d like to have some feedback on whether it is a good idea to work on it.
Thank you!