Module: Origin::Selectable

Extended by:
Macroable
Included in:
Queryable
Defined in:
lib/origin/selectable.rb

Overview

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.

Constant Summary

LINE_STRING =

Constant for a LineString $geometry.

Since:

  • 2.0.0

"LineString"
POINT =

Constant for a Point $geometry.

Since:

  • 2.0.0

"Point"
POLYGON =

Constant for a Polygon $geometry.

Since:

  • 2.0.0

"Polygon"

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from Macroable

key

Instance Attribute Details

- (Object) negating

Returns the value of attribute negating



27
28
29
# File 'lib/origin/selectable.rb', line 27

def negating
  @negating
end

- (Object) negating If the next spression is negated.(Ifthe)



27
# File 'lib/origin/selectable.rb', line 27

attr_accessor :negating, :selector

- (Object) selector

Returns the value of attribute selector



27
28
29
# File 'lib/origin/selectable.rb', line 27

def selector
  @selector
end

- (Object) selector The query selector.(Thequeryselector.)



27
# File 'lib/origin/selectable.rb', line 27

attr_accessor :negating, :selector

Class Method Details

+ (Array<Symbol>) forwardables

Get the methods on the selectable that can be forwarded to from a model.

Examples:

Get the forwardable methods.

Selectable.forwardables

Since:

  • 1.0.0



626
627
628
629
# File 'lib/origin/selectable.rb', line 626

def forwardables
  public_instance_methods(false) -
    [ :negating, :negating=, :negating?, :selector, :selector= ]
end

Instance Method Details

- (Selectable) all(criterion = nil) Also known as: all_in

Add the $all criterion.

Examples:

Add the criterion.

selectable.all(field: [ 1, 2 ])

Execute an $all in a where query.

selectable.where(:field.all => [ 1, 2 ])

Since:

  • 1.0.0



42
43
44
# File 'lib/origin/selectable.rb', line 42

def all(criterion = nil)
  send(strategy || :__union__, with_array_values(criterion), "$all")
end

- (Selectable) and(*criterion) Also known as: all_of

Add the $and criterion.

Examples:

Add the criterion.

selectable.and({ field: value }, { other: value })

Since:

  • 1.0.0



59
60
61
# File 'lib/origin/selectable.rb', line 59

def and(*criterion)
  __multi__(criterion, "$and")
end

- (Selectable) between(criterion = nil)

Add the range selection.

Examples:

Match on results within a single range.

selectable.between(field: 1..2)

Match on results between multiple ranges.

selectable.between(field: 1..2, other: 5..7)

Since:

  • 1.0.0



77
78
79
80
81
82
83
84
# File 'lib/origin/selectable.rb', line 77

def between(criterion = nil)
  selection(criterion) do |selector, field, value|
    selector.store(
      field,
      { "$gte" => value.min, "$lte" => value.max }
    )
  end
end

- (Selectable) elem_match(criterion = nil)

Select with an $elemMatch.

Examples:

Add criterion for a single match.

selectable.elem_match(field: { name: "value" })

Add criterion for multiple matches.

selectable.elem_match(
  field: { name: "value" },
  other: { name: "value"}
)

Execute an $elemMatch in a where query.

selectable.where(:field.elem_match => { name: "value" })

Since:

  • 1.0.0



105
106
107
# File 'lib/origin/selectable.rb', line 105

def elem_match(criterion = nil)
  __override__(criterion, "$elemMatch")
end

- (Selectable) exists(criterion = nil)

Add the $exists selection.

Examples:

Add a single selection.

selectable.exists(field: true)

Add multiple selections.

selectable.exists(field: true, other: false)

Execute an $exists in a where query.

selectable.where(:field.exists => true)

Since:

  • 1.0.0



126
127
128
129
130
# File 'lib/origin/selectable.rb', line 126

def exists(criterion = nil)
  typed_override(criterion, "$exists") do |value|
    ::Boolean.evolve(value)
  end
end

- (Selectable) geo_spacial(criterion = nil)

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

Add a $geoIntersects or $geoWithin selection. Symbol operators must be used as shown in the examples to expand the criteria.

Examples:

Add a geo intersect criterion for a line.

query.geo_spacial(:location.intersects_line => [[ 1, 10 ], [ 2, 10 ]])

Add a geo intersect criterion for a point.

query.geo_spacial(:location.intersects_point => [[ 1, 10 ]])

Add a geo intersect criterion for a polygon.

query.geo_spacial(:location.intersects_polygon => [[ 1, 10 ], [ 2, 10 ], [ 1, 10 ]])

Add a geo within criterion for a polygon.

query.geo_spacial(:location.within_polygon => [[ 1, 10 ], [ 2, 10 ], [ 1, 10 ]])

Since:

  • 2.0.0



160
161
162
# File 'lib/origin/selectable.rb', line 160

def geo_spacial(criterion = nil)
  __merge__(criterion)
end

- (Selectable) gt(criterion = nil)

Add the $gt criterion to the selector.

Examples:

Add the $gt criterion.

selectable.gt(age: 60)

Execute an $gt in a where query.

selectable.where(:field.gt => 10)

Since:

  • 1.0.0



189
190
191
# File 'lib/origin/selectable.rb', line 189

def gt(criterion = nil)
  __override__(criterion, "$gt")
end

- (Selectable) gte(criterion = nil)

Add the $gte criterion to the selector.

Examples:

Add the $gte criterion.

selectable.gte(age: 60)

Execute an $gte in a where query.

selectable.where(:field.gte => 10)

Since:

  • 1.0.0



207
208
209
# File 'lib/origin/selectable.rb', line 207

def gte(criterion = nil)
  __override__(criterion, "$gte")
end

- (Selectable) in(criterion = nil) Also known as: any_in

Adds the $in selection to the selectable.

Examples:

Add $in selection on an array.

selectable.in(age: [ 1, 2, 3 ])

Add $in selection on a range.

selectable.in(age: 18..24)

Execute an $in in a where query.

selectable.where(:field.in => [ 1, 2, 3 ])

Since:

  • 1.0.0



228
229
230
# File 'lib/origin/selectable.rb', line 228

def in(criterion = nil)
  send(strategy || :__intersect__, with_array_values(criterion), "$in")
end

- (Selectable) lt(criterion = nil)

Add the $lt criterion to the selector.

Examples:

Add the $lt criterion.

selectable.lt(age: 60)

Execute an $lt in a where query.

selectable.where(:field.lt => 10)

Since:

  • 1.0.0



247
248
249
# File 'lib/origin/selectable.rb', line 247

def lt(criterion = nil)
  __override__(criterion, "$lt")
end

- (Selectable) lte(criterion = nil)

Add the $lte criterion to the selector.

Examples:

Add the $lte criterion.

selectable.lte(age: 60)

Execute an $lte in a where query.

selectable.where(:field.lte => 10)

Since:

  • 1.0.0



265
266
267
# File 'lib/origin/selectable.rb', line 265

def lte(criterion = nil)
  __override__(criterion, "$lte")
end

- (Selectable) max_distance(criterion = nil)

Add a $maxDistance selection to the selectable.

Examples:

Add the $maxDistance selection.

selectable.max_distance(location: 10)

Since:

  • 1.0.0



280
281
282
# File 'lib/origin/selectable.rb', line 280

def max_distance(criterion = nil)
  __add__(criterion, "$maxDistance")
end

- (Selectable) mod(criterion = nil)

Adds $mod selection to the selectable.

Examples:

Add the $mod selection.

selectable.mod(field: [ 10, 1 ])

Execute an $mod in a where query.

selectable.where(:field.mod => [ 10, 1 ])

Since:

  • 1.0.0



297
298
299
# File 'lib/origin/selectable.rb', line 297

def mod(criterion = nil)
  __override__(criterion, "$mod")
end

- (Selectable) ne(criterion = nil) Also known as: excludes

Adds $ne selection to the selectable.

Examples:

Query for a value $ne to something.

selectable.ne(field: 10)

Execute an $ne in a where query.

selectable.where(:field.ne => "value")

Since:

  • 1.0.0



315
316
317
# File 'lib/origin/selectable.rb', line 315

def ne(criterion = nil)
  __override__(criterion, "$ne")
end

- (Selectable) near(criterion = nil)

Adds a $near criterion to a geo selection.

Examples:

Add the $near selection.

selectable.near(location: [ 23.1, 12.1 ])

Execute an $near in a where query.

selectable.where(:field.near => [ 23.2, 12.1 ])

Since:

  • 1.0.0



334
335
336
# File 'lib/origin/selectable.rb', line 334

def near(criterion = nil)
  __override__(criterion, "$near")
end

- (Selectable) near_sphere(criterion = nil)

Adds a $nearSphere criterion to a geo selection.

Examples:

Add the $nearSphere selection.

selectable.near_sphere(location: [ 23.1, 12.1 ])

Execute an $nearSphere in a where query.

selectable.where(:field.near_sphere => [ 10.11, 3.22 ])

Since:

  • 1.0.0



352
353
354
# File 'lib/origin/selectable.rb', line 352

def near_sphere(criterion = nil)
  __override__(criterion, "$nearSphere")
end

- (true, false) negating?

Is the current selectable negating the next selection?

Examples:

Is the selectable negating?

selectable.negating?

Since:

  • 1.0.0



401
402
403
# File 'lib/origin/selectable.rb', line 401

def negating?
  !!negating
end

- (Selectable) nin(criterion = nil) Also known as: not_in

Adds the $nin selection to the selectable.

Examples:

Add $nin selection on an array.

selectable.nin(age: [ 1, 2, 3 ])

Add $nin selection on a range.

selectable.nin(age: 18..24)

Execute an $nin in a where query.

selectable.where(:field.nin => [ 1, 2, 3 ])

Since:

  • 1.0.0



373
374
375
# File 'lib/origin/selectable.rb', line 373

def nin(criterion = nil)
  send(strategy || :__intersect__, with_array_values(criterion), "$nin")
end

- (Selectable) nor(*criterion)

Adds $nor selection to the selectable.

Examples:

Add the $nor selection.

selectable.nor(field: 1, field: 2)

Since:

  • 1.0.0



389
390
391
# File 'lib/origin/selectable.rb', line 389

def nor(*criterion)
  __multi__(criterion, "$nor")
end

- (Selectable) not(*criterion)

Negate the next selection.

Examples:

Negate the selection.

selectable.not.in(field: [ 1, 2 ])

Add the $not criterion.

selectable.not(name: /Bob/)

Execute a $not in a where query.

selectable.where(:field.not => /Bob/)

Since:

  • 1.0.0



421
422
423
424
425
426
427
# File 'lib/origin/selectable.rb', line 421

def not(*criterion)
  if criterion.empty?
    tap { |query| query.negating = true }
  else
    __override__(criterion.first, "$not")
  end
end

- (Selectable) or(*criterion) Also known as: any_of

Adds $or selection to the selectable.

Examples:

Add the $or selection.

selectable.or(field: 1, field: 2)

Since:

  • 1.0.0



440
441
442
# File 'lib/origin/selectable.rb', line 440

def or(*criterion)
  __multi__(criterion, "$or")
end

- (Selectable) where(criterion = nil)

This is the general entry point for most MongoDB queries. This either creates a standard field: value selection, and expanded selection with the use of hash methods, or a $where selection if a string is provided.

Examples:

Add a standard selection.

selectable.where(name: "syd")

Add a javascript selection.

selectable.where("this.name == 'syd'")

Since:

  • 1.0.0



509
510
511
# File 'lib/origin/selectable.rb', line 509

def where(criterion = nil)
  criterion.is_a?(String) ? js_query(criterion) : expr_query(criterion)
end

- (Selectable) with_size(criterion = nil)

Note:

This method is named #with_size not to conflict with any existing #size method on enumerables or symbols.

Add a $size selection for array fields.

Examples:

Add the $size selection.

selectable.with_size(field: 5)

Execute an $size in a where query.

selectable.where(:field.with_size => 10)

Since:

  • 1.0.0



461
462
463
464
465
# File 'lib/origin/selectable.rb', line 461

def with_size(criterion = nil)
  typed_override(criterion, "$size") do |value|
    ::Integer.evolve(value)
  end
end

- (Selectable) with_type(criterion = nil)

Note:

vurl.me/PGOU contains a list of all types.

Adds a $type selection to the selectable.

Examples:

Add the $type selection.

selectable.with_type(field: 15)

Execute an $type in a where query.

selectable.where(:field.with_type => 15)

Since:

  • 1.0.0



485
486
487
488
489
# File 'lib/origin/selectable.rb', line 485

def with_type(criterion = nil)
  typed_override(criterion, "$type") do |value|
    ::Integer.evolve(value)
  end
end