r/ruby • u/6stringfanatic • Aug 25 '26
The Primitive Urge to Be of Value: composed_of in Rails
Wrote a piece about spotting a missing value object and refactoring towards one, adding sorting and comparison.
https://pushfromkush.com/publications/primitive-urge-to-be-of-value-composed_of-in-rails
Feedback welcome.
37
Upvotes
26
u/the_maddogx Aug 25 '26
TIL
composed_of5
u/TommyTheTiger Aug 25 '26
Dang, on the one hand very cool, especially that you can share these composable classes between e.g. different classes that both carry currency, OTOH yet one more layer of indirection between what is actually in the DB and the application logic. For experienced devs seems like a nice way to make the code base more readable.
6
u/KimJongIlLover Aug 25 '26
having this
and then going to this
and then calling that "better" has to be a joke right?
2
u/Richard-Degenne Aug 26 '26
You could do away with most of the boilerplate by declaring your `Measurement` as a custom ActiveRecord type, no?
-18
u/Mediocre-Brain9051 Aug 25 '26
This already exists in rails:
https://api.rubyonrails.org/classes/ActiveRecord/Aggregations/ClassMethods.html
You rediscovering the wheel.