Skip to content
Browse files

Forward messages from the relationship set to the scope with method_m…

…issing.
  • Loading branch information...
1 parent a4a4ea2 commit e023c10e05613a3c80046efc9c57713b03385894 @alloy committed Aug 13, 2012
Showing with 28 additions and 40 deletions.
  1. +6 −1 app/managed_object.rb
  2. +2 −2 app/schema.rb
  3. +18 −35 app/scope.rb
  4. +2 −2 spec/managed_object_spec.rb
View
7 app/managed_object.rb
@@ -104,6 +104,7 @@ def scopes
# Adds a named scope to the class and makes it available as a class
# method named after the scope.
def scope(name, scope)
+ name = name.to_sym
scopes[name] = scope
defineNamedScopeMethod(name)
scope
@@ -112,7 +113,7 @@ def scope(name, scope)
# Called from method that's dynamically added from
# +[MotionDataManagedObjectBase defineNamedScopeMethod:]
def scopeByName(name)
- scopes[name]
+ scopes[name.to_sym]
end
end
@@ -133,6 +134,10 @@ def writeAttribute(key, value)
setPrimitiveValue(value, forKey:key)
didChangeValueForKey(key)
end
+
+ def inspect
+ description
+ end
end
end
View
4 app/schema.rb
@@ -21,8 +21,8 @@ def hasMany(name, options = {})
relationshipDescriptionWithOptions({ :name => name, :maxCount => -1 }.merge(options))
end
- def klass
- @klass ||= Object.const_get(name)
+ def modelClass
+ @modelClass ||= Object.const_get(name)
end
private
View
53 app/scope.rb
@@ -140,46 +140,25 @@ def setByApplyingConditionsToSet(set)
class Scope
class Relationship < Scope::Set
# A Core Data relationship set is extended with this module to provide
- # scoping.
- #
- # TODO declare these delegating methods in a dynamic way which ensures we
- # always forward all the methods from the Scope classes.
+ # scoping by forwarding messages to a Scope::Relationship instance which
+ # wraps the set.
module SetExt
attr_accessor :__scope__
- def new(properties = nil)
- @__scope__.new(properties)
- end
-
- def where(conditions, *formatArguments)
- @__scope__.where(conditions, *formatArguments)
- end
-
- def sortBy(keyPathOrSortDescriptor)
- @__scope__.sortBy(keyPathOrSortDescriptor)
- end
-
- def sortBy(keyPath, ascending:ascending)
- @__scope__.sortBy(keyPath, ascending:ascending)
- end
-
- def each(&block)
- @__scope__.each(&block)
- end
-
- def array
- @__scope__.array
+ def set
+ self
end
- # TODO RM bug? aliased methods in subclasses don't call overriden version of aliased method.
- #alias_method :to_a, :array
-
- def to_a
- @__scope__.array
+ def respond_to?(method)
+ @__scope__.respond_to?(method) || super
end
- def set
- self
+ def method_missing(method, *args, &block)
+ if respond_to?(method)
+ @__scope__.send(method, *args, &block)
+ else
+ super
+ end
end
end
@@ -228,8 +207,12 @@ def fetchRequest
request
end
+ def respond_to?(method)
+ !targetClass.scopeByName(method).nil? || super
+ end
+
def method_missing(method, *args, &block)
- if scope = targetClass.scopes[method]
+ if scope = targetClass.scopeByName(method)
where(scope)
else
super
@@ -247,7 +230,7 @@ def targetEntityDescription
end
def targetClass
- targetEntityDescription.klass
+ targetEntityDescription.modelClass
end
def inverseRelationshipName
View
4 spec/managed_object_spec.rb
@@ -73,8 +73,8 @@ module MotionData
author.articles.to_a.should == []
article1 = author.articles.new(:title => 'article1')
- #article2 = author.articles.new(:title => 'article2')
- #author.articles.withTitles.to_a.should == [article1, article2]
+ article2 = author.articles.new(:title => 'article2')
+ author.articles.withTitle.to_a.should == [article2, article1]
end
end
end

0 comments on commit e023c10

Please sign in to comment.
Something went wrong with that request. Please try again.