With :reader, parsing is completely up to you. Representable will only invoke the function and do nothing else.
I have the following representer that sets a different property on represented after processing it. I do not want to have status_code being set automatically by representable, which is why I'm using a reader. Actually, I could have lived with a setter: but that again is not called when passing { status: nil } into the representer.
class MyRepresenter < Representable::Decorator
property :status,
getter: ->(*) { status&.code },
reader: ->(doc:, represented:, **) {
status = doc['status']
# (omitted custom processing of status)
represented.status = status
}
end
The
reader:
option is documented as follows:I have the following representer that sets a different property on represented after processing it. I do not want to have
status_code
being set automatically by representable, which is why I'm using areader
. Actually, I could have lived with asetter:
but that again is not called when passing{ status: nil }
into the representer.I would suspect the following code:
to output
But actually the call outputs
How should one avoid the
status
property being written and bypassing any setter / reader ?