Skip to content

Commit

Permalink
Added Meta#last as complement to Meta#first.
Browse files Browse the repository at this point in the history
  • Loading branch information
ndarilek committed Apr 23, 2008
1 parent 9e8d771 commit 5e354b3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
8 changes: 8 additions & 0 deletions lib/strokedb/document/meta.rb
Expand Up @@ -198,6 +198,14 @@ def first(args = {})
result.respond_to?(:first) ? result.first : result
end

#
# Finds the last document matching the given criteria.
#
def last(args = {})
result = find(args)
result.respond_to?(:last) ? result.last : result
end

#
# Similar to +find+, but a creates document with appropriate slot values if
# not found.
Expand Down
19 changes: 18 additions & 1 deletion spec/lib/strokedb/document/meta_spec.rb
Expand Up @@ -84,11 +84,28 @@
SomeName.first.should == SomeName.find.first
end

it "correctly handles finding via UUID on call to Meta#first" do
it "correctly handles finding via UUID on call to #first" do
a = SomeName.create!(:slot1 => 5)
SomeName.first(a.uuid).should == a
end

it "should return last found document matching given criteria on call to #last" do
a = SomeName.create!(:slot1 => 1)
b = SomeName.create!(:slot1 => 1)
SomeName.last(:slot1 => 1).should == SomeName.find(:slot1 => 1).last
end

it "should return last document if no args are passed to #last" do
a = SomeName.create!(:slot1 => 1)
b = SomeName.create!(:slot1 => 2)
SomeName.last.should == SomeName.find.last
end

it "correctly handles finding via UUID on call to #last" do
a = SomeName.create!(:slot1 => 5)
SomeName.last(a.uuid).should == a
end

it "should raise ArgumentError unless args size is 1 or 2" do
a = SomeName.create!(:slot1 => 1, :slot2 => 2)
lambda { SomeName.find("foo","bar","foobar") }.should raise_error(ArgumentError)
Expand Down

0 comments on commit 5e354b3

Please sign in to comment.