Skip to content

Commit

Permalink
implemented lambda expression for exclude and or
Browse files Browse the repository at this point in the history
  • Loading branch information
Coen Wessels authored and Coen Wessels committed Jul 29, 2014
1 parent 3fe74fc commit 6186529
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 11 deletions.
15 changes: 5 additions & 10 deletions lib/lotus/model/adapters/sql/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,8 @@ def all
# .where(framework: 'lotus')
#
# # => SELECT * FROM `projects` WHERE (`language` = 'ruby') AND (`framework` = 'lotus')
#
# @example Expressions
#
# query.where{ publish_at > function(:DATE, Date.today) }
#
# # => SELECT * FROM `articles` WHERE (`publish_at` > DATE('2014-07-29'))
def where(condition=nil, &blk)
condition or blk or raise ArgumentError.new("You need to specify an condition.")
condition = blk unless condition
condition = (condition or blk or raise ArgumentError.new('You need to specify an condition.'))
conditions.push([:where, condition])
self
end
Expand Down Expand Up @@ -157,7 +150,8 @@ def where(condition=nil, &blk)
# query.where(country: 'italy').or(year: 1900..1982)
#
# # => SELECT * FROM `people` WHERE ((`country` = 'italy') OR ((`year` >= 1900) AND (`year` <= 1982)))
def or(condition)
def or(condition=nil, &blk)
condition = (condition or blk or raise ArgumentError.new('You need to specify an condition.'))
conditions.push([:or, condition])
self
end
Expand Down Expand Up @@ -198,7 +192,8 @@ def or(condition)
# .exclude(company: 'enterprise')
#
# # => SELECT * FROM `projects` WHERE (`language` != 'java') AND (`company` != 'enterprise')
def exclude(condition)
def exclude(condition=nil, &blk)
condition = (condition or blk or raise ArgumentError.new('You need to specify an condition.'))
conditions.push([:exclude, condition])
self
end
Expand Down
42 changes: 41 additions & 1 deletion test/model/adapters/sql_adapter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@
result.must_equal [user1]
end

it 'can use code block(sequel block) to describe where conditions' do
it 'can use lambda to describe where conditions' do
query = Proc.new {
where{ age > 31 }
}
Expand Down Expand Up @@ -387,6 +387,24 @@
result = @adapter.query(collection, &query).all
result.must_equal [user3]
end

it 'can use lambda to describe exclude conditions' do
query = Proc.new {
exclude{ age > 31 }
}

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

it 'raises an error if you dont specify condition or block' do
-> {
query = Proc.new {
exclude()
}
@adapter.query(collection, &query).all
}.must_raise(ArgumentError)
end
end
end

Expand Down Expand Up @@ -418,6 +436,28 @@
result = @adapter.query(collection, &query).all
result.must_equal [user1, user2]
end

it 'can use lambda to describe exclude conditions' do
name1 = user1.name

query = Proc.new {
where(name: name1).or{ age < 32 }
}

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

it 'raises an error if you dont specify condition or block' do
-> {
name1 = user1.name

query = Proc.new {
where(name: name1).or()
}
@adapter.query(collection, &query).all
}.must_raise(ArgumentError)
end
end
end

Expand Down

0 comments on commit 6186529

Please sign in to comment.