GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - alloy/MotionData: An experiment in using Core Data in a Ruby-ish way with RubyMotion

BranchesTags
Open more actions menu

Latest commit

 

History

116 Commits
116 Commits

Folders and files

NameName
Last commit message
Last commit date
app
app
 
 
examples/Recipes
examples/Recipes
 
 
lib
lib
 
 
spec
spec
 
 
vendor/motion_data/ext
vendor/motion_data/ext
 
 
.gitignore
.gitignore
 
 
.kick
.kick
 
 
Gemfile
Gemfile
 
 
Gemfile.lock
Gemfile.lock
 
 
README.md
README.md
 
 
Rakefile
Rakefile
 
 
TODO
TODO
 
 
motion_data.gemspec
motion_data.gemspec
 
 

Repository files navigation

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.

Schema

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
end

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
end

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

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

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

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

end

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)
end

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

Article.published.valid

Or on a to-many association:

author.articles.published.valid

About

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

Resources

Readme
Activity

Stars

143 stars

Watchers

10 watching

Forks

23 forks
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.