Module: MongoMapper::Document::ClassMethods

Defined in:
lib/mongomapper/document.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/mongomapper/document.rb', line 150

def method_missing(method, *args)
  finder = DynamicFinder.new(self, method)
  
  if finder.valid?
    meta_def(finder.options[:method]) do |*args|
      find_with_args(args, finder.options)
    end
    
    send(finder.options[:method], *args)
  else
    super
  end
end

Instance Method Details

#all(options = {}) ⇒ Object



57
58
59
# File 'lib/mongomapper/document.rb', line 57

def all(options={})
  find_every(options)
end

#collection(name = nil) ⇒ Object



133
134
135
136
137
138
139
140
# File 'lib/mongomapper/document.rb', line 133

def collection(name=nil)
  if name.nil?
    @collection ||= database.collection(self.to_s.demodulize.tableize)
  else
    @collection = database.collection(name)
  end
  @collection
end

#connection(mongo_connection = nil) ⇒ Object



115
116
117
118
119
120
121
122
# File 'lib/mongomapper/document.rb', line 115

def connection(mongo_connection=nil)
  if mongo_connection.nil?
    @connection ||= MongoMapper.connection
  else
    @connection = mongo_connection
  end
  @connection
end

#count(conditions = {}) ⇒ Object



68
69
70
# File 'lib/mongomapper/document.rb', line 68

def count(conditions={})
  collection.find(FinderOptions.to_mongo_criteria(conditions)).count
end

#create(*docs) ⇒ Object



72
73
74
75
76
77
78
79
80
# File 'lib/mongomapper/document.rb', line 72

def create(*docs)
  instances = []
  docs = [{}] if docs.blank?
  docs.flatten.each do |attrs|
    doc = new(attrs); doc.save
    instances << doc
  end
  instances.size == 1 ? instances[0] : instances
end

#database(name = nil) ⇒ Object



124
125
126
127
128
129
130
131
# File 'lib/mongomapper/document.rb', line 124

def database(name=nil)
  if name.nil?
    @database ||= MongoMapper.database
  else
    @database = connection.db(name)
  end
  @database
end

#delete(*ids) ⇒ Object



97
98
99
100
# File 'lib/mongomapper/document.rb', line 97

def delete(*ids)
  criteria = FinderOptions.to_mongo_criteria(:_id => ids.flatten)
  collection.remove(criteria)
end

#delete_all(conditions = {}) ⇒ Object



102
103
104
105
# File 'lib/mongomapper/document.rb', line 102

def delete_all(conditions={})
  criteria = FinderOptions.to_mongo_criteria(conditions)
  collection.remove(criteria)
end

#destroy(*ids) ⇒ Object



107
108
109
# File 'lib/mongomapper/document.rb', line 107

def destroy(*ids)
  find_some(ids.flatten).each(&:destroy)
end

#destroy_all(conditions = {}) ⇒ Object



111
112
113
# File 'lib/mongomapper/document.rb', line 111

def destroy_all(conditions={})
  find(:all, :conditions => conditions).each(&:destroy)
end

#find(*args) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/mongomapper/document.rb', line 25

def find(*args)
  options = args.extract_options!

  case args.first
    when :first then find_first(options)
    when :last  then find_last(options)
    when :all   then find_every(options)
    else             find_from_ids(args, options)
  end
end

#find_by_id(id) ⇒ Object



61
62
63
64
65
66
# File 'lib/mongomapper/document.rb', line 61

def find_by_id(id)
  criteria = FinderOptions.to_mongo_criteria(:_id => id)
  if doc = collection.find_one(criteria)
    new(doc)
  end
end

#first(options = {}) ⇒ Object



49
50
51
# File 'lib/mongomapper/document.rb', line 49

def first(options={})
  find_first(options)
end

#last(options = {}) ⇒ Object



53
54
55
# File 'lib/mongomapper/document.rb', line 53

def last(options={})
  find_last(options)
end

#paginate(options) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/mongomapper/document.rb', line 36

def paginate(options)
  per_page      = options.delete(:per_page)
  page          = options.delete(:page)
  total_entries = count(options[:conditions] || {})
  collection    = Pagination::PaginationProxy.new(total_entries, page, per_page)

  options[:limit]   = collection.limit
  options[:offset]  = collection.offset

  collection.subject = find_every(options)
  collection
end

#timestamps!Object



142
143
144
145
146
147
# File 'lib/mongomapper/document.rb', line 142

def timestamps!
  key :created_at, Time
  key :updated_at, Time
  
  class_eval { before_save :update_timestamps }
end

#update(*args) ⇒ Object

For updating single document

Person.update(1, {:foo => 'bar'})

For updating multiple documents at once:

Person.update({'1' => {:foo => 'bar'}, '2' => {:baz => 'wick'}})


87
88
89
90
91
92
93
94
95
# File 'lib/mongomapper/document.rb', line 87

def update(*args)
  updating_multiple = args.length == 1
  if updating_multiple
    update_multiple(args[0])
  else
    id, attributes = args
    update_single(id, attributes)
  end
end