[MacRuby-devel] Need to Write Core Data Accessors in MacRuby?
Eloy Duran
eloy at dekleineprins.me
Tue Jun 28 09:08:22 PDT 2011
That’s untested code btw.
On Tue, Jun 28, 2011 at 6:08 PM, Eloy Duran <eloy at dekleineprins.me> wrote:
> I have no experience with Core Data, but if you are looking to make
> repetitive work easy then don't forget about the metaprogramming
> possibilities with Ruby. Taking your example, you could create class
> method like the following:
>
> class Model
> def self.core_data_accessor(name)
> class_eval(%{
> def add#{name}Object(value)
> changedObjects = NSSet.setWithObject(value)
> willChangeValueForKey('#{name}',
> withSetMutation:NSKeyValueUnionSetMutation,
> usingObjects:changedObjects)
> primitiveValueForKey('#{name}').addObject(value)
> didChangeValueForKey('#{name}',
> withSetMutation:NSKeyValueUnionSetMutation,
> usingObjects:changedObjects)
> end
> })
> end
>
> core_data_accessor 'SubFolders'
> core_data_accessor 'OtherFolders'
> # etc
> end
>
> HTH
>
> On Tue, Jun 28, 2011 at 5:55 PM, Shannon Love <techzen at me.com> wrote:
>> I'm not to the point of wiring up the UI yet. I will employ bindings when I
>> do so but at the moment I am creating a complex data model using
>> NSManagedObject subclasses with lots of customized behaviors. I do this all
>> the time in Objective-c but I am uncertain how to go about doing so in
>> MacRuby.
>> The @dynamic processor in Objective-C 2.0 autogenerates attribute and
>> relationship accessors except for the to-many relationship convenience
>> methods but obviously MacRuby has no such capability. I don't think the
>> `attr_accessor ` in ruby will generate the proper core data accessors.
>> I suppose my real question is: Do we have to write all the accessors like we
>> used to do in the early days of Core Data in Objective-s 1.0 or is there
>> some functionality in ruby or MacRuby that obviates the need to do so?
>> Thanks,
>> Shannon
>>
>>
>> On Jun 27, 2011, at 5:55 PM, Matt Aimonetti wrote:
>>
>> Hey Shannon,
>> I'm not sure I fully understand, but you should be able to just set the
>> accessor and do the binding via Xcode as shown
>> here: http://ofps.oreilly.com/titles/9781449380373/_core_data.html
>> Let me know if that doesn't answer your question.
>> - Matt
>>
>> On Mon, Jun 27, 2011 at 3:45 PM, Shannon Love <techzen at me.com> wrote:
>>>
>>> Howdy,
>>>
>>> I've Core Data a lot in Objective-c and now I am trying to use it in
>>> MacRuby. It occurs to me that I might need to create the to-many
>>> relationship accessors just like you have to do in Objective-C.
>>>
>>> To clarify: Suppose I have a data model that models a file structure and
>>> which looks like this:
>>>
>>> Folder{
>>> name:string
>>> parent<<-->Folder.subFolders
>>> subFolders<-->>Folder.parent
>>> files<-->>File.folder
>>> }
>>> File{
>>> name:string
>>> folder<<-->Folder.file
>>> }
>>>
>>> In Objective-C, I would normally have methods in the `Folder` class that
>>> would look like:
>>>
>>> addSubFoldersObject:
>>> removeSubFoldersObject:
>>> addSubFoldersObjects:
>>> removeSubFoldersObjects:
>>>
>>> The methods themselves would use look something like:
>>>
>>> - (void)addSubFoldersObject:(FetchedPropertyExtractor *)value {
>>> NSSet *changedObjects = [[NSSet alloc] initWithObjects:&value
>>> count:1];
>>> [self willChangeValueForKey:@"SubFolders"
>>> withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
>>> [[self primitiveValueForKey:@"SubFolders"] addObject:value];
>>> [self didChangeValueForKey:@"SubFolders"
>>> withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
>>> [changedObjects release];
>>> }
>>>
>>> Do you have to do the same thing in MacRuby or will the normal ruby set
>>> operations suffice?
>>> Thanks,
>>> Shannon Love a.k.a TechZen
>>>
>>>
>>>
>>> _______________________________________________
>>> MacRuby-devel mailing list
>>> MacRuby-devel at lists.macosforge.org
>>> http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
>>>
>>
>> _______________________________________________
>> MacRuby-devel mailing list
>> MacRuby-devel at lists.macosforge.org
>> http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
>>
>>
>> _______________________________________________
>> MacRuby-devel mailing list
>> MacRuby-devel at lists.macosforge.org
>> http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
>>
>>
>
More information about the MacRuby-devel
mailing list