Skip to content

Instantly share code, notes, and snippets.

@leandrob13
Created September 25, 2015 13:36
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save leandrob13/09c6bfb333e106cf5931 to your computer and use it in GitHub Desktop.
Save leandrob13/09c6bfb333e106cf5931 to your computer and use it in GitHub Desktop.
Based on MaybeFilter (https://gist.github.com/cvogt/9193220), it uses implicit class to add the methods to the Query objects. FilteredBy works for filtering lists and it returns the query if no filters were applied. FoundBy works for finding a single register, if no filter defined it returns None.
implicit class OptionFilter[ X, Y ]( query: Query[ X, Y, Seq ] ) {
def filteredBy[ T ]( op: Option[ T ] )( f: ( X, T ) => Column[ Option[ Boolean ] ] ): Query[ X, Y, Seq ] = {
op map { o => query.filter( f( _, o ) ) } getOrElse { query }
}
def foundBy[ T ]( op: Option[ T ] )( f: ( X, T ) => Column[ Option[ Boolean ] ] ): Query[ X, Y, Seq ] = {
op map { o => query.filter( f( _, o ) ) } getOrElse { query.take( 0 ) }
}
}
@rossanthony
Copy link

Do you know is this will work with Slick 3.x and do you have any example of putting it into practice?

@leandrob13
Copy link
Author

Yes, I have been using it but I have made some changes to it. If you are still interested I can share it with you

@leandrob13
Copy link
Author

Here is the new one: https://gist.github.com/leandrob13/a50f61db5a81d36dd5d35820fd9ca67e
I promise I will show you how to use it.

@leandrob13
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment