Class: Mongo::Model::Query

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model_class, selector = {}, options = {}) ⇒ Query

Returns a new instance of Query.



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

def initialize model_class, selector = {}, options = {}
  @model_class, @selector, @options = model_class, selector, options
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



50
51
52
53
54
55
56
# File 'lib/mongo/model/query.rb', line 50

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

Instance Attribute Details

#model_classObject (readonly)

Returns the value of attribute model_class.



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

def model_class
  @model_class
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



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

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

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



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

def build attributes = {}, options = {}
  model_class.build attributes, options do |model|
    model.set! selector
  end
end

#classObject



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

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

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



36
37
38
39
40
# File 'lib/mongo/model/query.rb', line 36

def create attributes = {}, options = {}
  model_class.create attributes, options do |model|
    model.set! selector
  end
end

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



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

def create! attributes = {}, options = {}
  model_class.create! attributes, options do |model|
    model.set! selector
  end
end

#inspectObject Also known as: to_s



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

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

#merge(query) ⇒ Object



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

def merge query
  model_class == query.model_class or
    model_class.is?(query.model_class) or
    query.model_class.is?(model_class) or
    raise("can't merge queries with different models!")

  self.class.new model_class, selector.merge(query.selector), options.merge(query.options)
end