I love the Nitron gem created by @mattgreen. But I missed some features, and I really hate to open Xcode to create the Core Data model. So I created some rake tasks that helped me generate the Core Data model. Thanks to @mattgreen for starting this in the 0.3 branch of the Nitron project.
Another reason why I wanted to create this gem is because I love using Magical Record. This is really a great way to handle Core Data, it's like a small layer on top of it.
Most gems that want to --railsify-- Core Data don't make correct use of the different contexts as used by Core Data. So I wanted to be able to use Core Data as it should, by just generating the model.
Maybe adding some simplicity in creating the relationships between models will be added later. But for now, generating the Core Data model is the main goal!
The installation is simple. Just add this line to you Gemfile:
$ bundle install
And BOOM, you're on your way!
With this gem you can set every option you can while using the Xcode modelling tool. I'm not going to describe every option because I assume RubyMotion developers know how Core Data works. Here is a short list of the available options for a property:
And of course here are the available options for the relationships (belongs to and has many):
The 'spec_project' is an example project with two models containing properties and relationships.
But the main question is how to generate this Core Data model. Well start by extending your models from MotionMigrate::Model. This class is extended from NSManagedObject so you're ready to continue with Core Data once the model is generated.
class Plane < MotionMigrate::Model end
Next define the properties:
class Plane < MotionMigrate::Model property :name, :string property :multi, :boolean, :default => false end
This will generate two properties, a name property and a multi property. To add some relationships to it, you can add a belongs_to -- rails-like-shizzle-- to the model.
class Plane < MotionMigrate::Model property :name, :string property :multi, :boolean, :default => false belongs_to :pilot, :class_name => "Pilot", :inverse_of => :planes end
Don't forget to add a has_many or belongs_to as a reverse relationship.
class Pilot < MotionMigrate::Model has_many :planes, :class_name => "Plane", :inverse_of => :pilot end
The relationships as defined above contain the minimal parameters you'll have to pass to the has_many or belongs_to.
Now the most important part, migrating the model. Just run this command to generate the Core Data model from the current models.
$ rake db:migrate
You can also revert to a previous version of the Core Data model by running:
$ rake db:rollback
You can check out the other rake tasks by running:
$ rake -T
But for me, the most obvious part is an small example application that shows you how this gem is used. Check out the spec_project en run it so you can see it in action.
This is certainly not the end, still got a lot to do.
Copyright (c) 2013 Jelle Vandebeeck
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.