grape-entity icon indicating copy to clipboard operation
grape-entity copied to clipboard

expose and method_missing

Open svanderbleek opened this issue 12 years ago • 4 comments

valid_exposure? uses respond_to?, and does not work with method_missing. Something like this would make it work

def respond_to_attribute?(object, attribute)
  object.send(attribute)
  true
rescue
  false
end

but it does seem dirty. Is it desired behavior to put the burden on exposed objects to properly respond to respond_to? or any interest in a pull request using this instead of respond_to??

svanderbleek avatar Oct 25 '13 06:10 svanderbleek

What's a scenario where this is a problem?

I believe the right way to implement this is to respond accordingly to :respond_to?.

dblock avatar Oct 25 '13 11:10 dblock

It's pretty common to have method_missing without properly doing repsond_to?, Grape::Route for example. I was representing Grape::Route with Grape::Entity for an API that serves its own documentation.

svanderbleek avatar Oct 25 '13 17:10 svanderbleek

Ok, I don't disagree. Can you demonstrate the problem that you're trying to fix though in a test?

dblock avatar Oct 26 '13 20:10 dblock

I had a lot of trouble with this when I updated my matchers. It's very hard to test a single exposure in isolation because the object you pass through needs to have methods for all of the exposures in a given entity.

Before I had been mocking this out by stubbing the method for just the exposure I was testing, but I now need to make them all. This becomes complicated when you have nested entities, because then you need to handle this for the current entity and the nested entity.

idyll avatar Mar 28 '14 17:03 idyll