class RansackMongo::MongoAdapter

Constants

PREDICATES

Public Class Methods

new() click to toggle source
# File lib/ransack_mongo/mongo_adapter.rb, line 5
def initialize
  @query = {}
end
predicates() click to toggle source
# File lib/ransack_mongo/mongo_adapter.rb, line 73
def self.predicates
  PREDICATES
end

Public Instance Methods

cont_matcher(attr, value) click to toggle source
# File lib/ransack_mongo/mongo_adapter.rb, line 21
def cont_matcher(attr, value)
  @query[attr] = /#{value}/i
end
eq_matcher(attr, value) click to toggle source
# File lib/ransack_mongo/mongo_adapter.rb, line 13
def eq_matcher(attr, value)
  @query[attr] = value
end
gt_matcher(attr, value) click to toggle source
# File lib/ransack_mongo/mongo_adapter.rb, line 43
def gt_matcher(attr, value)
  append_sizeable_matcher('$gt', attr, value)
end
gteq_matcher(attr, value) click to toggle source
# File lib/ransack_mongo/mongo_adapter.rb, line 51
def gteq_matcher(attr, value)
  append_sizeable_matcher('$gte', attr, value)
end
in_matcher(attr, value) click to toggle source
# File lib/ransack_mongo/mongo_adapter.rb, line 25
def in_matcher(attr, value)
  @query[attr] = { '$in' => value }
end
lt_matcher(attr, value) click to toggle source
# File lib/ransack_mongo/mongo_adapter.rb, line 47
def lt_matcher(attr, value)
  append_sizeable_matcher('$lt', attr, value)
end
lteq_matcher(attr, value) click to toggle source
# File lib/ransack_mongo/mongo_adapter.rb, line 55
def lteq_matcher(attr, value)
  append_sizeable_matcher('$lte', attr, value)
end
mstart_matcher(attr, value) click to toggle source
# File lib/ransack_mongo/mongo_adapter.rb, line 33
def mstart_matcher(attr, value)
  values = value.split(",").map do |current|
    if (current = current.strip).length > 0
      /^#{current}/i
    end
  end.compact

  @query[attr] = { '$in' => values }
end
not_eq_matcher(attr, value) click to toggle source
# File lib/ransack_mongo/mongo_adapter.rb, line 17
def not_eq_matcher(attr, value)
  @query[attr] = { '$ne' => value }
end
or_op() { || ... } click to toggle source
# File lib/ransack_mongo/mongo_adapter.rb, line 59
def or_op # or operation
  return unless block_given?

  original_query = @query
  @query = {}

  yield

  original_query['$or'] ||= []
  original_query['$or'].concat @query.map { |attr, value| { attr => value } }

  @query = original_query
end
start_matcher(attr, value) click to toggle source
# File lib/ransack_mongo/mongo_adapter.rb, line 29
def start_matcher(attr, value)
  @query[attr] = { '$in' => [/^#{value}/] }
end
to_query() click to toggle source
# File lib/ransack_mongo/mongo_adapter.rb, line 9
def to_query
  @query
end

Private Instance Methods

append_sizeable_matcher(m, attr, value) click to toggle source
# File lib/ransack_mongo/mongo_adapter.rb, line 79
def append_sizeable_matcher(m, attr, value)
  @query[attr] ||= {}
  @query[attr][m] = parse_sizeable_value(value)
end
parse_sizeable_value(value) click to toggle source
# File lib/ransack_mongo/mongo_adapter.rb, line 84
def parse_sizeable_value(value)
  case value
  when Date, Time
    value
  else
    Float(value) rescue value
  end
end