https://gist.github.com/1bc4548b226d331b8384
ruby 相关的书看了一些,始终没派上用场,今天突发奇想,可以用来生成 iphone model 部分的冗余代码。
本来可以用 oc 的反射机制来做的,但似乎用 Ruby 生成重复的 oc 代码更容易些。
代码看起来比较土,但是用起来还成。
DSL demo:
property name:"authorAvatar"
property name:"comment"
property name:"postId", type:"NSNumber"
property name:"postType"
property name:"postTypeText"
property name:"authorName"
property name:"action"
property name:"from_user_id", type:"NSNumber"
property name:"createTime", type:"NSDate"
property name:"postDesc"
property name:"text"
property name:"essayId", type:"NSNumber"
property name:"editorId", type:"NSNumber"
property name:"editorAvatar"
property name:"editorName"
property name:"topicPostId", type:"NSNumber"
输出:
//property declare
@property (nonatomic, strong) NSString * authorAvatar;
@property (nonatomic, strong) NSString * comment;
@property (nonatomic, strong) NSNumber * postId;
@property (nonatomic, strong) NSString * postType;
@property (nonatomic, strong) NSString * postTypeText;
@property (nonatomic, strong) NSString * authorName;
@property (nonatomic, strong) NSString * action;
@property (nonatomic, strong) NSNumber * from_user_id;
@property (nonatomic, strong) NSDate * createTime;
@property (nonatomic, strong) NSString * postDesc;
@property (nonatomic, strong) NSString * text;
@property (nonatomic, strong) NSNumber * essayId;
@property (nonatomic, strong) NSNumber * editorId;
@property (nonatomic, strong) NSString * editorAvatar;
@property (nonatomic, strong) NSString * editorName;
@property (nonatomic, strong) NSNumber * topicPostId;
// auto generate method
- (id) initWithCoder:(NSCoder *)aDecoder{
self = [super init]; if (self == nil) return nil;
self.authorAvatar = [aDecoder decodeObjectForKey:NSStringFromSelector(@selector(authorAvatar))];
self.comment = [aDecoder decodeObjectForKey:NSStringFromSelector(@selector(comment))];
self.postId = [aDecoder decodeObjectForKey:NSStringFromSelector(@selector(postId))];
self.postType = [aDecoder decodeObjectForKey:NSStringFromSelector(@selector(postType))];
self.postTypeText = [aDecoder decodeObjectForKey:NSStringFromSelector(@selector(postTypeText))];
self.authorName = [aDecoder decodeObjectForKey:NSStringFromSelector(@selector(authorName))];
self.action = [aDecoder decodeObjectForKey:NSStringFromSelector(@selector(action))];
self.from_user_id = [aDecoder decodeObjectForKey:NSStringFromSelector(@selector(from_user_id))];
self.createTime = [aDecoder decodeObjectForKey:NSStringFromSelector(@selector(createTime))];
self.postDesc = [aDecoder decodeObjectForKey:NSStringFromSelector(@selector(postDesc))];
self.text = [aDecoder decodeObjectForKey:NSStringFromSelector(@selector(text))];
self.essayId = [aDecoder decodeObjectForKey:NSStringFromSelector(@selector(essayId))];
self.editorId = [aDecoder decodeObjectForKey:NSStringFromSelector(@selector(editorId))];
self.editorAvatar = [aDecoder decodeObjectForKey:NSStringFromSelector(@selector(editorAvatar))];
self.editorName = [aDecoder decodeObjectForKey:NSStringFromSelector(@selector(editorName))];
self.topicPostId = [aDecoder decodeObjectForKey:NSStringFromSelector(@selector(topicPostId))];
return self;
}
- (void) encodeWithCoder:(NSCoder *)aCoder{
[aCoder encodeObject:self.authorAvatar forKey:NSStringFromSelector(@selector(authorAvatar))];
[aCoder encodeObject:self.comment forKey:NSStringFromSelector(@selector(comment))];
[aCoder encodeObject:self.postId forKey:NSStringFromSelector(@selector(postId))];
[aCoder encodeObject:self.postType forKey:NSStringFromSelector(@selector(postType))];
[aCoder encodeObject:self.postTypeText forKey:NSStringFromSelector(@selector(postTypeText))];
[aCoder encodeObject:self.authorName forKey:NSStringFromSelector(@selector(authorName))];
[aCoder encodeObject:self.action forKey:NSStringFromSelector(@selector(action))];
[aCoder encodeObject:self.from_user_id forKey:NSStringFromSelector(@selector(from_user_id))];
[aCoder encodeObject:self.createTime forKey:NSStringFromSelector(@selector(createTime))];
[aCoder encodeObject:self.postDesc forKey:NSStringFromSelector(@selector(postDesc))];
[aCoder encodeObject:self.text forKey:NSStringFromSelector(@selector(text))];
[aCoder encodeObject:self.essayId forKey:NSStringFromSelector(@selector(essayId))];
[aCoder encodeObject:self.editorId forKey:NSStringFromSelector(@selector(editorId))];
[aCoder encodeObject:self.editorAvatar forKey:NSStringFromSelector(@selector(editorAvatar))];
[aCoder encodeObject:self.editorName forKey:NSStringFromSelector(@selector(editorName))];
[aCoder encodeObject:self.topicPostId forKey:NSStringFromSelector(@selector(topicPostId))];
}
- (NSString *) description{
NSString *superDesc = [super description];
NSDictionary *info = @{
NSStringFromSelector(@selector(authorAvatar)): NIL2NULL(self.authorAvatar),
NSStringFromSelector(@selector(comment)): NIL2NULL(self.comment),
NSStringFromSelector(@selector(postId)): NIL2NULL(self.postId),
NSStringFromSelector(@selector(postType)): NIL2NULL(self.postType),
NSStringFromSelector(@selector(postTypeText)): NIL2NULL(self.postTypeText),
NSStringFromSelector(@selector(authorName)): NIL2NULL(self.authorName),
NSStringFromSelector(@selector(action)): NIL2NULL(self.action),
NSStringFromSelector(@selector(from_user_id)): NIL2NULL(self.from_user_id),
NSStringFromSelector(@selector(createTime)): NIL2NULL(self.createTime),
NSStringFromSelector(@selector(postDesc)): NIL2NULL(self.postDesc),
NSStringFromSelector(@selector(text)): NIL2NULL(self.text),
NSStringFromSelector(@selector(essayId)): NIL2NULL(self.essayId),
NSStringFromSelector(@selector(editorId)): NIL2NULL(self.editorId),
NSStringFromSelector(@selector(editorAvatar)): NIL2NULL(self.editorAvatar),
NSStringFromSelector(@selector(editorName)): NIL2NULL(self.editorName),
NSStringFromSelector(@selector(topicPostId)): NIL2NULL(self.topicPostId),
};
return [NSString stringWithFormat:@"%@\n%@",
superDesc,
info];
}
ruby 未入门,坛子里的兄弟给点指导意见~