Class: Mongo::Model::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/mongo/model/query.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, selector = {}, options = {} *args) ⇒ Query

Returns a new instance of Query.



4
5
6
# File 'lib/mongo/model/query.rb', line 4

def initialize model, selector = {}, options = {} *args
  @model, @selector, @options = model, selector, options
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (protected)



39
40
41
42
43
44
45
# File 'lib/mongo/model/query.rb', line 39

def method_missing method, *args, &block
  model.with_scope selector, options do
    result = model.send method, *args, &block
    result = self.merge result if result.is_a? self.class
    result
  end
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



2
3
4
# File 'lib/mongo/model/query.rb', line 2

def model
  @model
end

#optionsObject (readonly)

Returns the value of attribute options.



2
3
4
# File 'lib/mongo/model/query.rb', line 2

def options
  @options
end

#selectorObject (readonly)

Returns the value of attribute selector.



2
3
4
# File 'lib/mongo/model/query.rb', line 2

def selector
  @selector
end

Instance Method Details

#==(o) ⇒ Object



22
23
24
# File 'lib/mongo/model/query.rb', line 22

def == o
  self.class == o.class and ([model, selector, options] == [o.model, o.selector, o.options])
end

#build(attributes = {}, options = {}) ⇒ Object



26
27
28
# File 'lib/mongo/model/query.rb', line 26

def build attributes = {}, options = {}
  model.build selector.merge(attributes), options
end

#classObject



8
9
10
# File 'lib/mongo/model/query.rb', line 8

def class
  ::Mongo::Model::Query
end

#create(attributes = {}, options = {}) ⇒ Object



30
31
32
# File 'lib/mongo/model/query.rb', line 30

def create attributes = {}, options = {}
  model.create selector.merge(attributes), options
end

#create!(attributes = {}, options = {}) ⇒ Object



34
35
36
# File 'lib/mongo/model/query.rb', line 34

def create! attributes = {}, options = {}
  model.create! selector.merge(attributes), options
end

#inspectObject Also known as: to_s



17
18
19
# File 'lib/mongo/model/query.rb', line 17

def inspect
  "#<Mongo::Model::Query: #{model} #{@selector.inspect} #{@options.inspect}>"
end

#merge(query) ⇒ Object



12
13
14
15
# File 'lib/mongo/model/query.rb', line 12

def merge query
  raise "can't merge queries with different models!" unless model == query.model
  self.class.new model, selector.merge(query.selector), options.merge(query.options)
end