Any particular reason you were thinking of solving this by having to_json
accept a block? It seems to me like having a method that returns an
intermediate representation (like as_json in ActiveSupport) is a more
flexible approach.
For instance, let's say your model has a summary representation, a standard
representation, and a full representation that each depend on the prior.
It's cleaner for them to be able to call each other instead of having to
forward blocks. Ditto for inheritance, if your model mixes in or inherits a
base serializer. There are also plenty of scenarios where you're not
interested in the string at all - maybe you want to pretty print or
serialize to bjson or put it in queue or DB or something. The to_json block
approach still couples the intermediate representation to the string
representation, so it's going to be clumsy if you're primarily interested
in the former.
Thoughts? Would you be open to a PR along these lines?
Thanks for this awesome library.
Daniel
On Tuesday, May 30, 2017 at 6:43:10 PM UTC-7, Jeremy Evans wrote:
>
> On Tuesday, May 30, 2017 at 6:01:03 PM UTC-7, dota? =op wrote:
>>
>> > There currently isn't a good way to do it using the json_serializer
>> plugin,
>> > unless you want to add model instance methods for it. I'm considering
>> > having to_json take a block that yields the hash, and then serializing
>> the
>> > block's return value to JSON, but haven't implemented that yet.
>>
>> wat do you think about having a `to_json_hash()` or something liek
>> that? this way we would be able to just get teh hash without
>> cerealizing to jason at all.
>>
>
> If you want that method, it's easy to implement using the API I suggested:
>
> def to_json_hash(*a)
> to_json(*a){|h| return h}
> end
>
> Thanks,
> Jeremy
>
--
You received this message because you are subscribed to the Google Groups
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.