-
Notifications
You must be signed in to change notification settings - Fork 108
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Representing objects into objects using Representable::Object#to_object #267
Comments
Hey @elShiaLabeouf, thanks for your kind words! It means a lot to us knowing our tools are helpful to people! What I am wondering is, what is the matter with the original implementation? What exactly wasn't working for you? We might have missed something, I don't remember ever anyone using the object transformation, so I'm happy you took over! |
Thanks for a quick response! To explain with a few more details: What I'm trying to achieveLet's say I have a Rails model class AnimalRepresenter < Representable::Decorator
include Representable::Hash
property :name, getter: ->(represented:, **) { represented.name.upcase }
property :age,
property :is_favorite, getter: ->(options:, **) { options[:current_user].favorite_animals_ids.include?(id) }
end Normally and habitually I'd represent an AR record like this: What I triedI tried to change require 'representable/object'
class AnimalRepresenter < Representable::Decorator
include Representable::Object
property :name, getter: ->(represented:, **) { represented.name.upcase }
property :age,
property :is_favorite, getter: ->(options:, **) { options[:current_user].favorite_animals_ids.include?(id) }
end I'm getting an error in the moment of registering the class:
I dug the Internet and the Zulip chat to find an example, but unsuccessfully. I decided to fork the repo and make an attempt to implement the feature. Thankfully, the structure of the gem is flexible and easy to extend. So I added a few changes inspired by the hash implementation and added some tests. Then I noticed the code in require 'representable/object'
module AnimalRepresenter
include Representable::Object
property :name, getter: ->(represented:, **) { represented.name.upcase }
property :age,
property :is_favorite, getter: ->(options:, **) { options[:current_user].favorite_animals_ids.include?(id) }
end and with usage like this: What I achievedWith the changes from my PR, the line from my initial goal now works: A brief example of the output from my console:
|
@elShiaLabeouf IMO, the In your case where you want to access keys of an output hash as a method, could you pass it to OpenStruct for simple method access or Mash if you want nested method access ? |
@yogeshjain999 Hm, I kind of had the same in mind what @elShiaLabeouf is asking for. If I remember correctly, the object strategy was meant for nested objects. The |
Hi @yogeshjain999 ! That's, for sure, one of workarounds to my needs. But if I did so, I presume, the usage of memory would be inefficient since I'd create a hash first and only then convert it to OpenStruct or Hashie resulting in 2x time and resources needed. Meanwhile, with my approach we directly create a Struct with no middle-man. In fact, Struct is a structure with high performance - 3 to 50 times faster than OpenStruct (benchmark1, benchmark2). So I believe the feature is worth the hustle :) |
@apotonick thank you for having an understanding of the subject! I'd be happy to see it in I have changed the test according to you suggestion. Is there anything else needed to have it merged? |
I just commented on the PR. @elShiaLabeouf Just do it |
Nothing is impossible haha |
Hello @apotonick and the Trailblazer team!
First of all, I'd like to thank you all for the great framework you created and that you've been supporting it for all these years. It's truly great and I appreciate the work.
I met Trailblazer on an API project in 2019 and worked for 2 years with it back then.
Currently I'm working on a legacy SSR Rails 5 application with fat models and controllers and I really want to incorporate Trailblazer there to structure and organize the code.
During my first steps of trying it out on a SSR app, I came to think that it could've come in handy if Representable could make objects as output, i.e.
So that properties could be accessed via method calls.
Why would it be handy? For Rails view helpers, which often take an object and method names it should respond to. Plus partials would look less polluted (on a scale of hundreds variable calls in a single page) comparing to hash key-value usage:
I saw some developments in this direction in the Representable source code, but I couldn't make it work like in the example above. And there's no info about this mysterious
to_object
in the Trailblazer docs.I decided to create a PR that adds such a feature. The usage is similar to using
to_json
orto_hash
format options. I used RubyStruct
for represented object containers.I would also love to add a notion about this new option to the Trailblazer documentation as well if the PR gets to
master
.Looking forward to hearing your thoughts about it!
The text was updated successfully, but these errors were encountered: