Clean up and speed up ref attribute resolution

For my [schema validation benchmark](https://github.com/treppo/json-schema/blob/benchmarkin/bench/benchmark.rb), this change means a ~10% speedup. I extracted a few methods on the way to clean up.