Skip to content
sxross edited this page Oct 24, 2012 · 1 revision

Getting started with MotionModel is simple. Make sure you are running Ruby 1.9.2 or newer by typing:

$ ruby -v
ruby 1.9.2p180 (2011-02-18 revision 30909) [x86_64-darwin11.4.0]

If you got something less than 1.9.2, you should consider using RVM to install and manage your rubies.

Installing

All good? Now you can install the gem:

gem install motion_model

you may need to use sudo for this. If you are using RVM, and I suggest you do, create a gemset.

rvm use 1.9.2
rvm gemset create motion

At this point, you are no longer installing the gem in the system gem directory so there should be less confusion.

Your First Project

Let's start with a project that has no UI. It's easier that way. From the command line, type:

$ motion create task_list

After a requisite bunch of messages, you will have a new folder called task_list. Change to that folder and fire up your favorite editor.

Editing Your Rakefile for MotionModel

require 'motion/project'
require 'rubygems'
require "motion_model"

You need to add the require for rubygems and motion_model after requiring motion/project.

Creating Your First Model

This is really a snap because any Ruby class can become a model. Here is a Task model:

class Task
  include MotionModel::Model

  columns :task_name => :string,
          :due_date  => :date,
end

Save this in your app folder as task.rb and go back to the command line.

A Quick Test

From the command line, type:

rake

You should have the REPL in your console window and the simulator should start. Click back in your console window to give it the focus. Now type this:

task = Task.create :task_name => 'Clean Up My Spammy Email', :due_date => '12/3/12'

You should see something like:

=> #<Task:0xf64ac20 @data={:task_name=>"Clean Up My Spammy Email", :due_date=>nil, :id=>1} @tz_offset=" -0700" @cached_date_formatter=#<NSDateFormatter:0xb063d70>>

Now, you can do things like:

puts task.task_name   # => 'Clean Up My Spammy Email'
puts task.count       # => 1

and of course, you can do:

task.delete
puts Task.count       # => 0

Note that in this second example, I've used the class method Task.count because I deleted the task object so it can't be relied upon to be valid. In practice, the actual task object will still be there for you, but it is no longer part of the backing store.

Congratulations. You just wrote a Ruby Motion program using MotionModel to handle data.

Clone this wiki locally