38 #import <ActiveModelKit/ActiveModelKit.h>
41 #import <ActiveSupportKit/ActiveSupportKit.h>
64 - (id)copyWithZone:(NSZone *)zone
66 ARResource *copy = [[[
self class] allocWithZone:zone] init];
67 [copy setService:[
self service]];
80 if ([
self respondsToSelector:
@selector(site)])
82 [service setSite:[
self performSelector:@selector(site)]];
84 if ([
self respondsToSelector:
@selector(elementName)])
86 [service setElementName:[
self performSelector:@selector(elementName)]];
90 [service setElementName:[[[AMName alloc] initWithClass:self] element]];
101 [
self setService:service];
106 - (id)initWithService:(
ARService *)service attributes:(NSDictionary *)attributes
116 - (id)initWithService:(
ARService *)service attributes:(NSDictionary *)attributes persisted:(BOOL)persisted
121 [
self setPersisted:persisted];
135 @synthesize service = _service;
142 [
self setService:service = [[
self class] service]];
148 #pragma mark Attributes
158 return [_attributes copy];
161 - (void)setAttributes:(NSDictionary *)attributes
169 - (void)mergeAttributes:(NSDictionary *)attributes
176 id attributeValue = [
attributes objectForKey:attributeName];
177 [
_attributes setObject:attributeValue forKey:attributeName];
181 - (void)loadAttributes:(NSDictionary *)attributes removeRoot:(BOOL)removeRoot
186 [
self setPrefixOptions:prefixOptions];
206 - (id)valueForUndefinedKey:(NSString *)key
208 return [
_attributes objectForKey:[[ASInflector defaultInflector] underscore:key]];
211 - (void)setValue:(
id)value forUndefinedKey:(NSString *)key
213 [
_attributes setObject:value forKey:[[ASInflector defaultInflector] underscore:key]];
216 - (void)setNilValueForKey:(NSString *)key
218 [
self setValue:[NSNull null] forKey:key];
230 - (NSDictionary *)dictionaryWithValuesForKeys:(NSArray *)keys
232 NSMutableDictionary *keyedValues = [NSMutableDictionary dictionary];
233 for (NSString *key in keys)
235 [keyedValues setObject:[
self valueForKey:key] forKey:key];
237 return [keyedValues copy];
240 - (void)setValuesForKeysWithDictionary:(NSDictionary *)keyedValues
242 for (NSString *key in keyedValues)
244 [
self setValue:[keyedValues objectForKey:key] forKey:key];
249 #pragma mark Prefix Options
255 #pragma mark Schema and Known Attributes
266 NSMutableSet *
set = [NSMutableSet set];
268 [set addObjectsFromArray:[[
self attributes] allKeys]];
269 return [set allObjects];
273 #pragma mark Persisted
289 #pragma mark Primary Key
295 return ID && [
ID isKindOfClass:[NSNumber class]] ?
ID : nil;
298 - (void)setID:(NSNumber *)ID
304 #pragma mark RESTful Services
307 - (void)saveWithCompletionHandler:(
void (^)(
ARHTTPResponse *response, NSError *error))completionHandler
319 - (void)destroyWithCompletionHandler:(
void (^)(
ARHTTPResponse *response, NSError *error))completionHandler
322 completionHandler(response, error);
326 - (void)existsWithCompletionHandler:(
void (^)(
ARHTTPResponse *response, BOOL exists, NSError *error))completionHandler
329 completionHandler(response, exists, error);
345 NSMutableString *
string = [NSMutableString stringWithFormat:@"%@: %p", NSStringFromClass([
self class]), self];
352 [string appendFormat:@" element:%@", elementName];
357 [string appendFormat:@" collection:%@", collectionName];
360 for (NSString *attributeName in attributes)
362 [string appendFormat:@" %@=%@", attributeName, [attributes objectForKey:attributeName]];
365 return [string copy];