CoreData Motion

This project aims to provide a ruby-like way of creating CoreData models using a DSL, avoiding the need to create them using the XCode GUI builder.

Installation

Using bundler:

If you are using bundler for managing dependencies, you will need to add the gem to your project's Gemfile:

# Gemfile
source "https://rubygems.org"

gem "rake"
gem "core_data_motion"
# Some other gems for your poject

and setup your Rakefile to properly require bundler's dependencies:

# -*- coding: utf-8 -*-
# Rakefile
$:.unshift("/Library/RubyMotion/lib")
require 'motion/project/template/ios'
require 'rubygems'
require 'bundler'

Bundler.setup
Bundler.require

Motion::Project::App.setup do |app|
  # Use `rake config' to see complete project settings.
  app.name = 'your_app_name'

  app.frameworks += %w(CoreData)

  app.files.unshift(*Dir.glob(File.join(app.project_dir, 'app/lib/**/*.rb')))
end

Without bundler

If you are not managing dependencies with bundler, you will need to install the gem with:

gem install core_data_motion

And then require it in your Rakefile:

# -*- coding: utf-8 -*-
# Rakefile
$:.unshift("/Library/RubyMotion/lib")
require 'motion/project/template/ios'
require 'rubygems'
require 'core_data_motion'

Motion::Project::App.setup do |app|
  # Use `rake config' to see complete project settings.
  app.name = 'your_app_name'

  app.frameworks += %w(CoreData)

  app.files.unshift(*Dir.glob(File.join(app.project_dir, 'app/lib/**/*.rb')))
end

Usage example:

Create a file under your settings/ directory and define the ModelDefinition class as follows:

class ModelDefinition < CDM::EntityDefinition
  def define_model
    create_entity :project do |e|
      e.string    :name
      e.string    :description_text
      e.date      :start_date
      e.double    :budget
    end

    create_entity :task do |e|
      e.int16     :project_id, optional: false
      e.string    :name
      e.string    :details
    end

    create_entity :comment do |e|
      e.int16     :owner_id, optional: false
      e.string    :owner_type
      e.string    :content
    end
  end
end

Run the following rake task to create the files needed to create the data model:

rake coredata:setup

If you are using MagicalRecord, you can initialize your database in your application delegate by running:

MagicalRecord.setupCoreDataStackWithStoreNamed('database.sqlite')

You can use any CoreData wrapper you want (or none at all) to query for records and save them to the device's local database.

Available data-types

The available data types in a CoreData model are:

  • Binary e.binary :binary_field
  • Boolean e.boolean :boolean_field
  • Date e.date :date_field
  • Decimal e.decimal :decimal_field
  • Double e.double :double_field
  • Float e.float :float_field
  • Integer 16 e.int16 :int_field
  • Integer 32 e.int32 :int_field
  • Integer 64 e.int64 :int_field
  • String e.string :string_field
  • Transformable e.transformable :transformable_field

Contributing

This is a work in progress and help is greatly appreciated! If you want to add a feature, fix a bug or help with documentation, you are very welcome to do so. Just follow the usual way of contributing:

  • Fork the repo.
  • Create a feature branch.
  • Make tests for the feature you want to add.
  • Make the tests pass.
  • Send a pull request.