Skip to content

Instantly share code, notes, and snippets.

@link82
Created July 4, 2012 19:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save link82/3049225 to your computer and use it in GitHub Desktop.
Save link82/3049225 to your computer and use it in GitHub Desktop.
Nested object collection mapping
//
// Album.m
// NeedleMusic
//
// Created by Davide Cenzi on 13/05/12.
// Copyright (c) 2012 No1. All rights reserved.
//
#import "Album.h"
#import "Song.h"
@implementation Album
@synthesize code = _code;
@synthesize source = _source;
@synthesize collection_name = _collection_name;
@synthesize artist_name = _artist_name;
@synthesize genre = _genre;
@synthesize genre_id = _genre_id;
//@synthesize release_date = _release_date;
@synthesize collection_id = _collection_id;
@synthesize label_id = _label_id;
@synthesize label = _label;
@synthesize diffusion = _diffusion;
@synthesize small_thumb = _small_thumb;
@synthesize big_thumb = _big_thumb;
@synthesize songs = _songs;
+ (void)setupMapping{
//managerWithBaseURLString:[[BTAuth sharedObject] apiURL]
RKObjectManager* objectManager = [RKObjectManager sharedManager];
// Setup our object mappings
RKObjectMapping* albumMapping = [Album mapping];
[objectManager.mappingProvider setObjectMapping:albumMapping forKeyPath:@"album"];
// Serialization mapping for user class
RKObjectMapping* albumSerialization = [Album serializationMapping];
[objectManager.mappingProvider setSerializationMapping:albumSerialization forClass:[Album class]];
//Routes for user class
[objectManager.router routeClass:[Album class] toResourcePath:@"/albums/:code"];
[objectManager.router routeClass:[Album class] toResourcePath:@"/albums" forMethod:RKRequestMethodPOST];
}
+(RKObjectMapping*)mapping{
RKObjectMapping* albumMapping = [RKObjectMapping mappingForClass:[Album class]];
albumMapping.rootKeyPath = @"album";
[albumMapping mapKeyPath:@"id" toAttribute:@"code"];
[albumMapping mapKeyPath:@"source" toAttribute:@"source"];
[albumMapping mapKeyPath:@"collection_name" toAttribute:@"collection_name"];
[albumMapping mapKeyPath:@"artist_name" toAttribute:@"artist_name"];
[albumMapping mapKeyPath:@"genre" toAttribute:@"genre"];
[albumMapping mapKeyPath:@"genre_id" toAttribute:@"genre_id"];
[albumMapping mapKeyPath:@"collection_id" toAttribute:@"collection_id"];
[albumMapping mapKeyPath:@"label_id" toAttribute:@"label_id"];
[albumMapping mapKeyPath:@"label" toAttribute:@"label"];
[albumMapping mapKeyPath:@"diffusion" toAttribute:@"diffusion"];
[albumMapping mapKeyPath:@"small_thumb" toAttribute:@"small_thumb"];
[albumMapping mapKeyPath:@"big_thumb" toAttribute:@"big_thumb"];
RKObjectMapping *songMapping = [RKObjectMapping mappingForClass:[Song class]];
[albumMapping mapKeyPath:@"songs" toRelationship:@"songs" withMapping:songMapping];
return albumMapping;
}
+ (RKObjectMapping*)serializationMapping{
RKObjectMapping* albumSerialization = [RKObjectMapping mappingForClass:[Album class]];
//[albumSerialization mapKeyPath:@"code" toAttribute:@"album[id]"];
[albumSerialization mapKeyPath:@"source" toAttribute:@"album[source]"];
[albumSerialization mapKeyPath:@"collection_name" toAttribute:@"album[collection_name]"];
[albumSerialization mapKeyPath:@"artist_name" toAttribute:@"album[artist_name]"];
[albumSerialization mapKeyPath:@"genre" toAttribute:@"album[genre]"];
[albumSerialization mapKeyPath:@"genre_id" toAttribute:@"album[genre_id]"];
[albumSerialization mapKeyPath:@"collection_id" toAttribute:@"album[collection_id]"];
[albumSerialization mapKeyPath:@"label" toAttribute:@"album[label]"];
[albumSerialization mapKeyPath:@"label_id" toAttribute:@"album[label_id]"];
[albumSerialization mapKeyPath:@"small_thumb" toAttribute:@"album[small_thumb]"];
[albumSerialization mapKeyPath:@"big_thumb" toAttribute:@"album[big_thumb]"];
RKObjectMapping *songMapping = [RKObjectMapping mappingForClass:[Song class]];
[albumSerialization mapKeyPath:@"songs" toRelationship:@"songs" withMapping:[songMapping inverseMapping] serialize:YES];
return albumSerialization;
}
@end
//
// Song.m
// NeedleMusic
//
// Created by Davide Cenzi on 17/05/12.
// Copyright (c) 2012 No1. All rights reserved.
//
#import "Song.h"
@implementation Song
@synthesize code = _code;
@synthesize source = _source;
@synthesize album_id = _album_id;
@synthesize track_name = _track_name;
@synthesize track_time_millis = _track_time_millis;
@synthesize track_time = _track_time;
@synthesize disc_number = _disc_number;
@synthesize track_number = _track_number;
@synthesize audios = _audios;
@synthesize preview_url = _preview_url;
+ (RKObjectMapping*)mapping{
RKObjectMapping* songMapping = [RKObjectMapping mappingForClass:[Song class]];
songMapping.rootKeyPath = @"song";
[songMapping mapKeyPath:@"id" toAttribute:@"code"];
[songMapping mapKeyPath:@"source" toAttribute:@"source"];
[songMapping mapKeyPath:@"album_id" toAttribute:@"album_id"];
[songMapping mapKeyPath:@"track_name" toAttribute:@"track_name"];
[songMapping mapKeyPath:@"track_time_millis" toAttribute:@"track_time_millis"];
//calculated attribute
[songMapping mapKeyPath:@"track_time" toAttribute:@"track_time"];
[songMapping mapKeyPath:@"disc_number" toAttribute:@"disc_number"];
[songMapping mapKeyPath:@"track_number" toAttribute:@"track_number"];
#warning removed relationship 'audios' from song
//[songMapping hasMany:@"audios" withMapping:[Audio mapping]];
return songMapping;
}
+ (RKObjectMapping*)serializationMapping{
RKObjectMapping *songSerialization = [RKObjectMapping mappingForClass:[Song class]];
//[songSerialization mapKeyPath:@"code" toAttribute:@"song[id]"];
[songSerialization mapKeyPath:@"source" toAttribute:@"source"];
[songSerialization mapKeyPath:@"album_id" toAttribute:@"album_id"];
[songSerialization mapKeyPath:@"track_name" toAttribute:@"track_name"];
//[songSerialization mapKeyPath:@"track_time_millis" toAttribute:@"track_time_millis"];
//skip track_time, it's a calculated attribute
[songSerialization mapKeyPath:@"disc_number" toAttribute:@"disc_number"];
[songSerialization mapKeyPath:@"track_number" toAttribute:@"track_number"];
//custom attribute used to track audio player
//[songSerialization mapKeyPath:@"preview_url" toAttribute:@"preview_url"];
return songSerialization;
}
+ (void)setupMapping{
//managerWithBaseURLString:[[BTAuth sharedObject] apiURL]
RKObjectManager* objectManager = [RKObjectManager sharedManager];
// Setup our object mappings
RKObjectMapping* songMapping = [Song mapping];
[objectManager.mappingProvider setObjectMapping:songMapping forKeyPath:@"song"];
// Serialization mapping for user class
RKObjectMapping* songSerialization = [Song serializationMapping];
[objectManager.mappingProvider setSerializationMapping:songSerialization forClass:[Song class]];
//Routes for user class
#warning removed routes for songs, sent only within an album object
//[objectManager.router routeClass:[Song class] toResourcePath:@"albums/:album_id/songs/:code"];
//[objectManager.router routeClass:[Song class] toResourcePath:@"albums/:album_id/songs" forMethod:RKRequestMethodPOST];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment