#401 new
Troy K

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

    Dan Kubb 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

    Troy K June 19th, 2008 @ 01:39 AM

    Ok, I'll take a look at OneToMany::Proxy and submit a patch for your review.

Please Login or create a free account to add a new comment.

You can update this ticket by sending an email to from your email client. (help)

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

Shared Ticket Bins

People watching this ticket

Tags