Sha256: 3603d735852ed4b3d1b949fe527dfe6253e3941108eb7ab37bd3af0b1bec8f80

Contents?: true

Size: 1.45 KB

Versions: 4

Compression:

Stored size: 1.45 KB

Contents

# encoding: utf-8
module Mongoid #:nodoc:
  module Criterion #:nodoc:

    # NearSpecial criterion is used when performing #near with symbols to get
    # get a shorthand syntax for where clauses. 
    #
    # @example Conversion of a simple to complex criterion.
    #   { :field => { "$nearSphere" => => [20,30]}, '$maxDistance' => 5 } 
    #   becomes:
    #   { :field.near(:sphere) => {:point => [20,30], :max => 5, :unit => :km} }
    class NearSpacial < Complex
      
      # Convert input to query for near or nearSphere
      #
      # @example
      #   near = NearSpacial.new(:key => :field, :operator => "near")
      #   near.to_mongo_query({:point => [:50,50], :max_distance => 5, :unit => :km}) => { '$near : [50,50]' , '$maxDistance' : 5 }
      #
      # @param [Hash,Array] v input to conver to query
      def to_mongo_query(v)
        if v.kind_of? Hash
          v[:point] = v[:point].to_lng_lat if v[:point].respond_to?(:to_lng_lat)
          query = {"$#{operator}" => v[:point] }
          if v[:max]
            unit = Mongoid::Spacial.earth_radius[v[:unit]]
            query['$maxDistance'] = (unit) ? v[:max]/unit : v[:max] 
          end
          query
        elsif v.kind_of? Array
          if v.first.kind_of? Numeric
            {"$#{operator}" => v }
          else
            v[0] = v[0].to_lng_lat if v[0].respond_to?(:to_lng_lat)
            {"$#{operator}" => v[0], '$maxDistance' => v[1] }
          end
        end
      end

    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
mongoid_spacial-0.2.0 lib/mongoid_spacial/criterion/near_spacial.rb
mongoid_spacial-0.1.1 lib/mongoid_spacial/criterion/near_spacial.rb
mongoid_spacial-0.1.0 lib/mongoid_spacial/criterion/near_spacial.rb
mongoid_spacial-0.0.1 lib/mongoid_spacial/criterion/near_spacial.rb