[MacRuby-devel] Some general MacRuby/Cocoa questions
Hi List, I'm new to the list and currently I'm struggling with MacRuby (or better: Cocoa). I've done some Ruby development (mostly Rails) in the past and think my knowledge about ruby is okay. But I've no idea about Cocoa and all I know about C are a few fragments C++ back from university... Currently I try to teach me some MacRuby/Cocoa with hacking together a small desktop application. Matt Aimonetti's book is already ordered and I hope it will arrive next week. I did some reading back when it was available for free on the net. So I already know some (small) things but actually I've some questions and I hope I'll find some answers on the list. If this is the wrong place to ask such questions, please let me know. At the moment my application mostly consists of a NSTableView bound to a CoreData Entity. Works like expected. In a next step I would like to extend my application to open another window to enter the details for adding content. I've designed the window in the interface builder (Another.xib) and added another ruby file to my project with the following content: class AnotherController < NSWindowController def windowNibName() return "Another" end end I added a NSObject in IB and set it's class to AnotherController. In my AppDelegate.rb I've added a method which gets called when selecting a menuitem: def addItem(sender) newWindow = AnotherController.alloc.init newWindow.window.makeKeyAndOrderFront(self) end Now I can select the menu entry, which is calling addItem, which then opens the other window. This is working, but it doesn't feel right. Especially the AnotherController things seems a little bit odd to me. Is there a better way to do this? What are the next steps? What would be the best way to add another record to my CoreData based on the form values of my second window? I find it generally hard to imagine how to arrange a files in a larger MacRuby/Cocoa project. Is there some good reading on this topic? Regards, Timo -- twitter.com/orangeorb ___ MacRuby-devel mailing list [email protected] http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
Re: [MacRuby-devel] Some general MacRuby/Cocoa questions
Good morning, Am 11.11.2011 um 23:36 schrieb Henry Maddocks: > On 12/11/2011, at 10:07 AM, Timo Springmann wrote: >> class AnotherController < NSWindowController >> def windowNibName() >>return "Another" >> end >> end > > From a code point of view what you have done here is correct, one controller > per window (view), the rails paradigm is similar to cocoa. > But from a UI point of view this isn't very Mac like. Mac apps tend to have > the list and detail views in panes in the same window, a split view. Look at > mail.app, message list down one side, message details on the other side. To stay with your Mail.app example: when you create a new mail you do this in a new window (at least in Mail.app from Snow Leopard, don't know about Lion). As my data will have many attributes I would like to open a new window to enter all the details. I've also planned a "preview pane", where you can see the objects details when you select them in the TableView. I think one controller per window is fine. What I don't like about the code is the windowNibName method. Do I really have to do it this way? Or is there a better way to 'connect' my in IB designed window to my ruby contoller class? Regards, Timo -- twitter.com/orangeorb ___ MacRuby-devel mailing list [email protected] http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
[MacRuby-devel] Create new Core Data Instances
Hi List, I've successfully created a core data entity with some attributes. I'm displaying object in a NSTableView and I was able to add new instances by calling the entities array controller add: action. The last hour I tried to figure out how to add/create a core data entity in ruby code without using the interface builder. I tried to instantiate a new object like this: MyEntity.new but I always get the following error: Failed to call designated initializer on NSManagedObject class 'MyEntity' What's the proper way of doing this? For rails-devs: I'm looking for something like: a = MyARObject.new a.attribute1 = "test" a.attribute2 = "test2 a.save Regards, Timo -- twitter.com/orangeorb ___ MacRuby-devel mailing list [email protected] http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
Re: [MacRuby-devel] Create new Core Data Instances
A small step ahead...
... the following code seems to create an instance of MyEntity and sets the
attribute 'attribute1' to the value 'Test'.
entityDesc = NSEntityDescription.new
entityDesc.setName("MyEntity")
att = NSAttributeDescription.new
att.setAttributeType(NSStringAttributeType)
att.setName("attribute1")
entityDesc.setProperties([att])
instance = MyEntity.alloc.initWithEntity(entityDesc,
insertIntoManagedObjectContext:nil)
instance.setValue("Test", forKey:"attribute1")
Two question arising from this code:
a) Is it really THAT complicated to create an instance of MyEntity and set the
attribute? I'm coming from the Rails/ActiveRecord world where this could easily
be done with a single line of code (including saving the instantiated object):
MyEntity.new(:attribute1 => "Test").save )
b) How can I save this fresh instance and update my NSTableView?
Regards,
Timo
Am 13.11.2011 um 22:20 schrieb Timo Springmann:
> Hi List,
>
> I've successfully created a core data entity with some attributes. I'm
> displaying object in a NSTableView and I was able to add new instances by
> calling the entities array controller add: action.
>
> The last hour I tried to figure out how to add/create a core data entity in
> ruby code without using the interface builder. I tried to instantiate a new
> object like this:
>
> MyEntity.new
>
> but I always get the following error:
>
> Failed to call designated initializer on NSManagedObject class 'MyEntity'
>
> What's the proper way of doing this?
>
> For rails-devs: I'm looking for something like:
> a = MyARObject.new
> a.attribute1 = "test"
> a.attribute2 = "test2
> a.save
>
> Regards,
> Timo
>
> --
> twitter.com/orangeorb
>
> ___
> MacRuby-devel mailing list
> [email protected]
> http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
--
Ich im Web 2.0 - www.orangeorb.de - twitter.com/orangeorb
___
MacRuby-devel mailing list
[email protected]
http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
Re: [MacRuby-devel] Create new Core Data Instances
Hi,
Am 14.11.2011 um 09:47 schrieb Sven A. Schmidt:
>> a) Is it really THAT complicated to create an instance of MyEntity and set
>> the attribute? I'm coming from the Rails/ActiveRecord world where this could
>> easily be done with a single line of code (including saving the instantiated
>> object): MyEntity.new(:attribute1 => "Test").save )
>>
>> b) How can I save this fresh instance and update my NSTableView?
>
> After you have modeled your entity in the Xcode core data modeling view, you
> can use
>
>instance = NSEntityDescription.insertNewObjectForEntityForName(
> "MyEntity", inManagedObjectContext:managedObjectContext
>)
>
> to instantiate an instance. You can save it calling
>
>error = Pointer.new_with_type("@")
>managedObjectContext.save(error)
>
> To save attributes, simply assign to them:
>
>instance.attribute1 = 'Test'
>
> This assumes you've added that attribute to your model. The
> managedObjectContext can be created or, if you're using a core data project
> like the document one, you'll have a managedObjectContext accessible from
> your NSDocument subclass: self. managedObjectContext
I've already added attributes to my model. I'm not sure about this
manageObjectContext thing. I've created the Entity with it's attributes in IB.
I added an NSTableView to my window and connected it to my model. Objects are
displayed in the TableView and I also can add record directly in the table
(connected the add: action to a button). So my model seems to be fine. Next
step would be to create an object more manually (i.e. for some kind of Import
functions...). I'm not sure where I get this manageObjectContext from in my
current situation or how to create one. It also seems like I should dig a
little more into Core Date and check how it works... currently it seems a lot
more complicated than ActiveRecord to me...
> If you have bound your NSTableView to the model object, changes will be
> picked up automatically. You'll only need to call save to actually persist to
> disk.
I hoped that would be the case. Good to know it is.
> I hope this helps somewhat, there's much more detail to it and the following
> links explain more of those:
>
> http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreData/cdProgrammingGuide.html
I'll take a look.
> and some MacRuby specific ones that I've found useful in the past
>
> http://www.springenwerk.com/2008/10/macruby-and-core-data-tutorial.html
> http://cyberfox.com/blog/120-how-i-learned-to-stop-worrying-about-core-data-and-love-macruby
> http://reborg.tumblr.com/post/263347770/macruby-coredata-tutorial
Thanks for the links. I've already found and read them before. But they didn't
answer my questions (or I were too blind to see it). I've got feeling that I've
read every single page about MacRuby that Google will find ;-)
Regards,
Timo
--
twitter.com/orangeorb
___
MacRuby-devel mailing list
[email protected]
http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
