From 7167f973efc87a541d3c6b5ee4ffcfb2510abc6d Mon Sep 17 00:00:00 2001 From: Dan Kubb Date: Mon, 24 May 2010 22:50:00 -0700 Subject: [PATCH] Added Model#fetch * Added specs for #fetch to finder interface --- lib/dm-core/model.rb | 4 ++ spec/public/shared/finder_shared_spec.rb | 47 ++++++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/lib/dm-core/model.rb b/lib/dm-core/model.rb index 37d055abf..01220c143 100644 --- a/lib/dm-core/model.rb +++ b/lib/dm-core/model.rb @@ -305,6 +305,10 @@ def at(*args) all.at(*args) end + def fetch(*args, &block) + all.fetch(*args, &block) + end + def reverse all.reverse end diff --git a/spec/public/shared/finder_shared_spec.rb b/spec/public/shared/finder_shared_spec.rb index ed16d63af..a23116ecb 100644 --- a/spec/public/shared/finder_shared_spec.rb +++ b/spec/public/shared/finder_shared_spec.rb @@ -880,6 +880,53 @@ end end + it { @articles.should respond_to(:fetch) } + + describe '#fetch' do + subject { @articles.fetch(*args, &block) } + + let(:block) { nil } + + context 'with a valid index and no default' do + let(:args) { [ 0 ] } + + before do + @copy = @articles.kind_of?(Class) ? @articles : @articles.dup + @copy.to_a + end + + should_not_be_a_kicker + + it { should be_kind_of(DataMapper::Resource) } + + it { should == @copy.entries.fetch(*args) } + end + + context 'with an invalid index and no default' do + let(:args) { [ 42 ] } + + it { method(:subject).should raise_error(IndexError) } + end + + context 'with an invalid index and a default' do + let(:default) { mock('Default') } + let(:args) { [ 42, default ] } + + it { should equal(default) } + end + + context 'with an invalid index and a block default' do + let(:yields) { [] } + let(:default) { mock('Default') } + let(:block) { lambda { |index| yields << index; default } } + let(:args) { [ 42 ] } + + it { should equal(default) } + + it { method(:subject).should change { yields.dup }.from([]).to([ 42 ]) } + end + end + it { @articles.should respond_to(:first) } describe '#first' do