class Mongous::Filter
Public Class Methods
new( klass )
click to toggle source
# File lib/mongous/filter.rb, line 103 def initialize( klass ) @klass = klass @filter = {} @option = {} end
Public Instance Methods
[]( nth_or_range, len = nil )
click to toggle source
# File lib/mongous/filter.rb, line 204 def []( nth_or_range, len = nil ) case nth_or_range when Integer new_skip = nth_or_range if len.is_a?(NilClass) new_limit = 1 elsif len.is_a?(Integer) && len == 0 new_limit = nil elsif len.is_a?(Integer) && len > 0 new_limit = len else raise Mongous::Error, "invalid len. : #{ len }" end when Range from = nth_or_range.begin raise Mongous::Error, "invalid range. : #{ nth_or_range }" unless from.is_a? Integer to = nth_or_range.end raise Mongous::Error, "invalid range. : #{ nth_or_range }" unless to.is_a? Integer to -= 1 if nth_or_range.exclude_end? new_skip = from new_limit = to - from + 1 else raise Mongous::Error, "invalid class. : #{ nth_or_range }" end w = self.dup w.instance_variable_set( :@skip, new_skip ) w.instance_variable_set( :@limit, new_limit ) w end
all()
click to toggle source
# File lib/mongous/filter.rb, line 297 def all exec_query.map do |doc| @klass.new( **doc ) end end
attach( collection_name )
click to toggle source
# File lib/mongous/filter.rb, line 109 def attach( collection_name ) w = self.dup w.instance_variable_set( :@collection_name, collection_name.to_s ) w end
build_condition( conditions )
click to toggle source
# File lib/mongous/filter.rb, line 115 def build_condition( conditions ) hash = {} conditions.each do |key, item| case key when /\$(and|or|nor)/ hash[key] = item else case item when Array hash[key] = {"$in"=>item} when Range begin_oper = "$gte" end_oper = item.exclude_end? ? "$lt" : "$lte" if item.begin && item.end hash[key] = { begin_oper => item.begin, end_oper => item.end } elsif !item.begin && item.end hash[key] = { end_oper => item.end } elsif item.begin && !item.end hash[key] = { begin_oper => item.begin } else raise Mongous::Error, "invalid range. : #{ item }" end else hash[key] = item end end end hash end
count()
click to toggle source
# File lib/mongous/filter.rb, line 252 def count found = @klass.collection.find( @filter ) found = found.skip( @skip ) if @skip found = found.limit( @limit ) if @limit new_count = found.count_documents if @skip if @skip > new_count 0 elsif @limit [new_count - @skip, @limit].min else new_count - @skip end else if @limit [new_count, @limit].min else new_count end end end
delete()
click to toggle source
# File lib/mongous/filter.rb, line 311 def delete @klass.collection.delete_many( @filter ) end
each( &block )
click to toggle source
# File lib/mongous/filter.rb, line 303 def each( &block ) all.each( &block ) end
exec_query()
click to toggle source
# File lib/mongous/filter.rb, line 241 def exec_query new_filter = @filter new_option = @option.dup new_option[:projection] = @projection if @projection found = @klass.collection( @collection_name ).find( new_filter, new_option ) found = found.sort( @sort ) if @sort found = found.skip( @skip ) if @skip found = found.limit( @limit ) if @limit found end
first()
click to toggle source
# File lib/mongous/filter.rb, line 274 def first new_filter = @filter new_option = @option.dup new_option[:projection] = @projection if @projection found = @klass.collection( @collection_name ).find( new_filter, new_option ) new_order = @sort || { _id: 1 } doc = found.sort( new_order ).first @klass.new( **doc ) if doc end
last()
click to toggle source
# File lib/mongous/filter.rb, line 284 def last new_filter = @filter new_option = @option.dup new_option[:projection] = @projection if @projection found = @klass.collection( @collection_name ).find( new_filter, new_option ) new_order = {} ( @sort || {_id: 1} ).each do |k,v| new_order[k] = - v end doc = found.sort( new_order ).first @klass.new( **doc ) if doc end
map( &block )
click to toggle source
# File lib/mongous/filter.rb, line 307 def map( &block ) all.map( &block ) end
not( conditions = {} )
click to toggle source
# File lib/mongous/filter.rb, line 161 def not( conditions = {} ) hash = build_condition( conditions ) w = self.dup w.instance_variable_set( :@filter, @filter.merge( {"$nor" => [hash]} ) ) w end
option( new_option )
click to toggle source
# File lib/mongous/filter.rb, line 172 def option( new_option ) w = self.dup w.instance_variable_set( :@option, @option.merge( new_option ) ) w end
select( *keys, **hash )
click to toggle source
# File lib/mongous/filter.rb, line 178 def select( *keys, **hash ) if not keys.empty? new_projection = Hash[ keys.zip( Array.new(keys.length, 1) ) ] elsif not hash.empty? new_projection = hash else new_projection = nil end w = self.dup w.instance_variable_set( :@projection, new_projection ) w end
sort( *keys, **hash )
click to toggle source
# File lib/mongous/filter.rb, line 191 def sort( *keys, **hash ) if not keys.empty? new_sort = Hash[ keys.zip( Array.new( keys.length, 1 ) ) ] elsif not hash.empty? new_sort = hash else new_sort = nil end w = self.dup w.instance_variable_set( :@sort, new_sort ) w end
to_condition()
click to toggle source
# File lib/mongous/filter.rb, line 168 def to_condition @filter.dup end
where( conditions = {} )
click to toggle source
# File lib/mongous/filter.rb, line 154 def where( conditions = {} ) hash = build_condition( conditions ) w = self.dup w.instance_variable_set( :@filter, @filter.merge( hash ) ) w end