O'Reilly logo

RubyMotion by Clay Allsopp

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Adding Hash-Friendly Models

Let’s start with the models. The model code should be independent of any other classes, so filling them in is usually a good place to start. Resist the temptation to make explicit references to your views or controllers!

First up are the Color objects. The Colr API returns its colors as JSON objects in the following form:

 {
 "timestamp"​​:​ 1285886579,
 "hex"​​:​ ​"ff00ff"​,
 "id"​​:​ 3976,
 "tags"​​:​ [{
 "timestamp"​: 1108110851,
 "id"​: 2583,
 "name"​: ​"fuchsia"
  }]
 }

Right away, it looks like we need timestamp, hex, id, and tags properties. In particular, tags represents a has-many relationship with Tag objects.

Let’s get down to business. Open color.rb and start filling it in with our ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required