lib/mongoid_spacial/criterion/near_spacial.rb
# Coninputert input to query for near or nearSphere
#
# @example
#   near = NearSpacial.new(:key => :field, :operator => "near")
#   near.to_mongo_query({:point => [:50,50], :max => 5, :unit => :km}) => { '$near : [50,50]' , '$maxDistance' : 5 }
#
# @param [Hash,Array] input input to coninputer to query
Coninputert input to query for near or nearSphere
Coninputert input to query for near or nearSphere
def to_mongo_query(input)
        if input.kind_of?(Hash)
          raise ':point required to make valid query' unless input[:point]
          input[:point] = input[:point].to_lng_lat if input[:point].respond_to?(:to_lng_lat)
          query = {"$#{operator}" => input[:point] }
          if input[:max]
            query['$maxDistance'] = input[:max].to_f
            if unit = Mongoid::Spacial.earth_radius[input[:unit]]
              unit *= Mongoid::Spacial::RAD_PER_DEG unless operator =~ /sphere/i
              input[:unit] = unit
            end
            query['$maxDistance'] = query['$maxDistance']/input[:unit].to_f if input[:unit]
          end
          query
        elsif input.kind_of? Array
          if input.first.kind_of? Numeric
            {"$#{operator}" => input }
          else
            input[0] = input[0].to_lng_lat if input[0].respond_to?(:to_lng_lat)
            {"$#{operator}" => input[0], '$maxDistance' => input[1] }
          end
        end
      end
def to_mongo_query ( input ) if input . kind_of? ( Hash ) raise ':point required to make valid query' unless input [ :point ] input [ :point ] = input [ :point ] . to_lng_lat if input [ :point ] . respond_to? ( :to_lng_lat ) query = { "$#{operator}" => input [ :point ] } if input [ :max ] query [ '$maxDistance' ] = input [ :max ] . to_f if unit = Mongoid :: Spacial . earth_radius [ input [ :unit ] ] unit *= Mongoid :: Spacial :: RAD_PER_DEG unless operator =~ / sphere /i input [ :unit ] = unit end query [ '$maxDistance' ] = query [ '$maxDistance' ] / input [ :unit ] . to_f if input [ :unit ] end query elsif input . kind_of? Array if input . first . kind_of? Numeric { "$#{operator}" => input } else input [ 0 ] = input [ 0 ] . to_lng_lat if input [ 0 ] . respond_to? ( :to_lng_lat ) { "$#{operator}" => input [ 0 ] , '$maxDistance' => input [ 1 ] } end end end
Coninputert input to query for near or nearSphere
Coninputert input to query for near or nearSphere
input
input input to coninputer to query
input input to coninputer to query
example
None