Class: Mongous::Filter

Inherits:
Object
  • Object
show all
Defined in:
lib/mongous/filter.rb

Instance Method Summary collapse

Constructor Details

#initialize(klass) ⇒ Filter

Returns a new instance of Filter.



103
104
105
106
107
# File 'lib/mongous/filter.rb', line 103

def initialize( klass )
  @klass  =  klass
  @filter  =  {}
  @option  =  {}
end

Instance Method Details

#[](nth_or_range, len = nil) ⇒ Object



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# 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

#allObject



297
298
299
300
301
# File 'lib/mongous/filter.rb', line 297

def all
  exec_query.map do |doc|
    @klass.new( **doc )
  end
end

#attach(collection_name) ⇒ Object



109
110
111
112
113
# 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) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# 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

#countObject



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# 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

#deleteObject



311
312
313
# File 'lib/mongous/filter.rb', line 311

def delete
  @klass.collection.delete_many( @filter )
end

#each(&block) ⇒ Object



303
304
305
# File 'lib/mongous/filter.rb', line 303

def each( &block )
  all.each( &block )
end

#exec_queryObject



241
242
243
244
245
246
247
248
249
250
# 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

#firstObject



274
275
276
277
278
279
280
281
282
# 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

#lastObject



284
285
286
287
288
289
290
291
292
293
294
295
# 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) ⇒ Object



307
308
309
# File 'lib/mongous/filter.rb', line 307

def map( &block )
  all.map( &block )
end

#not(conditions = {}) ⇒ Object



161
162
163
164
165
166
# 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) ⇒ Object



172
173
174
175
176
# 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) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
189
# 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) ⇒ Object



191
192
193
194
195
196
197
198
199
200
201
202
# 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_conditionObject



168
169
170
# File 'lib/mongous/filter.rb', line 168

def to_condition
  @filter.dup
end

#where(conditions = {}) ⇒ Object



154
155
156
157
158
159
# 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