Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added convenience methods #17

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Classes/EGODatabaseRow.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,14 @@
- (NSDate*)dateForColumn:(NSString*)name;
- (NSDate*)dateForColumnAtIndex:(NSUInteger)index;

- (id)populateObject:(id)obj;
- (id)populateObject:(id)obj mappings:(NSDictionary*)d;

- (id)objectOfClass:(Class)c;
- (id)objectOfClass:(Class)c mappings:(NSDictionary*)d;

@property(nonatomic,strong,readonly) NSArray* data;
@property(nonatomic,strong,readonly) NSArray* names;
@property(nonatomic,readonly) NSDictionary* dictionary;

@end
47 changes: 41 additions & 6 deletions Classes/EGODatabaseRow.m
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,14 @@
#import "EGODatabaseRow_Internal.h"
#import "EGODatabaseResult.h"

@interface EGODatabaseRow ()

@property(nonatomic,strong) NSArray* names;
@end

@implementation EGODatabaseRow

@dynamic dictionary;
@synthesize names = _names;

- (instancetype)initWithDatabaseResult:(EGODatabaseResult*)result data:(NSArray*)data {
if((self = [super init])) {
self.names = result.columnNames;
_names = result.columnNames;
self.data = data;
}

Expand Down Expand Up @@ -150,4 +148,41 @@ - (NSDate*)dateForColumnAtIndex:(NSUInteger)index {
return [NSDate dateWithTimeIntervalSince1970:[self doubleForColumnAtIndex:index]];
}

- (NSDictionary*)dictionary
{
return [NSDictionary dictionaryWithObjects:self.data forKeys:self.names];
}

- (id)populateObject:(id)obj
{
[obj setValuesForKeysWithDictionary:self.dictionary];
return obj;
}

-(id)populateObject:(id)obj mappings:(NSDictionary*)d
{
NSMutableArray* newNames = [NSMutableArray arrayWithCapacity:self.names.count];
for (NSString* n in self.names)
{
NSString* newName = d[n];
if(!newName) { newName = n; }
[newNames addObject:newName];
}
[obj setValuesForKeysWithDictionary:[NSDictionary dictionaryWithObjects: self.data forKeys: newNames]];
return obj;
}

- (id)objectOfClass:(Class)c
{
id obj = [[c alloc]init];
return [self populateObject:obj];
}

- (id)objectOfClass:(Class)c mappings:(NSDictionary*)d
{
id obj = [[c alloc]init];
return [self populateObject:obj mappings:d];
}


@end