From 41791bd2d6c4cce9d57d7ecf8d38044a83c94d7f Mon Sep 17 00:00:00 2001 From: Dan Kubb Date: Tue, 7 Oct 2008 22:20:15 -0700 Subject: [PATCH] Updated InMemoryAdapter to search by more criteria --- lib/dm-core/adapters/in_memory_adapter.rb | 24 ++++++- spec/unit/adapters/in_memory_adapter_spec.rb | 66 ++++++++++++++++---- 2 files changed, 76 insertions(+), 14 deletions(-) diff --git a/lib/dm-core/adapters/in_memory_adapter.rb b/lib/dm-core/adapters/in_memory_adapter.rb index 6f5ea627..b1f82488 100644 --- a/lib/dm-core/adapters/in_memory_adapter.rb +++ b/lib/dm-core/adapters/in_memory_adapter.rb @@ -50,19 +50,37 @@ def read(query, set, many = true) result = @records[model].send(match_with) do |resource| conditions.all? do |tuple| operator, property, bind_value = *tuple + value = property.get!(resource) + case operator - when :eql then value == bind_value - # TODO: Things other than :eql + when :eql, :in then equality_comparison(bind_value, value) + when :not then !equality_comparison(bind_value, value) + when :like then Regexp.new(bind_value) =~ value + when :gt then !value.nil? && value > bind_value + when :gte then !value.nil? && value >= bind_value + when :lt then !value.nil? && value < bind_value + when :lte then !value.nil? && value <= bind_value + else raise "Invalid query operator: #{operator.inspect}" end end end + return result unless many + # TODO Sort # TODO Limit - many ? set.replace(result) : result + set.replace(result) + end + + def equality_comparison(bind_value, value) + case bind_value + when Array, Range then bind_value.include?(value) + when NilClass then value.nil? + else bind_value == value + end end end end diff --git a/spec/unit/adapters/in_memory_adapter_spec.rb b/spec/unit/adapters/in_memory_adapter_spec.rb index 73374dce..7961d889 100644 --- a/spec/unit/adapters/in_memory_adapter_spec.rb +++ b/spec/unit/adapters/in_memory_adapter_spec.rb @@ -6,6 +6,7 @@ class Heffalump include DataMapper::Resource + def self.default_repository_name :inmem end @@ -15,14 +16,9 @@ def self.default_repository_name property :striped, Boolean end - @heff1 = Heffalump.new(:color => 'Black', - :num_spots => 0, - :striped => true) - @heff1.save - @heff2 = Heffalump.new(:color => 'Brown', - :num_spots => 25, - :striped => false) - @heff2.save + @heff1 = Heffalump.create(:color => 'Black', :num_spots => 0, :striped => true) + @heff2 = Heffalump.create(:color => 'Brown', :num_spots => 25, :striped => false) + @heff3 = Heffalump.create(:color => 'Blue', :num_spots => nil, :striped => false) end it 'should successfully save an object' do @@ -34,13 +30,61 @@ def self.default_repository_name end it 'should be able to get all the objects' do - Heffalump.all.should == [@heff1, @heff2] + Heffalump.all.should == [@heff1, @heff2, @heff3] end - it 'should be able to search for an object' do + it 'should be able to search for objects with equal value' do Heffalump.all(:striped => true).should == [@heff1] end + it 'should be able to search for objects included in an array of values' do + Heffalump.all(:num_spots => [ 25, 50, 75, 100 ]).should == [@heff2] + end + + it 'should be able to search for objects included in a range of values' do + Heffalump.all(:num_spots => 25..100).should == [@heff2] + end + + it 'should be able to search for objects with nil value' do + Heffalump.all(:num_spots => nil).should == [@heff3] + end + + it 'should be able to search for objects with not equal value' do + Heffalump.all(:striped.not => true).should == [@heff2, @heff3] + end + + it 'should be able to search for objects not included in an array of values' do + Heffalump.all(:num_spots.not => [ 25, 50, 75, 100 ]).should == [@heff1, @heff3] + end + + it 'should be able to search for objects not included in a range of values' do + Heffalump.all(:num_spots.not => 25..100).should == [@heff1, @heff3] + end + + it 'should be able to search for objects with not nil value' do + Heffalump.all(:num_spots.not => nil).should == [@heff1, @heff2] + end + + it 'should be able to search for objects that match value' do + Heffalump.all(:color.like => 'Bl').should == [@heff1, @heff3] + end + + it 'should be able to search for objects with value greater than' do + Heffalump.all(:num_spots.gt => 0).should == [@heff2] + end + + it 'should be able to search for objects with value greater than or equal to' do + Heffalump.all(:num_spots.gte => 0).should == [@heff1, @heff2] + end + + it 'should be able to search for objects with value less than' do + Heffalump.all(:num_spots.lt => 1).should == [@heff1] + end + + it 'should be able to search for objects with value less than or equal to' do + Heffalump.all(:num_spots.lte => 0).should == [@heff1] + end + it 'should be able to update an object' do @heff1.num_spots = 10 @heff1.save @@ -49,6 +93,6 @@ def self.default_repository_name it 'should be able to destroy an object' do @heff1.destroy - Heffalump.all.size.should == 1 + Heffalump.all.size.should == 2 end end