Hands-on query construction

The standard way to run a query is through DomainObject.get, which generates the query and runs it immediately, returning the results. However, there are times when you'll want a more fine-grained way to construct a query.

As an example, let's imagine a search form on a online dating website. The code to generate the query will use Query#infer, and then run it via ObjectStore#query.

qry = Lafcadio::Query.infer( User ) { |u|
  u.gender.equals( @search[:gender] )
}
if @search[:min_age]
  qry = qry.and { |u| u.age.gte( @search[:min_age] }
end
if @search[:max_age]
  qry = qry.and { |u| u.age.lte( @search[:max_age] }
end
if @search[:require_photos]
  qry = qry.and { |u| u.num_photos.gte( 1 ) }
end
matches = Lafcadio::ObjectStore.get_object_store.query( qry )