2 // HMSlotItemShortNameTransformer.m
5 // Created by Hori,Masaki on 2015/10/23.
6 // Copyright (c) 2015年 Hori,Masaki. All rights reserved.
9 #import "HMSlotItemShortNameTransformer.h"
11 #import "HMServerDataStore.h"
13 #import "HMKCSlotItemObject+Extensions.h"
14 #import "HMKCMasterSlotItemObject.h"
16 static NSDictionary *slotItemShortName = nil;
18 @implementation HMSlotItemShortNameTransformer
21 static dispatch_once_t onceToken;
22 dispatch_once(&onceToken, ^{
23 [NSValueTransformer setValueTransformer:[self new] forName:@"HMSlotItemShortNameTransformer"];
25 NSBundle *mainBundle = [NSBundle mainBundle];
26 NSString *path = [mainBundle pathForResource:@"SlotItemShortName" ofType:@"plist"];
28 NSLog(@"Could not find SlotItemShortName.plist");
32 id dict = [NSDictionary dictionaryWithContentsOfFile:path];
34 NSLog(@"SlotItemShortName.plist is not dictionay.");
38 slotItemShortName = dict;
42 + (Class)transformedValueClass
44 return [NSString class];
46 + (BOOL)allowsReverseTransformation
51 - (id)transformedValue:(id)value
53 if(![value isKindOfClass:[NSNumber class]]) return nil;
54 NSInteger slotItemID = [value integerValue];
55 if(slotItemID == -1) return nil;
56 if(slotItemID == 0) return nil;
58 HMServerDataStore *store = [HMServerDataStore defaultManager];
61 NSArray *array = [store objectsWithEntityName:@"SlotItem"
63 predicateFormat:@"id = %@", value];
64 if([array count] == 0) {
65 NSLog(@"SlotItem is invalid.");
69 HMKCSlotItemObject *slotItem = array[0];
70 NSString *mstIDString = [slotItem.master_slotItem.id stringValue];
71 NSString *shortName = slotItemShortName[mstIDString];
72 if(shortName) return shortName;
74 return [slotItem.name copy];