87 lines
2.9 KiB
Objective-C
87 lines
2.9 KiB
Objective-C
//
|
|
// Verb+Create.m
|
|
// Espagram
|
|
//
|
|
// Created by Abel Fokkinga on 11/16/12.
|
|
// Copyright (c) 2012 Abel Fokkinga. All rights reserved.
|
|
//
|
|
|
|
#import "Verb+Create.h"
|
|
|
|
|
|
@implementation Verb (Create)
|
|
|
|
+ (void) addVerb:(NSString *)newVerb andMeaning:(NSString *)meaning toLesson:(Lesson *)lesson
|
|
{
|
|
Verb * verb = [NSEntityDescription insertNewObjectForEntityForName:@"Verb" inManagedObjectContext:[lesson managedObjectContext]];
|
|
|
|
newVerb = [newVerb lowercaseString];
|
|
|
|
// Check first if the lessons already contains this verb
|
|
for ( Verb * v in lesson.verbs) {
|
|
if ( [newVerb isEqualToString:v.verb]) {
|
|
if (meaning == nil)
|
|
return;
|
|
else if ([meaning isEqualToString:v.meaning])
|
|
return;
|
|
}
|
|
}
|
|
|
|
if ( [lesson.conjugator isEqualToString:@"Spanish"]) {
|
|
if ( newVerb.length >= 2 ) {
|
|
NSString *verbExit = [newVerb substringFromIndex:newVerb.length - 2];
|
|
if ( ![verbExit isEqualToString:@"ar"] &&
|
|
![verbExit isEqualToString:@"er"] &&
|
|
![verbExit isEqualToString:@"ir"] &&
|
|
![verbExit isEqualToString:@"ír"] &&
|
|
![verbExit isEqualToString:@"se"] ) {
|
|
// Not an existing Spanish verb
|
|
return;
|
|
}
|
|
if ( [verbExit isEqualToString:@"se"] && newVerb.length >= 4 ) {
|
|
verbExit = [[newVerb substringFromIndex:newVerb.length - 4] substringToIndex:2];
|
|
if ( ![verbExit isEqualToString:@"ar"] &&
|
|
![verbExit isEqualToString:@"er"] &&
|
|
![verbExit isEqualToString:@"ir"] &&
|
|
![verbExit isEqualToString:@"ír"] ) {
|
|
// Not an existing Spanish verb
|
|
return;
|
|
}
|
|
}
|
|
} else {
|
|
// Verb should be longer than 2 characters
|
|
return;
|
|
}
|
|
}
|
|
NSLog(@"Adding word %@", newVerb);
|
|
verb.verb = newVerb;
|
|
verb.meaning = meaning;
|
|
[lesson addVerbsObject:verb];
|
|
[[lesson managedObjectContext] save:nil];
|
|
};
|
|
|
|
+ (Verb *) searchWordMeaning:(NSString *)verb usingLessonLanguage:(Lesson *)lesson
|
|
{
|
|
|
|
NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Verb"];
|
|
|
|
|
|
// Where clause
|
|
request.predicate = [NSCompoundPredicate andPredicateWithSubpredicates:[NSArray arrayWithObjects:[NSPredicate predicateWithFormat:@"verb = %@", verb],
|
|
[NSPredicate predicateWithFormat:@"meaning != nil AND meaning !=''"],
|
|
[NSPredicate predicateWithFormat:@"lesson.conjugator = %@", lesson.conjugator],nil]];
|
|
|
|
|
|
|
|
NSError *error = nil;
|
|
NSArray *verbs = [[lesson managedObjectContext] executeFetchRequest:request error:&error];
|
|
|
|
if ( verbs && verbs.count > 0 ){
|
|
return [verbs lastObject];
|
|
}
|
|
|
|
return nil;
|
|
};
|
|
|
|
@end
|