class MetaSearchMongoid::SearchBuilder

Attributes

options[R]
params[R]
relation[R]

Public Class Methods

new(relation, params, options) click to toggle source
Calls superclass method
# File lib/meta_search_mongoid/search.rb, line 5
def initialize relation, params, options
  super(relation)
  @relation = relation
  @params, @options = params, options
end

Public Instance Methods

base() click to toggle source
# File lib/meta_search_mongoid/search.rb, line 64
def base
  self
end
build() click to toggle source
# File lib/meta_search_mongoid/search.rb, line 11
def build
  params.each_pair do |field_query, value|
    field, query = field_query.to_s.scan(metasearch_regexp).first
    case query.to_sym
    when :equals, :eq
      @relation = relation.where(field => value)
    when :does_not_equal, :ne, :not_eq
      @relation = relation.where(field.to_sym.ne => value)
    when :contains, :like, :matches
      @relation = relation.where(field => /#{value}/)
    when :does_not_contain, :nlike, :not_matches
      @relation = relation.where(field.to_sym.not => /#{value}/)
    when :starts_with, :sw
      @relation = relation.where(field.to_sym => /\A#{Regexp.quote(value)}/)
    when :does_not_start_with, :dnsw
      @relation = relation.where(field.to_sym.not => /\A#{Regexp.quote(value)}/)
    when :ends_with, :ew
      @relation = relation.where(field.to_sym => /#{Regexp.quote(value)}\z/)
    when :does_not_end_with, :dnew
      @relation = relation.where(field.to_sym.not => /#{Regexp.quote(value)}\z/)
    when :greater_than, :gt
      @relation = relation.where(field.to_sym.gt => value)
    when :less_than, :lt
      @relation = relation.where(field.to_sym.lt => value)
    when :greater_than_or_equal_to, :gte, :gteq
      @relation = relation.where(field.to_sym.gte => value)
    when :less_than_or_equal_to, :lte, :lteq
      @relation = relation.where(field.to_sym.lte => value)
    when :in
      @relation = relation.where(field.to_sym.in => Array.wrap(value))
    when :not_in, :ni
      @relation = relation.where(field.to_sym.nin => Array.wrap(value))
    when :is_true
      @relation = relation.where(field => true)
    when :is_false
      @relation = relation.where(field => false)
    when :is_present
      @relation = relation.where(field.to_sym.exists => true)
    when :is_blank
      @relation = relation.where(field.to_sym.exists => false)
    when :is_null
      @relation = relation.where(field => nil)
    when :is_not_null
      @relation = relation.where(field.to_sym.ne => nil)
    else
      raise [field_query, value].inspect
    end
  end
  @relation
end
klass() click to toggle source
# File lib/meta_search_mongoid/search.rb, line 68
def klass
  relation
end
metasearch_regexp() click to toggle source
# File lib/meta_search_mongoid/search.rb, line 88
def metasearch_regexp
  field_names = klass.fields.map(&:second).map(&:name)
  conditions = MetaSearchMongoid::Conditions::CONDITIONS.map {|condition| condition[0...-1]}
  /\A(#{field_names.join('|')})_(#{conditions.join('|')})\z/
end
method_missing(name, *attrs, &block) click to toggle source
# File lib/meta_search_mongoid/search.rb, line 72
def method_missing name, *attrs, &block
  relation.send(name, *attrs, &block)
end
respond_to?(name, include_private = false) click to toggle source
Calls superclass method
# File lib/meta_search_mongoid/search.rb, line 76
def respond_to? name, include_private = false
  name.to_s =~ metasearch_regexp or super
end