Skip to content

Commit

Permalink
Enhance "where" condition for memory adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
jodosha committed Apr 14, 2015
1 parent 5a98026 commit 053f814
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
14 changes: 12 additions & 2 deletions lib/lotus/model/adapters/memory/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,17 @@ def all
# .where(framework: 'lotus')
def where(condition)
column, value = _expand_condition(condition)
conditions.push([:where, Proc.new{ find_all{|r| r.fetch(column, nil) == value} }])
conditions.push([:where, Proc.new{
find_all{|r|
case value
when Array,Set,Range
value.include?(r.fetch(column, nil))
else
r.fetch(column, nil) == value
end
}
}])

self
end

Expand Down Expand Up @@ -523,7 +533,7 @@ def _all_with_present_column(column)
end

def _expand_condition(condition)
Array(condition).flatten
Array(condition).flatten(1)
end
end
end
Expand Down
33 changes: 33 additions & 0 deletions test/model/adapters/memory_adapter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,39 @@ class TestDeviceRepository
result = @adapter.query(collection, &query).all
result.must_equal [@user3]
end

it 'accepts an array as condition' do
names = [@user1.name, @user2.name]

query = Proc.new {
where(name: names)
}

result = @adapter.query(collection, &query).all
result.must_equal [@user1, @user2]
end

it 'accepts a set as condition' do
names = Set.new([@user1.name, @user2.name])

query = Proc.new {
where(name: names)
}

result = @adapter.query(collection, &query).all
result.must_equal [@user1, @user2]
end

it 'accepts a range as condition' do
ages = @user3.age..@user2.age

query = Proc.new {
where(age: ages)
}

result = @adapter.query(collection, &query).all
result.must_equal [@user2, @user3]
end
end
end

Expand Down

0 comments on commit 053f814

Please sign in to comment.