grape-entity
grape-entity copied to clipboard
expose and method_missing
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??
What's a scenario where this is a problem?
I believe the right way to implement this is to respond accordingly to :respond_to?.
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.
Ok, I don't disagree. Can you demonstrate the problem that you're trying to fix though in a test?
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.