Sha256: d69d211b735ca67c2b9de98f036c00bf7d1932eced887094c35efb86e58abd66

Contents?: true

Size: 1.33 KB

Versions: 2

Compression:

Stored size: 1.33 KB

Contents

# encoding: utf-8
module Mongoid #:nodoc:
  module Extensions #:nodoc:
    module Symbol #:nodoc:
      module Inflections #:nodoc:

        # return a class that will accept a value to convert the query correctly for near
        #
        # @param [Symbol] calc This accepts :sphere
        #
        # @return [Criterion::NearSpatial]

        def near(calc = :flat)
          Criterion::NearSpatial.new(:operator => get_op('near',calc), :key => self)
        end

        # alias for self.near(:sphere)
        #
        # @return [Criterion::NearSpatial]
        def near_sphere
          self.near(:sphere)
        end

        # @param [Symbol] shape :box,:polygon,:center,:center_sphere
        #
        # @return [Criterion::WithinSpatial]
        def within(shape)
          shape = get_op(:center,:sphere) if shape == :center_sphere
          Criterion::WithinSpatial.new(:operator => shape.to_s , :key => self)
        end

        private

        def get_op operator, calc
          if calc.to_sym == :sphere && Mongoid.master.connection.server_version >= '1.7'
            "#{operator}Sphere"
          elsif calc.to_sym == :sphere
            raise "MongoDB Server version #{Mongoid.master.connection.server_version} does not have Spherical Calculation"
          else
            operator.to_s
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mongoid_geospatial-1.0.0rc1 lib/mongoid_geospatial/extensions/symbol.rb
mongoid_geospatial-1.0.0rc0 lib/mongoid_geospatial/extensions/symbol.rb