I needed transient attributes in ROM::Factory for my Hanami app. Since all I could find was this old post (Support transient attributes in rom-factory), I took a stab at implementing transient attributes myself.
I have an implementation that works for my use case, but since I am not too familiar with the architecture of ROM::Factory besides the parts that I needed to understand to get things to work, I was hoping to get some feedback on the changes, and eventually submit them as a PR if that feature is still wanted: https://github.com/rom-rb/rom-factory/compare/main…rickenharp:rom-factory:transient_attributes?expand=1
1 Like
Thanks for taking this on! I tried to do this myself a couple years back and sort of ran aground on the complexity of TupleEvaluator.
Based on my understanding of how this works, I think your approach looks promising.
What happens when a transient attribute conflicts with a real one?
Sorry for only getting back to you now.
Currently, adding a new attribute overwrites the previously defined one ( rom-factory/lib/rom/factory/attribute_registry.rb at 63b20c754990f783ec8b451e0cf9026aa75884a8 · rom-rb/rom-factory · GitHub )
Looking at how factory_bot does it, they raise an Exception when you try to define an attribute that has already been defined, regardless of if it’s transient or not. I think adding a similar guard to AttributeRegistry might be beneficial, but it would be a breaking change and some extensive refactoring.
I think for the sake of backwards compatibility and the least amount of change necessary to implement this feature, we should keep this behaviour for transient atrributes as well, but document this behaviour clearly in the documentation. Currently, (ROM) does not mention this, and that seems to be the only documentation besides the YARD generated one.