lib/origin/selectable.rb in origin-1.1.0 vs lib/origin/selectable.rb in origin-2.0.0

- old
+ new

@@ -3,13 +3,27 @@ # An origin selectable is selectable, in that it has the ability to select # document from the database. The selectable module brings all functionality # to the selectable that has to do with building MongoDB selectors. module Selectable - include Mergeable extend Macroable + # Constant for a LineString $geometry. + # + # @since 2.0.0 + LINE_STRING = "LineString" + + # Constant for a Point $geometry. + # + # @since 2.0.0 + POINT = "Point" + + # Constant for a Polygon $geometry. + # + # @since 2.0.0 + POLYGON = "Polygon" + # @attribute [rw] negating If the next spression is negated. # @attribute [rw] selector The query selector. attr_accessor :negating, :selector # Add the $all criterion. @@ -116,10 +130,51 @@ end key :exists, :override, "$exists" do |value| ::Boolean.evolve(value) end + # Add a $geoIntersects or $geoWithin selection. Symbol operators must be used as shown in + # the examples to expand the criteria. + # + # @note The only valid geometry shapes for a $geoIntersects are: + # :intersects_line, :intersects_point, and :intersects_polygon. + # + # @note The only valid geometry shape for a $geoWithin is :within_polygon + # + # @example Add a geo intersect criterion for a line. + # query.geo_spacial(:location.intersects_line => [[ 1, 10 ], [ 2, 10 ]]) + # + # @example Add a geo intersect criterion for a point. + # query.geo_spacial(:location.intersects_point => [[ 1, 10 ]]) + # + # @example Add a geo intersect criterion for a polygon. + # query.geo_spacial(:location.intersects_polygon => [[ 1, 10 ], [ 2, 10 ], [ 1, 10 ]]) + # + # @example Add a geo within criterion for a polygon. + # query.geo_spacial(:location.within_polygon => [[ 1, 10 ], [ 2, 10 ], [ 1, 10 ]]) + # + # @param [ Hash ] criterion The criterion. + # + # @return [ Selectable ] The cloned selectable. + # + # @since 2.0.0 + def geo_spacial(criterion = nil) + __merge__(criterion) + end + key :intersects_line, :override, "$geoIntersects", "$geometry" do |value| + { "type" => LINE_STRING, "coordinates" => value } + end + key :intersects_point, :override, "$geoIntersects", "$geometry" do |value| + { "type" => POINT, "coordinates" => value } + end + key :intersects_polygon, :override, "$geoIntersects", "$geometry" do |value| + { "type" => POLYGON, "coordinates" => value } + end + key :within_polygon, :override, "$geoWithin", "$geometry" do |value| + { "type" => POLYGON, "coordinates" => value } + end + # Add the $gt criterion to the selector. # # @example Add the $gt criterion. # selectable.gt(age: 60) # @@ -362,11 +417,15 @@ # # @return [ Selectable ] The negated selectable. # # @since 1.0.0 def not(*criterion) - (criterion.size == 0) ? tap { |query| query.negating = true } : __override__(criterion.first, "$not") + if criterion.empty? + tap { |query| query.negating = true } + else + __override__(criterion.first, "$not") + end end key :not, :override, "$not" # Adds $or selection to the selectable. # @@ -449,86 +508,10 @@ # @since 1.0.0 def where(criterion = nil) criterion.is_a?(String) ? js_query(criterion) : expr_query(criterion) end - # Adds the $within/$box selection to the selectable. - # - # @example Add the selection. - # selectable.within_box(location: [[ 1, 10 ], [ 10, 1 ]]) - # - # @example Execute an $within/$box in a where query. - # selectable.where(:field.within_box => [[ 1, 10 ], [ 10, 1 ]]) - # - # @param [ Hash ] criterion The field/box corner criterion. - # - # @return [ Selectable ] The cloned selectable. - # - # @since 1.0.0 - def within_box(criterion = nil) - __expanded__(criterion, "$within", "$box") - end - key :within_box, :expanded, "$within", "$box" - - # Adds the $within/$center selection to the selectable. - # - # @example Add the selection. - # selectable.within_circle(location: [[ 1, 10 ], 25 ]) - # - # @example Execute an $within/$center in a where query. - # selectable.where(:field.within_circle => [[ 1, 10 ], 25 ]) - # - # @param [ Hash ] criterion The field/radius criterion. - # - # @return [ Selectable ] The cloned selectable. - # - # @since 1.0.0 - def within_circle(criterion = nil) - __expanded__(criterion, "$within", "$center") - end - key :within_circle, :expanded, "$within", "$center" - - # Adds the $within/$polygon selection to the selectable. - # - # @example Add the selection. - # selectable.within_polygon( - # location: [[ 10, 20 ], [ 10, 40 ], [ 30, 40 ], [ 30, 20 ]] - # ) - # - # @example Execute an $within/$polygon in a where query. - # selectable.where( - # :field.within_polygon => [[ 10, 20 ], [ 10, 40 ], [ 30, 40 ], [ 30, 20 ]] - # ) - # - # @param [ Hash ] criterion The field/polygon points criterion. - # - # @return [ Selectable ] The cloned selectable. - # - # @since 1.0.0 - def within_polygon(criterion = nil) - __expanded__(criterion, "$within", "$polygon") - end - key :within_polygon, :expanded, "$within", "$polygon" - - # Adds the $within/$centerSphere selection to the selectable. - # - # @example Add the selection. - # selectable.within_spherical_circle(location: [[ 1, 10 ], 25 ]) - # - # @example Execute an $within/$centerSphere in a where query. - # selectable.where(:field.within_spherical_circle => [[ 1, 10 ], 25 ]) - # - # @param [ Hash ] criterion The field/distance criterion. - # - # @return [ Selectable ] The cloned selectable. - # - # @since 1.0.0 - def within_spherical_circle(criterion = nil) - __expanded__(criterion, "$within", "$centerSphere") - end - key :within_spherical_circle, :expanded, "$within", "$centerSphere" - private # Create the standard expression query. # # @api private @@ -541,10 +524,10 @@ # @return [ Selectable ] The cloned selectable. # # @since 1.0.0 def expr_query(criterion) selection(criterion) do |selector, field, value| - selector.merge!(field.specify(value.__expand_complex__, negating?)) + selector.merge!(field.__expr_part__(value.__expand_complex__, negating?)) end end # Force the values of the criterion to be evolved. #