Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add named scopes to MotionData::ManagedObject.

  • Loading branch information...
commit e8027503b6cfbbfeb5de297fae2c4b2fafa7bdb8 1 parent 393d5b6
@alloy authored
Showing with 41 additions and 13 deletions.
  1. +26 −10 app/managed_object.rb
  2. +7 −1 app/scope.rb
  3. +8 −2 spec/managed_object_spec.rb
View
36 app/managed_object.rb
@@ -53,18 +53,34 @@ def property(name, type, options = {})
# Finders
def all
- scope = NSFetchRequest.new
- scope.entity = entityDescription
- # Encueing the fetch like this ensures other transactions on the context
- # have been run.
- #
- # This is what MagicalRecord does too.
- Context.current.perform do
- Context.current.executeFetchRequest(scope, error:nil)
- # TODO handleError(error) unless results
- end
+ Scope::Model.alloc.initWithTarget(self)
+ end
+
+ def where(conditions)
+ all.where(conditions)
+ end
+
+ # TODO copy to subclasses of abstract models
+ def scopes
+ @scopes ||= {}
end
+ # Adds a named scope to the class.
+ def scope(name, scope)
+ scopes[name] = scope
+ end
+
+ # Returns a scope that matches the method name if one exists.
+ #
+ # TODO Until RubyMotion allows the use of define_method, this is the best
+ # we can do.
+ def method_missing(method, *args, &block)
+ if scope = scopes[method]
+ scope
+ else
+ super
+ end
+ end
end
def writeAttribute(key, value)
View
8 app/scope.rb
@@ -62,7 +62,13 @@ def each(&block)
def array
raise "Not implemented"
end
- alias_method :to_a, :array
+
+ # TODO RM bug? aliased methods in subclasses don't call overriden version of aliased method.
+ #alias_method :to_a, :array
+
+ def to_a
+ array
+ end
def set
raise "Not implemented."
View
10 spec/managed_object_spec.rb
@@ -36,12 +36,18 @@ module MotionData
end
end
- describe "finders" do
+ describe "scopes" do
it "returns all entities of a managed object in the current context" do
- Author.all.should == []
+ Author.all.to_a.should == []
Author.new(:name => "Edgar Allan Poe")
Author.all.map(&:name).should == ["Edgar Allan Poe"]
end
+
+ it "defines a named scope" do
+ Author.scope(:edgars, Author.where(:name => 'edgar'))
+ Author.edgars.target.should == Author
+ Author.edgars.predicate.predicateFormat.should == 'name == "edgar"'
+ end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.