Module: Opium::Model::Queryable::ClassMethods
- Included in:
- Criteria
- Defined in:
- lib/opium/model/queryable.rb
Instance Method Summary collapse
- #all(constraints = nil) ⇒ Object (also: #all_in)
- #between(constraints) ⇒ Object
- #cache ⇒ Object
- #cached? ⇒ Boolean
- #dont_select(constraints) ⇒ Object
- #exists(constraints) ⇒ Object
- #gt(constraints) ⇒ Object
- #gte(constraints) ⇒ Object
- #in(constraints) ⇒ Object (also: #any_in)
- #keys(*field_names) ⇒ Object
- #limit(value) ⇒ Object
- #lt(constraints) ⇒ Object
- #lte(constraints) ⇒ Object
- #ne(constraints) ⇒ Object
- #nin(constraints) ⇒ Object
- #or(*subqueries) ⇒ Object
- #order(options) ⇒ Object
-
#pluck(field_name) ⇒ Object
Should be noted that pluck is an immediate query execution, so doesn’t play well with further chainable criteria.
- #select(constraints) ⇒ Object
- #skip(value) ⇒ Object
- #uncache ⇒ Object
- #where(constraints) ⇒ Object (also: #and)
Instance Method Details
#all(constraints = nil) ⇒ Object Also known as: all_in
9 10 11 |
# File 'lib/opium/model/queryable.rb', line 9 def all( constraints = nil ) constraints ? imbued_where( arrayize( constraints ), '$all' ) : criteria end |
#between(constraints) ⇒ Object
15 16 17 18 19 20 |
# File 'lib/opium/model/queryable.rb', line 15 def between( constraints ) start = constraints.map {|key, range| [key, range.begin]} inclusive = constraints.reject {|_, range| range.exclude_end?}.map {|key, range| [key, range.end]} exclusive = constraints.select {|_, range| range.exclude_end?}.map {|key, range| [key, range.end]} gte( start ).lte( inclusive ).lt( exclusive ) end |
#cache ⇒ Object
102 103 104 |
# File 'lib/opium/model/queryable.rb', line 102 def cache criteria.update_variable( :cache, true ) end |
#cached? ⇒ Boolean
110 111 112 |
# File 'lib/opium/model/queryable.rb', line 110 def cached? criteria.variables[:cache] end |
#dont_select(constraints) ⇒ Object
64 65 66 |
# File 'lib/opium/model/queryable.rb', line 64 def dont_select( constraints ) imbued_where( constraints, '$dontSelect' ) end |
#exists(constraints) ⇒ Object
22 23 24 |
# File 'lib/opium/model/queryable.rb', line 22 def exists( constraints ) imbued_where( constraints.map {|key, value| [key, value.to_bool.freeze] }, '$exists' ) end |
#gt(constraints) ⇒ Object
26 27 28 |
# File 'lib/opium/model/queryable.rb', line 26 def gt( constraints ) imbued_where( constraints, '$gt' ) end |
#gte(constraints) ⇒ Object
30 31 32 |
# File 'lib/opium/model/queryable.rb', line 30 def gte( constraints ) imbued_where( constraints, '$gte' ) end |
#in(constraints) ⇒ Object Also known as: any_in
42 43 44 |
# File 'lib/opium/model/queryable.rb', line 42 def in( constraints ) imbued_where( arrayize( constraints ), '$in' ) end |
#keys(*field_names) ⇒ Object
68 69 70 71 |
# File 'lib/opium/model/queryable.rb', line 68 def keys( *field_names ) validate_fields_exist( field_names ) criteria.update_constraint( :keys, field_names.map(&method(:translate_name)).join(',') ) end |
#limit(value) ⇒ Object
87 88 89 |
# File 'lib/opium/model/queryable.rb', line 87 def limit( value ) criteria.update_constraint( :limit, value ) end |
#lt(constraints) ⇒ Object
34 35 36 |
# File 'lib/opium/model/queryable.rb', line 34 def lt( constraints ) imbued_where( constraints, '$lt' ) end |
#lte(constraints) ⇒ Object
38 39 40 |
# File 'lib/opium/model/queryable.rb', line 38 def lte( constraints ) imbued_where( constraints, '$lte' ) end |
#ne(constraints) ⇒ Object
52 53 54 |
# File 'lib/opium/model/queryable.rb', line 52 def ne( constraints ) imbued_where( constraints, '$ne' ) end |
#nin(constraints) ⇒ Object
48 49 50 |
# File 'lib/opium/model/queryable.rb', line 48 def nin( constraints ) imbued_where( arrayize( constraints ), '$nin' ) end |
#or(*subqueries) ⇒ Object
56 57 58 |
# File 'lib/opium/model/queryable.rb', line 56 def or( *subqueries ) where( '$or' => subqueries.map {|query| translate_to_parse( query )}.freeze ) end |
#order(options) ⇒ Object
78 79 80 81 82 83 84 85 |
# File 'lib/opium/model/queryable.rb', line 78 def order( ) validate_fields_exist( ) previous = criteria.constraints[:order] ordering = ( [previous].compact + .map {|key, value| (['-', 'desc', '-1'].include?( value.to_s ) ? '-' : '' ) + translate_name( key.to_s ) } ).join(',') criteria.update_constraint( :order, ordering ) end |
#pluck(field_name) ⇒ Object
Should be noted that pluck is an immediate query execution, so doesn’t play well with further chainable criteria
74 75 76 |
# File 'lib/opium/model/queryable.rb', line 74 def pluck( field_name ) end |
#select(constraints) ⇒ Object
60 61 62 |
# File 'lib/opium/model/queryable.rb', line 60 def select( constraints ) imbued_where( constraints, '$select' ) end |
#skip(value) ⇒ Object
91 92 93 |
# File 'lib/opium/model/queryable.rb', line 91 def skip( value ) criteria.update_constraint( :skip, value ) end |
#uncache ⇒ Object
106 107 108 |
# File 'lib/opium/model/queryable.rb', line 106 def uncache criteria.update_variable( :cache, false ) end |
#where(constraints) ⇒ Object Also known as: and
95 96 97 98 |
# File 'lib/opium/model/queryable.rb', line 95 def where( constraints ) validate_fields_exist( constraints ) criteria.update_constraint( :where, translate_to_parse( constraints ) ) end |