Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
108 lines (75 sloc) 2.78 KB

An experiment in using Core Data in a Ruby-ish way

This uses a DSL which is inspired by DataMapper, but also ActiveRecord. In addition, many of its Core Date related design principles are based on MagicalRecord.


The following models define a schema that is immediatly available during development:

class Author < MotionData::ManagedObject
  hasMany :articles, :class => 'Article', :inverse => :author

  property :name, String, :required => true

class Article < MotionData::ManagedObject
  belongsTo :author, :class => 'Author', :inverse => :articles

  property :title,     String,  :required => true
  property :body,      String,  :required => true
  property :published, Boolean, :default  => false

The Schema instance can dump this definition, which looks like:

Schema.defineVersion('1.0') do |s|

  s.addEntity do |e| = 'Article'
    e.managedObjectClassName = 'Article'
    e.addProperty :published, Boolean, {:default=>false}
    e.addProperty :title, String, {:required=>true}
    e.addProperty :body, String, {:required=>true}

  s.addEntity do |e| = 'Author'
    e.managedObjectClassName = 'Author'
    e.addProperty :name, String, {:required=>true}


As you can see it has a version, this is the app’s release version. These dumps would be created on each new release of the app and would then allow for easy migrations with code that can be found in @mdiep’s CoreDataInCode example.

Dynamic scopes

MotionData provides ‘finder scopes’, which are conceptually the same as those in ActiveRecord.

For instance, to select a subset of an entity’s to-many association:

author.articles.where(:published => true)
author.articles.where(:published => true).sortBy(:title)

Or for predicates that are more elaborate than simple AND condtitions, you can use a predicate builder proxy:

author.articles.where(value(:body) != nil).and(value(:body) != nil)

Finally, in case you need to create a typical NSPredicate, you can do that too and pass the instance to where, or create one with where:

author.articles.where('body != %@ AND published == %@', nil, true)

Named scopes

The query builder methods that can be found in the ‘dynamic scopes’ section, can also be ‘saved’ on the model:

class Article
  scope :published, where(:published => true)
  scope :valid, where(value(:body) != nil)

This makes these scopes available to query all of a model’s entities:


Or on a to-many association: