
extlib LazyArray to_json proxy
Reported by Troy K | June 18th, 2008 @ 02:57 PM
I'm not sure if this is something you want to support, but I'm mentioning it because json encoding model/resource collections is important in my small corner of the world, and it should at least be noted in the documentation that Resource.all() returns a DataMapper::Collection < LazyArray and although it smells like an Array, you need to add your own proxy for something like to_json
LazyArray will not proxy the method "to_json" to the underlying array, because the JSON library auto-adds "to_json" to every object, so method_missing is never called. The fix is simple:
class LazyArray
def to_json(*a)
@array.to_json(*a)
end
end
We do this when we initialize our framework, which is where I'm tempted to say it belongs -- but just wanted you guys to be aware as I'm sure it will raise a few wtf's if not documented, especially in Rails apps migrating to dm where render :json just works with AR collections.
Comments and changes to this ticket
-
Dan Kubb (dkubb) June 18th, 2008 @ 04:23 PM
- Milestone cleared.
I think it's sort of a slippery slope trying to override cases like this, there will always be modules like json that shove stuff into the namespace that we have to override.
However, I would accept a patch to LazyArray that makes it act more like a "Blank Slate" proxy, and uses undef_method to remove all but a few essential methods, and delegates everything else down to the underlying Array. See how we've done this in the OneToMany::Proxy class for example.
-
Troy K June 19th, 2008 @ 01:39 AM
Ok, I'll take a look at OneToMany::Proxy and submit a patch for your review.
-
Dan Kubb (dkubb) November 28th, 2008 @ 02:39 AM
- Assigned user changed from Sam Smoot to Dan Kubb (dkubb)
- State changed from new to open
Troy, is this still an issue?
-
Dan Kubb (dkubb) November 30th, 2008 @ 06:50 PM
- Assigned user changed from Dan Kubb (dkubb) to Xavier Shay
Xavier, I've emailed Troy about this too, but I wanted to follow up with you to see if you think this is still applicable.
-
Xavier Shay November 30th, 2008 @ 06:59 PM
- Tag changed from suggestion to dm-serializer, extlib, lazyarray
- State changed from open to resolved
Collections implement #to_json, so it should never get down to LazyArray to proxy the call.
There was a related problem with the collection proxies - we had to undef #to_json so it was passed through to the collection correctly.
This could still be a potential issue if LazyArray is used for anything that isn't a DM::Collection? If that is the case, I recommend using the same approach as used for the other proxies - undef the method in the lib that depends on the method being proxied (dm-serializer).
-
Dan Kubb (dkubb) November 30th, 2008 @ 07:52 PM
Yeah, I'll look into it. I would like LazyArray to be useful for more than just Collection (although offhand I can't think of anything I'd use it for)
Please Sign in or create a free account to add a new ticket.
With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »