Ruby CoreData Tutorial
December 31, 2005 § 1 Comment
As part of my DARC strategy, I’m working on creating a Rails Adaptor for CoreData. The first step is simply getting
CoreData to talk to Ruby. Fortunately, RubyCocoa 0.4.2 adds CoreData support, so this should be
doable.
CoreData to talk to Ruby. Fortunately, RubyCocoa 0.4.2 adds CoreData support, so this should be
doable.
I have created a file — cdcli.rb — which implements Apple’s “Low-Level Core Data Tutorial” in Ruby, resulting
in a command-line utility which programmatically creates and uses a Core Data
model.
Update:
With important help from Chris Thomas of RubyCocoa — it works! In addition, he
pointed me to the Xcodes templates, which allowed me to do the same example
(much more simply!) as a Ruby Core Data
Document-based
Application.
[Read More] for
notes on what I am learning along the way.
1. Must use RubyCocoa 0.4.2, as that is the first
version with Core Data support
version with Core Data support
2. require ‘osx/coredata’
as well as ‘osx/cocoa’
as well as ‘osx/cocoa’
3. Remember to prefix everything with
“OSX::” (or use include
OSX)
“OSX::” (or use include
OSX)
4. Trailing ‘()’ not needed, but helps me
remember what’s a function
remember what’s a function
5. p.14: Do
not
use AutoRelease or create a pool (Ruby
takes care of all that)
not
use AutoRelease or create a pool (Ruby
takes care of all that)
6. p. 19:
OSX::NSFileManager#fileExistsAtPath:isDirectory:
requires a pointer
OSX::NSFileManager#fileExistsAtPath:isDirectory:
requires a pointer
7. -> pass normally, trust RubyCocoa to “do
the right thing”
the right thing”
8. -> pass in Boolean as string “NO”; seems to
work… (should probably pass ‘nil’ or
maybe ‘^c’)
work… (should probably pass ‘nil’ or
maybe ‘^c’)
9. p. 22: External constants implemented as
methods: STORE_TYPE =
OSX::NSXMLStoreType()
methods: STORE_TYPE =
OSX::NSXMLStoreType()
10. p. 22: Same issue with
error
in
OSX::NSPersistentStoreCoordinator.addPersistentStoreWithType
error
in
OSX::NSPersistentStoreCoordinator.addPersistentStoreWithType
11. ->
error =
OSX::NSError.alloc.init # to ensure right type
(may not be needed)
error =
OSX::NSError.alloc.init # to ensure right type
(may not be needed)
12. p. 23: Have
managedObjectContext
take ‘mom’ as a parameter, passed in by main
managedObjectContext
take ‘mom’ as a parameter, passed in by main
13. -> will be needed there for entity lookup,
though it is designed to be called twice
though it is designed to be called twice
14. p. 25: Define Run class in same file, for
simplicity (gotta love Ruby’s dynamism 🙂
simplicity (gotta love Ruby’s dynamism 🙂
15. p. 26: Define “processID” as a primitive; no
real scalars in Ruby, and @ processID causes problems
real scalars in Ruby, and @ processID causes problems
16. p. 27:
awakeFromInsert
requires explicit: ns_overrides
‘awakeFromInsert’
awakeFromInsert
requires explicit: ns_overrides
‘awakeFromInsert’
17. By jove, I think it works! (as Tue Jan 3rd @
3 PM PST)
3 PM PST)
Hi Ernest, I have reimplemented the Low-level Core Data Tutorial starting from your implementation so that it works under current RubyCocoa releases. It was really useful to have a head start into the matter, and your sample code was of great help, but I found that it does not run under RubyCocoa 0.10. For reference, my implementation can be found at this link. Any comments or suggestions are welcome!
sergio