thoughtbot / props_template

A very fast json builder for Rails
MIT License
133 stars 4 forks source link

Additional APIs #30

Open kortirso opened 2 months ago

kortirso commented 2 months ago

While we were integrating props into our application, we added some additional APIs that were spied on by the Jbuilder.

1) json.optional! - defines attribute or structure and marks it as optional, during serialization this method checks condition and renders or does not render such attribute

example of usage

json.optional! :properties do
  json.optional! :value, -> { object.value } # lambda here is to avoid value calculations before checking conditions for rendering
end

2) json.extract! - extracts the mentioned attributes or hash elements from the passed object and turns them into attributes of the JSON

example of usage - json.extract! object, :id, :unit

instead of

json.id object.id
json.unit object.unit

3) json.partial! - renders partial without setting result to some key

@jho406 do you have any plans to integrate such features to PropsTemplate? Or maybe I can share their implementation and then dicsuss/modify/accept/reject them, what do you think?

jho406 commented 2 months ago

maybe I can share their implementation and then dicsuss/modify/accept/reject them, what do you think?

Yes. I'd like to discuss that!

Would you be open to creating 3 separate PRs / issues? At the very least, I can give architectural context/guidance for each of them.

jho406 commented 1 month ago

Nice pr in #31 @kortirso! I'll take a look later this week. I feel like that one might take a bit more time for me to review. If you are open to PRing for json.extract that seems like a great low hanging fruit and much easier for me to review. Followed by json.partial!

Also as you're migrating from jbuilder to props_template, one thing i recommend is to avoid autocamelizing your keys as it may slow your views down.

kortirso commented 1 month ago

@jho406 what is better approach for formatting keys?

jho406 commented 1 month ago

The latter.

direct json.firstValue (if we need camelCased format)

Do it directly. Its a low effort approach, speedy, and if you plan to use it in JS land, easier to search for.