Class: Parse::Constraint::WithinPolygonQueryConstraint

Inherits:
Parse::Constraint show all
Defined in:
lib/parse/query/constraints.rb

Overview

Equivalent to the $geoWithin Parse query operation and $polygon geopoints constraint. The polygon area is defined by a list of GeoPoint objects that make up the enclosed area. A polygon query should have 3 or more geopoints. Please note that some Geo queries that cross the international date lines are not currently supported by Parse.

As many points as you want, minimum 3

q.where :field.within_polygon => [geopoint1, geopoint2, geopoint3]

Polygon for the Bermuda Triangle

bermuda = Parse::GeoPoint.new 32.3078000,-64.7504999 # Bermuda miami = Parse::GeoPoint.new 25.7823198,-80.2660226 # Miami, FL san_juan = Parse::GeoPoint.new 18.3848232,-66.0933608 # San Juan, PR

get all sunken ships inside the Bermuda Triangle

SunkenShip.all :location.within_polygon => [bermuda, san_juan, miami]

Instance Attribute Summary

Attributes inherited from Parse::Constraint

#operand, #operation, #operator, #value

Instance Method Summary collapse

Methods inherited from Parse::Constraint

#as_json, contraint_keyword, create, formatted_value, #formatted_value, #initialize, #key, #precedence, register, #to_s

Constructor Details

This class inherits a constructor from Parse::Constraint

Instance Method Details

#build ⇒ Hash

Returns the compiled constraint.

Returns:

  • (Hash) —

    the compiled constraint.



703
704
705
706
707
708
709
710
711
712
713
714
# File 'lib/parse/query/constraints.rb', line 703

def build
  geopoint_values = formatted_value
  unless geopoint_values.is_a?(Array) &&
         geopoint_values.all? {|point| point.is_a?(Parse::GeoPoint) } &&
         geopoint_values.count > 2
    raise ArgumentError, '[Parse::Query] Invalid query value parameter passed to'\
                ' `within_polygon` constraint: Value must be an array with 3'\
                ' or more `Parse::GeoPoint` objects'
  end

  { @operation.operand => { :$geoWithin => { :$polygon => geopoint_values } } }
end

#within_polygon ⇒ WithinPolygonQueryConstraint

A registered method on a symbol to create the constraint. Maps to Parse operator "$geoWithin" with "$polygon" subconstraint. Takes an array of GeoPoint objects.

Examples:

# As many points as you want
q.where :field.within_polygon => [geopoint1, geopoint2, geopoint3]

Returns:

Version:

  • 1.7.0 (requires Server v2.4.2 or later)



699
# File 'lib/parse/query/constraints.rb', line 699

contraint_keyword :$geoWithin