Git Product home page Git Product logo

motiondata's Introduction

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

motiondata's People

Contributors

alloy avatar kemiller avatar naoyamakino avatar jonathanpenn avatar jetpad avatar tbugai avatar

Stargazers

Yoshida Seisuke avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.