Permalink
Browse files

We don't need something like define_method, we can do it more efficient.

  • Loading branch information...
alloy committed Aug 12, 2012
1 parent 38a1370 commit 8cd7fdd30e5176ddf0dac8e019e3ca0178453701
Showing with 69 additions and 37 deletions.
  1. +20 −10 app/managed_object.rb
  2. +11 −0 ext/MotionDataManagedObjectBase.h
  3. +38 −0 ext/MotionDataManagedObjectBase.m
  4. +0 −10 ext/define_method.h
  5. +0 −17 ext/define_method.m
View
@@ -31,7 +31,7 @@ class Transformable
end
end
- class ManagedObject < NSManagedObject
+ class ManagedObject < MotionDataManagedObjectBase
include CoreTypes
extend Predicate::Builder::Mixin
@@ -67,12 +67,7 @@ def hasOne(name, options = {})
def hasMany(name, options = {})
#puts "#{self.name} has many `#{name}' (#{options.inspect})"
entityDescription.hasMany(name, options)
- klass = self
- defineInstanceMethod(name) do |_self|
- # TODO will/did access
- set = _self.primitiveValueForKey(name.to_s)
- Scope::Relationship.alloc.initWithTarget(set, relationshipName:name, owner:_self, ownerClass:klass)
- end
+ defineRelationshipMethod(name)
end
def property(name, type, options = {})
@@ -98,12 +93,27 @@ def scopes
# method named after the scope.
def scope(name, scope)
scopes[name] = scope
- defineClassMethod(name) { |_self| scope }
+ defineNamedScopeMethod(name)
+ scope
+ end
+
+ # Called from method that's dynamically added from
+ # +[MotionDataManagedObjectBase defineNamedScopeMethod:]
+ def scopeByName(name)
+ scopes[name]
end
end
- def relationship(name)
- Scope::Relationship.alloc.initWithTarget(send(name), relationshipName:name, owner:self, ownerClass:self.class.entityDescription.klass)
+ # Called from method that's dynamically added from
+ # +[MotionDataManagedObjectBase defineRelationshipMethod:]
+ def relationshipByName(name)
+ willAccessValueForKey(name)
+ scope = Scope::Relationship.alloc.initWithTarget(primitiveValueForKey(name),
+ relationshipName:name,
+ owner:self,
+ ownerClass:self.class)
+ didAccessValueForKey(name)
+ scope
end
def writeAttribute(key, value)
@@ -0,0 +1,11 @@
+#import <CoreData/CoreData.h>
+
+@interface MotionDataManagedObjectBase : NSManagedObject
+
++ (id)scopeByName:(NSString *)name;
++ (void)defineNamedScopeMethod:(NSString *)name;
+
+- (id)relationshipByName:(NSString *)name;
++ (void)defineRelationshipMethod:(NSString *)name;
+
+@end
@@ -0,0 +1,38 @@
+#import "MotionDataManagedObjectBase.h"
+#import <objc/runtime.h>
+
+@implementation MotionDataManagedObjectBase
+
++ (id)scopeByName:(NSString *)name;
+{
+ // should be overriden by the subclass
+ printf("Unimplemented\n");
+ abort();
+ return nil;
+}
+
++ (void)defineNamedScopeMethod:(NSString *)name;
+{
+ IMP imp = imp_implementationWithBlock(^id(Class modelClass) {
+ return [modelClass scopeByName:name];
+ });
+ class_addMethod(object_getClass([self class]), NSSelectorFromString(name), imp, "@@:");
+}
+
+- (id)relationshipByName:(NSString *)name;
+{
+ // should be overriden by the subclass
+ printf("Unimplemented\n");
+ abort();
+ return nil;
+}
+
++ (void)defineRelationshipMethod:(NSString *)name;
+{
+ IMP imp = imp_implementationWithBlock(^id(MotionDataManagedObjectBase *entity) {
+ return [entity relationshipByName:name];
+ });
+ class_addMethod([self class], NSSelectorFromString(name), imp, "@@:");
+}
+
+@end
View
@@ -1,10 +0,0 @@
-#import <Foundation/Foundation.h>
-
-typedef id(^rubyMethodBlock)(id);
-
-@interface ClassExt : NSObject
-
-+ (void)defineRubyClassMethod:(rubyMethodBlock)block withSelector:(SEL)selector onClass:(Class)klass;
-+ (void)defineRubyInstanceMethod:(rubyMethodBlock)block withSelector:(SEL)selector onClass:(Class)klass;
-
-@end
View
@@ -1,17 +0,0 @@
-#import "define_method.h"
-#import <objc/runtime.h>
-
-@implementation ClassExt
-
-+ (void)defineRubyClassMethod:(rubyMethodBlock)block withSelector:(SEL)selector onClass:(Class)klass;
-{
- [ClassExt defineRubyInstanceMethod:block withSelector:selector onClass:object_getClass(klass)];
-}
-
-+ (void)defineRubyInstanceMethod:(rubyMethodBlock)block withSelector:(SEL)selector onClass:(Class)klass;
-{
- IMP imp = imp_implementationWithBlock(block);
- class_addMethod(klass, selector, (IMP)imp, "@@:");
-}
-
-@end

0 comments on commit 8cd7fdd

Please sign in to comment.