class Gizmo::Search

Public Instance Methods

call(criteria, params={}) { |criteria, params| ... } click to toggle source

Search for items.

@param [Mongoid::Criteria] criteria the criteria to use for searching the database @param [Hash] params parameters used for searching @option params [String] :term the field to search @option params [String] :query the value used for searching @option params [Integer] :num_items the maximum number of items to return, default 10 @option params [Boolean] :fuzzy true if fuzzy matching should be used, false otherwise, default to false @param [Proc] if a block is given, it will be passed the criteria and params. It should

return the result/data. note: the block will be used to perform the search.

@return [Gizmo::Response]

# File lib/gizmo/search.rb, line 16
def call(criteria, params={})
  response = create_response
  if block_given?
    response.data = yield criteria, params
  else
    term = params[:term]
    query = params[:query]
    num_items = (params[:num_items] || 10).to_i
    num_items = 10 if num_items > 100 || num_items < 1

    fuzzy = !!((params[:fuzzy] || "") =~ /true/i)
    if fuzzy
      query = /#{query}/i
    end

    response.data = criteria.where(term => query).limit(num_items)
  end
  response
end