Skip to content
This repository has been archived by the owner on Apr 17, 2018. It is now read-only.

Commit

Permalink
Updated InMemoryAdapter to search by more criteria
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Kubb committed Oct 8, 2008
1 parent ecac20d commit 41791bd
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 14 deletions.
24 changes: 21 additions & 3 deletions lib/dm-core/adapters/in_memory_adapter.rb
Expand Up @@ -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
Expand Down
66 changes: 55 additions & 11 deletions spec/unit/adapters/in_memory_adapter_spec.rb
Expand Up @@ -6,6 +6,7 @@

class Heffalump
include DataMapper::Resource

def self.default_repository_name
:inmem
end
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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

0 comments on commit 41791bd

Please sign in to comment.