Sha256: 41b2aea8b55407cd95e14928d02ff65e2d80d543428d0fb5ac2d94d7e12838bb

Contents?: true

Size: 1.58 KB

Versions: 8

Compression:

Stored size: 1.58 KB

Contents

# encoding: utf-8
module Mongoid #:nodoc:
  module Extensions #:nodoc:
    module Symbol #:nodoc:
      module Inflections #:nodoc:
        
        # $nearSphere $centerSphere        
        # nearMax
        # - { $near : [50,50] , $maxDistance : 5 }
        # withinBox
        # - {"$within" : {"$box" : box}
        # withinCenter
        # - {"$within" : {"$center" : [center, radius]}}})          

        def nearSphere
          Criterion::Complex.new(:operator => 'nearSphere', :key => self)          
        end

        def nearMax *calcs
          calcs = (!calcs || calcs.empty?) ? [:flat] : calcs
          case calcs.size
          when 1
            Criterion::TwinOperators.new(:op_a => get_op(calcs.first, 'near'), :op_b => get_op(calcs.first, 'maxDistance'), :key => self)
          when 2
            Criterion::TwinOperators.new(:op_a => get_op(calcs.first, 'near'), :op_b => get_op(calcs.last, 'maxDistance'), :key => self)            
          else
            raise "method nearMax takes one or two symbols as arguments, each symbol must be either :flat or :sphere"
          end
        end

        def withinBox calc = :flat
          Criterion::OuterOperator.new(:outer_op => 'within', :operator => get_op(calc, 'box'), :key => self)
        end

        def withinCenter calc = :flat
          Criterion::OuterOperator.new(:outer_op => 'within', :operator => get_op(calc, 'center'), :key => self)
        end 
        
        private
        
        def get_op calc, operator
          calc.to_s == 'sphere' ? "#{operator}Sphere" : operator
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
mongoid_geo-0.1.8 lib/mongoid/geo/inflections.rb
mongoid_geo-0.1.7 lib/mongoid/geo/inflections.rb
mongoid_geo-0.1.6 lib/mongoid/geo/inflections.rb
mongoid_geo-0.1.5 lib/mongoid/geo/inflections.rb
mongoid_geo-0.1.4 lib/mongoid/geo/inflections.rb
mongoid_geo-0.1.3 lib/mongoid/geo/inflections.rb
mongoid_geo-0.1.2 lib/mongoid/geo/inflections.rb
mongoid_geo-0.1.1 lib/mongoid/geo/inflections.rb