Skip to content

Commit

Permalink
Adding ability to get positional operator for a document
Browse files Browse the repository at this point in the history
  • Loading branch information
durran committed Mar 24, 2010
1 parent 2aec413 commit 09c1b85
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
13 changes: 11 additions & 2 deletions lib/mongoid/paths.rb
Expand Up @@ -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
Expand All @@ -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.
#
Expand Down
37 changes: 37 additions & 0 deletions spec/unit/mongoid/paths_spec.rb
Expand Up @@ -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.