-
Notifications
You must be signed in to change notification settings - Fork 67
Getting Started
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.
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.
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.
require 'motion/project'
require 'rubygems'
require "motion_model"You need to add the require for rubygems and motion_model after requiring motion/project.
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,
endSave this in your app folder as task.rb and go back to the command line.
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 # => 1and of course, you can do:
task.delete
puts Task.count # => 0Note 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.