Grails:groovy subcollection

时间:2012-02-04 21:48:21

标签: grails collections groovy closures currying

拥有以下域类:

class Message{
}

希望获得id超过offset的所有邮件:

def offset = params.offset
def messages = Message.list()
def subMessages = messages.findAll({offset, it ->
    it.id > offset
}.curry(offset))

怎么了?


更新:我知道Message.list()可以Message.list(params)执行,但现在不重要了。

1 个答案:

答案 0 :(得分:4)

使用地图的示例

def params = [offset: 2]
def messages = [[id: 1], [id: 2], [id: 3], [id: 4], [id: 5]]
def subMessages = messages.findAll({ it.id > params.offset })
assert subMessages == [[id: 3], [id: 4], [id: 5]]

使用Grails域类进行集成测试

(1..5).each { new Message(text: "${it}").save(failOnError: true) }
def params = [offset: "2"]
def offset = Integer.parseInt(params.offset)
def messages = Message.list()
def subMessages = messages.findAll({ it.id > offset })
assert subMessages == Message.findAllByIdGreaterThan(offset)