Class: MetaSearch::Searches::MongoidSearchBuilder

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/meta_search/searches/mongoid.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(relation, params, options) ⇒ MongoidSearchBuilder

Returns a new instance of MongoidSearchBuilder.



10
11
12
13
14
# File 'lib/meta_search/searches/mongoid.rb', line 10

def initialize relation, params, options
  super(relation)
  @relation = relation
  @params, @options = params, options
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



77
78
79
# File 'lib/meta_search/searches/mongoid.rb', line 77

def method_missing name, *attrs, &block
  relation.send(name, *attrs, &block)
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



67
68
69
# File 'lib/meta_search/searches/mongoid.rb', line 67

def options
  @options
end

#paramsObject (readonly)

Returns the value of attribute params.



67
68
69
# File 'lib/meta_search/searches/mongoid.rb', line 67

def params
  @params
end

#relationObject (readonly)

Returns the value of attribute relation.



67
68
69
# File 'lib/meta_search/searches/mongoid.rb', line 67

def relation
  @relation
end

Instance Method Details

#baseObject



69
70
71
# File 'lib/meta_search/searches/mongoid.rb', line 69

def base
  self
end

#buildObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/meta_search/searches/mongoid.rb', line 16

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}/i)
    when :does_not_contain, :nlike, :not_matches
      @relation = relation.where(field.to_sym.not => /#{value}/i)
    when :starts_with, :sw
      @relation = relation.where(field.to_sym => /\A#{Regexp.quote(value)}/i)
    when :does_not_start_with, :dnsw
      @relation = relation.where(field.to_sym.not => /\A#{Regexp.quote(value)}/i)
    when :ends_with, :ew
      @relation = relation.where(field.to_sym => /#{Regexp.quote(value)}\z/i)
    when :does_not_end_with, :dnew
      @relation = relation.where(field.to_sym.not => /#{Regexp.quote(value)}\z/i)
    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
  self
end

#klassObject



73
74
75
# File 'lib/meta_search/searches/mongoid.rb', line 73

def klass
  relation
end

#metasearch_regexpObject



94
95
96
97
98
99
# File 'lib/meta_search/searches/mongoid.rb', line 94

def metasearch_regexp
  field_names = klass.fields.map(&:second).map(&:name)
  conditions = MetaSearch::DEFAULT_WHERES.map {|condition| condition[0...-1]} # pop tail options

  /\A(#{field_names.join('|')})_(#{conditions.join('|')})\z/
end

#respond_to?(name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/meta_search/searches/mongoid.rb', line 82

def respond_to? name, include_private = false
  name.to_s =~ metasearch_regexp or super
end