module Opium::Model::Queryable::ClassMethods
Public Instance Methods
all( constraints = nil )
click to toggle source
# File lib/opium/model/queryable.rb, line 9 def all( constraints = nil ) constraints ? imbued_where( arrayize( constraints ), '$all' ) : criteria end
Also aliased as: all_in
between( constraints )
click to toggle source
# 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()
click to toggle source
# File lib/opium/model/queryable.rb, line 102 def cache criteria.update_variable( :cache, true ) end
cached?()
click to toggle source
# File lib/opium/model/queryable.rb, line 110 def cached? criteria.variables[:cache] end
dont_select( constraints )
click to toggle source
# File lib/opium/model/queryable.rb, line 64 def dont_select( constraints ) imbued_where( constraints, '$dontSelect' ) end
exists( constraints )
click to toggle source
# 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 )
click to toggle source
# File lib/opium/model/queryable.rb, line 26 def gt( constraints ) imbued_where( constraints, '$gt' ) end
gte( constraints )
click to toggle source
# File lib/opium/model/queryable.rb, line 30 def gte( constraints ) imbued_where( constraints, '$gte' ) end
in( constraints )
click to toggle source
# File lib/opium/model/queryable.rb, line 42 def in( constraints ) imbued_where( arrayize( constraints ), '$in' ) end
Also aliased as: any_in
keys( *field_names )
click to toggle source
# 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 )
click to toggle source
# File lib/opium/model/queryable.rb, line 87 def limit( value ) criteria.update_constraint( :limit, value ) end
lt( constraints )
click to toggle source
# File lib/opium/model/queryable.rb, line 34 def lt( constraints ) imbued_where( constraints, '$lt' ) end
lte( constraints )
click to toggle source
# File lib/opium/model/queryable.rb, line 38 def lte( constraints ) imbued_where( constraints, '$lte' ) end
ne( constraints )
click to toggle source
# File lib/opium/model/queryable.rb, line 52 def ne( constraints ) imbued_where( constraints, '$ne' ) end
nin( constraints )
click to toggle source
# File lib/opium/model/queryable.rb, line 48 def nin( constraints ) imbued_where( arrayize( constraints ), '$nin' ) end
or( *subqueries )
click to toggle source
# File lib/opium/model/queryable.rb, line 56 def or( *subqueries ) where( '$or' => subqueries.map {|query| translate_to_parse( query )}.freeze ) end
order( options )
click to toggle source
# File lib/opium/model/queryable.rb, line 78 def order( options ) validate_fields_exist( options ) previous = criteria.constraints[:order] ordering = ( [previous].compact + options.map {|key, value| (['-', 'desc', '-1'].include?( value.to_s ) ? '-' : '' ) + translate_name( key.to_s ) } ).join(',') criteria.update_constraint( :order, ordering ) end
pluck( field_name )
click to toggle source
Should be noted that pluck is an immediate query execution, so doesn't play well with further chainable criteria
# File lib/opium/model/queryable.rb, line 74 def pluck( field_name ) end
select( constraints )
click to toggle source
# File lib/opium/model/queryable.rb, line 60 def select( constraints ) imbued_where( constraints, '$select' ) end
skip( value )
click to toggle source
# File lib/opium/model/queryable.rb, line 91 def skip( value ) criteria.update_constraint( :skip, value ) end
uncache()
click to toggle source
# File lib/opium/model/queryable.rb, line 106 def uncache criteria.update_variable( :cache, false ) end
where( constraints )
click to toggle source
# File lib/opium/model/queryable.rb, line 95 def where( constraints ) validate_fields_exist( constraints ) criteria.update_constraint( :where, translate_to_parse( constraints ) ) end
Also aliased as: and
Private Instance Methods
arrayize( constraints )
click to toggle source
# File lib/opium/model/queryable.rb, line 150 def arrayize( constraints ) constraints.map {|key, value| [key, value.to_a]} end
convert_to_field_type( field_name, value )
click to toggle source
# File lib/opium/model/queryable.rb, line 136 def convert_to_field_type( field_name, value ) return value if value.frozen? case value when Array value.map {|i| convert_to_field_type( field_name, i)} when Hash Hash[ *value.flat_map {|k, v| [translate_name( k ), convert_to_field_type( field_name, v)]} ] when Criteria, Pointer, Model value.to_parse else model.fields[field_name].type.to_parse value end end
imbue_field_constraints_with_operator( constraints, operator )
click to toggle source
# File lib/opium/model/queryable.rb, line 158 def imbue_field_constraints_with_operator( constraints, operator ) Hash[ *constraints.flat_map {|key, value| [key, { operator => value }]} ] end
imbued_where( constraints, operator )
click to toggle source
# File lib/opium/model/queryable.rb, line 154 def imbued_where( constraints, operator ) where( imbue_field_constraints_with_operator( constraints, operator ) ) end
model()
click to toggle source
# File lib/opium/model/queryable.rb, line 116 def model self end
translate_name( field_name )
click to toggle source
# File lib/opium/model/queryable.rb, line 128 def translate_name( field_name ) field_name =~ /^\$/ ? field_name : model.parse_canonical_field_names[ field_name ] end
translate_to_parse( constraints )
click to toggle source
# File lib/opium/model/queryable.rb, line 132 def translate_to_parse( constraints ) Hash[ *constraints.flat_map {|key, value| [translate_name( key ), convert_to_field_type( key, value )]} ] end
validate_fields_exist( field_names )
click to toggle source
# File lib/opium/model/queryable.rb, line 120 def validate_fields_exist( field_names ) field_names = field_names.keys if field_names.respond_to? :keys unless field_names.all? {|field_name| model.fields.key?( field_name ) || field_name =~ /^\$/ } not_fields = field_names.reject {|field_name| model.fields.key? field_name } raise ArgumentError, "#{not_fields.join(', ')} #{not_fields.length > 1 ? 'are not fields' : 'is not a field'} on this model" end end