Class: MetaSearch::Searches::MongoidSearchBuilder
- Inherits:
-
SimpleDelegator
- Object
- SimpleDelegator
- MetaSearch::Searches::MongoidSearchBuilder
- Defined in:
- lib/meta_search/searches/mongoid.rb
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
-
#relation ⇒ Object
readonly
Returns the value of attribute relation.
Instance Method Summary collapse
- #base ⇒ Object
- #build ⇒ Object
-
#initialize(relation, params, options) ⇒ MongoidSearchBuilder
constructor
A new instance of MongoidSearchBuilder.
- #klass ⇒ Object
- #metasearch_regexp ⇒ Object
- #method_missing(name, *args, &block) ⇒ Object
- #respond_to?(name, include_private = false) ⇒ Boolean
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, super(relation) @relation = relation @params, @options = params, 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
#options ⇒ Object (readonly)
Returns the value of attribute options.
67 68 69 |
# File 'lib/meta_search/searches/mongoid.rb', line 67 def @options end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
67 68 69 |
# File 'lib/meta_search/searches/mongoid.rb', line 67 def params @params end |
#relation ⇒ Object (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
#base ⇒ Object
69 70 71 |
# File 'lib/meta_search/searches/mongoid.rb', line 69 def base self end |
#build ⇒ Object
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().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 |
#klass ⇒ Object
73 74 75 |
# File 'lib/meta_search/searches/mongoid.rb', line 73 def klass relation end |
#metasearch_regexp ⇒ Object
94 95 96 97 98 99 |
# File 'lib/meta_search/searches/mongoid.rb', line 94 def 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
82 83 84 |
# File 'lib/meta_search/searches/mongoid.rb', line 82 def respond_to? name, include_private = false name.to_s =~ or super end |