Clever/clever-ruby — pull request #7 by carols10cents (2013-03-06 21:19:59+00:00)
Title: Return nil for option attributes that are not present instead of raising...

... a NoMethodError.

This is an intersection of the philosophy of hypermedia and ruby metaprogramming :)

We were running into NoMethodErrors when records did not have some of the optional attributes, which is because the hypermedia api is not returning anything for attributes that don't exist, and the ruby metaprogramming only defines methods for those attributes that exist in the response for that particular instance.

However, we don't want to have to do `if respond_to?(:state_id)` or `rescue NoMethodError` all over our code to handle this case, so in my opinion the methods for optional attributes should return nil (which has its own problems but is less of an issue than an exception).

The new test file `test/unit/optional_attributes_test.rb` now has an example of the behavior I'm advocating for.

I implemented this by adding an `optional_attributes` method in `clever_object` that is meant to be overwritten by the classes that inherit from this and contain a list of the attributes that are optional. I've populated these methods for the existing classes from the schema you've sent us.

I noticed the `@@permanent_attributes` class variable that is read a couple of places but I don't see anyplace that adds to that set-- was that a start to handling this issue?

One thing I've punted on for now is optional nested attributes like teacher name.middle. I think name, location, principal, and term could stand to be made into little Struct objects and then they could handle this issue in their own way, but I wanted to see what you thought of this change before I got too deep.

Thanks!!!!!!