Permalink
Browse files

Adding ability to get positional operator for a document

  • Loading branch information...
1 parent 2aec413 commit 09c1b85434264caace223d60213fa375f3836307 @durran durran committed Mar 24, 2010
Showing with 48 additions and 2 deletions.
  1. +11 −2 lib/mongoid/paths.rb
  2. +37 −0 spec/unit/mongoid/paths_spec.rb
View
@@ -3,8 +3,8 @@ module Mongoid #:nodoc:
module Paths #:nodoc:
extend ActiveSupport::Concern
included do
- cattr_accessor :_path
- delegate :_path, :to => "self.class"
+ cattr_accessor :_path, :_position
+ delegate :_path, :_position, :to => "self.class"
end
module InstanceMethods
# Return the path to this +Document+ in JSON notation, used for atomic
@@ -19,6 +19,15 @@ def path
end
end
+ # Returns the positional operator of this document for modification.
+ #
+ # Example:
+ #
+ # <tt>address.position</tt>
+ def position
+ self._position ||= (path.blank? ? "" : "#{path}.$")
+ end
+
# Return the selector for this document to be matched exactly for use
# with MongoDB's $ operator.
#
@@ -84,4 +84,41 @@
end
end
end
+
+ describe "#position" do
+
+ context "when the document is a parent" do
+
+ it "returns an empty string" do
+ person.position.should == ""
+ end
+ end
+
+ context "when the document is embedded" do
+
+ before do
+ person.addresses << address
+ end
+
+ it "returns the path plus $" do
+ address.position.should == "addresses.$"
+ end
+ end
+
+ context "when document embedded multiple levels" do
+
+ before do
+ address.locations << location
+ person.addresses << address
+ end
+
+ it "returns the path plus $" do
+ location.position.should == "addresses.locations.$"
+ end
+
+ it "sets the matcher class instance var" do
+ Location._position.should == "addresses.locations.$"
+ end
+ end
+ end
end

0 comments on commit 09c1b85

Please sign in to comment.